120 lines
3.9 KiB
C#
120 lines
3.9 KiB
C#
using DamageAssesment.Api.Surveys.Interfaces;
|
|
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>
|
|
[Route("surveys")]
|
|
[Route("surveys/{language:alpha}")]
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetSurveysAsync(string? language)
|
|
{
|
|
var result = await this.surveyProvider.GetSurveysAsync(language);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Surveys);
|
|
}
|
|
return NoContent();
|
|
}
|
|
/// <summary>
|
|
/// GET request for retrieving all active surveys.
|
|
/// </summary>
|
|
[Route("surveys/active")]
|
|
[Route("surveys/active/{language:alpha}")]
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetActiveSurveysAsync(string? language)
|
|
{
|
|
var result = await this.surveyProvider.GetActiveSurveysAsync(true,language);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Surveys);
|
|
}
|
|
return NoContent();
|
|
}
|
|
/// <summary>
|
|
/// GET request for retrieving all inactive surveys.
|
|
/// </summary>
|
|
[Route("surveys/inactive")]
|
|
[Route("surveys/inactive/{language:alpha}")]
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetInActiveSurveysAsync(string? language)
|
|
{
|
|
var result = await this.surveyProvider.GetActiveSurveysAsync(false, language);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Surveys);
|
|
}
|
|
return NoContent();
|
|
}
|
|
/// <summary>
|
|
/// GET request for retrieving surveys by ID.
|
|
/// </summary>
|
|
[Route("surveys/{id:int}")]
|
|
[Route("surveys/{id:int}/{language:alpha}")]
|
|
[HttpGet]
|
|
public async Task<ActionResult> GetSurveysAsync(int id, string? language)
|
|
{
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DELETE request for deleting a survey by ID.
|
|
/// </summary>
|
|
[HttpDelete("surveys/{id}")]
|
|
public async Task<ActionResult> DeleteSurveysAsync(int id)
|
|
{
|
|
var result = await this.surveyProvider.DeleteSurveyAsync(id);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Survey);
|
|
}
|
|
return NotFound();
|
|
}
|
|
}
|
|
}
|