DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Surveys/Controllers/SurveysController.cs

98 lines
3.2 KiB
C#
Raw Normal View History

2023-08-15 22:52:30 -05:00
using DamageAssesment.Api.Surveys.Interfaces;
using Microsoft.AspNetCore.Authorization;
2023-08-15 22:52:30 -05:00
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Surveys.Controllers
{
[ApiController]
public class SurveysController : ControllerBase
{
private ISurveyProvider surveyProvider;
public SurveysController(ISurveyProvider surveyProvider)
{
this.surveyProvider = surveyProvider;
}
/// <summary>
/// GET request for retrieving surveys.
/// </summary>
[Authorize(Roles ="admin,survey,user,report")]
2023-10-04 10:55:35 -05:00
[Route("surveys")]
[Route("surveys/{language:alpha}")]
2023-08-15 22:52:30 -05:00
[HttpGet]
2023-09-08 14:40:06 -05:00
public async Task<ActionResult> GetSurveysAsync(string? language)
2023-08-15 22:52:30 -05:00
{
2023-09-08 14:40:06 -05:00
var result = await this.surveyProvider.GetSurveysAsync(language);
2023-08-15 22:52:30 -05:00
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
2023-08-25 10:36:31 -05:00
/// <summary>
/// GET request for retrieving surveys by ID.
/// </summary>
[Authorize(Roles = "admin,survey,user,report")]
2023-10-04 10:55:35 -05:00
[Route("surveys/{id:int}")]
[Route("surveys/{id:int}/{language:alpha}")]
[HttpGet]
2023-09-08 14:40:06 -05:00
public async Task<ActionResult> GetSurveysAsync(int id, string? language)
2023-08-15 22:52:30 -05:00
{
2023-09-08 14:40:06 -05:00
var result = await this.surveyProvider.GetSurveysAsync(id, language);
2023-08-15 22:52:30 -05:00
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NotFound();
}
/// <summary>
/// POST request for creating a new survey.
/// </summary>
[Authorize(Roles = "admin,survey,user,report")]
2023-10-04 10:55:35 -05:00
[HttpPost("surveys")]
2023-08-15 22:52:30 -05:00
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>
2023-08-25 10:36:31 -05:00
[Authorize(Roles = "admin,survey")]
2023-10-04 10:55:35 -05:00
[HttpPut("surveys/{id}")]
2023-09-08 14:40:06 -05:00
public async Task<ActionResult> PutSurveysAsync(int id, Models.Survey survey)
2023-08-15 22:52:30 -05:00
{
2023-09-08 14:40:06 -05:00
var result = await this.surveyProvider.PutSurveyAsync(id, survey);
2023-08-15 22:52:30 -05:00
if (result.IsSuccess)
{
return Ok(result.Survey);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
2023-08-15 22:52:30 -05:00
return BadRequest(result.ErrorMessage);
}
/// <summary>
/// DELETE request for deleting a survey by ID.
/// </summary>
[Authorize(Roles = "admin,survey")]
2023-10-04 10:55:35 -05:00
[HttpDelete("surveys/{id}")]
2023-09-08 14:40:06 -05:00
public async Task<ActionResult> DeleteSurveysAsync(int id)
2023-08-15 22:52:30 -05:00
{
2023-09-08 14:40:06 -05:00
var result = await this.surveyProvider.DeleteSurveyAsync(id);
2023-08-15 22:52:30 -05:00
if (result.IsSuccess)
{
return Ok(result.Survey);
}
return NotFound();
}
}
}