DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs
2023-08-25 09:17:53 -04:00

110 lines
3.2 KiB
C#

using DamageAssesment.Api.Surveys.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Surveys.Controllers
{
[Route("api")]
[ApiController]
public class SurveysController : ControllerBase
{
private ISurveyProvider surveyProvider;
public SurveysController(ISurveyProvider surveyProvider)
{
this.surveyProvider = surveyProvider;
}
/// <summary>
/// GET request for retrieving surveys.
/// </summary>
[Route("Surveys")]
[Route("{Language}/Surveys")]
[HttpGet]
public async Task<ActionResult> GetSurveysAsync(string? Language)
{
var result = await this.surveyProvider.GetSurveysAsync(Language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
<<<<<<< HEAD
[Route("Surveys/{Id}")]
[Route("{Language}/Surveys/{Id}")]
[HttpGet]
public async Task<ActionResult> GetSurveysAsync(int Id, string? Language)
=======
/// <summary>
/// GET request for retrieving surveys by ID.
/// </summary>
[HttpGet("{Id}")]
public async Task<ActionResult> GetSurveysAsync(int Id)
>>>>>>> cf3a04891b7b50d0a02ac9c8b9a78ccb9436c35c
{
var result = await this.surveyProvider.GetSurveysAsync(Id, Language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NotFound();
}
/// <summary>
/// POST request for creating a new survey.
/// </summary>
[HttpPost("Surveys")]
public async Task<ActionResult> PostSurveysAsync(Models.Survey survey)
{
var result = await this.surveyProvider.PostSurveyAsync(survey);
if (result.IsSuccess)
{
return Ok(result.Survey);
}
return BadRequest(result.ErrorMessage);
}
/// <summary>
/// PUT request for updating an existing survey (surveyId,Updated Survey data).
/// </summary>
[HttpPut("Surveys/{Id}")]
public async Task<ActionResult> PutSurveysAsync(int Id, Models.Survey survey)
{
var result = await this.surveyProvider.PutSurveyAsync(Id, survey);
if (result.IsSuccess)
{
return Ok(result.Survey);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
<<<<<<< HEAD
[HttpDelete("Surveys/{Id}")]
=======
/// <summary>
/// DELETE request for deleting a survey by ID.
/// </summary>
[HttpDelete("{Id}")]
>>>>>>> cf3a04891b7b50d0a02ac9c8b9a78ccb9436c35c
public async Task<ActionResult> DeleteSurveysAsync(int Id)
{
var result = await this.surveyProvider.DeleteSurveyAsync(Id);
if (result.IsSuccess)
{
return Ok(result.Survey);
}
return NotFound();
}
}
}