using DamageAssesment.Api.Surveys.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Surveys.Controllers
{
[ApiController]
public class SurveysController : ControllerBase
{
private ISurveyProvider surveyProvider;
public SurveysController(ISurveyProvider surveyProvider)
{
this.surveyProvider = surveyProvider;
}
///
/// GET request for retrieving surveys.
///
[Authorize(Roles ="admin,survey,user,report")]
[Route("Surveys")]
[Route("Surveys/{language:alpha}")]
[HttpGet]
public async Task GetSurveysAsync(string? language)
{
var result = await this.surveyProvider.GetSurveysAsync(language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
///
/// GET request for retrieving surveys by ID.
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("Surveys/{id:int}")]
[Route("Surveys/{id:int}/{language:alpha}")]
[HttpGet]
public async Task GetSurveysAsync(int id, string? language)
{
var result = await this.surveyProvider.GetSurveysAsync(id, language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NotFound();
}
///
/// POST request for creating a new survey.
///
[Authorize(Roles = "admin,survey,user,report")]
[HttpPost("Surveys")]
public async Task PostSurveysAsync(Models.Survey survey)
{
var result = await this.surveyProvider.PostSurveyAsync(survey);
if (result.IsSuccess)
{
return Ok(result.Survey);
}
return BadRequest(result.ErrorMessage);
}
///
/// PUT request for updating an existing survey (surveyId,Updated Survey data).
///
[Authorize(Roles = "admin,survey")]
[HttpPut("Surveys/{id}")]
public async Task 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);
}
///
/// DELETE request for deleting a survey by ID.
///
[Authorize(Roles = "admin,survey")]
[HttpDelete("Surveys/{id}")]
public async Task DeleteSurveysAsync(int id)
{
var result = await this.surveyProvider.DeleteSurveyAsync(id);
if (result.IsSuccess)
{
return Ok(result.Survey);
}
return NotFound();
}
}
}