using DamageAssesment.Api.Responses.Interfaces;
using DamageAssesment.Api.Responses.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Responses.Controllers
{
[ApiController]
public class ResponsesController : ControllerBase
{
private readonly ISurveysResponse surveyResponseProvider;
private readonly IExcelExportService excelExportService;
public ResponsesController(ISurveysResponse surveyResponseProvider, IExcelExportService excelExportService)
{
this.surveyResponseProvider = surveyResponseProvider;
this.excelExportService = excelExportService;
}
///
/// GET request for retrieving survey responses.
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/{employeeid:int}")]
[Route("responses")]
[HttpGet]
public async Task GetSurveyResponsesAsync(int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesAsync(employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.surveyResponses);
}
if (result.ErrorMessage == "No Data Found")
return NoContent();
return BadRequest(result.ErrorMessage);
}
///
/// GET request for retrieving survey responses by survey ID.
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/bysurvey/{surveyid:int}/{employeeid:int}")]
[Route("responses/bysurvey/{surveyid:int}")]
[HttpGet]
public async Task GetSurveyResponsesAsync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
}
return NoContent();
}
///
/// GET request for retrieving survey responses by survey and location IDs.
///
/// The ID of the survey for which responses are to be retrieved.
/// The ID of the location for which responses are to be retrieved.
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/{surveyid:int}/{locationid:int}/{employeeid:int}")]
[Route("responses/{surveyid:int}/{locationid:int}")]
[HttpGet]
public async Task GetSurveyResponsesBySurveyAndLocationAsync(int surveyid, int locationid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyid, locationid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
}
return NoContent();
}
///
/// GET request for retrieving survey responses by survey, question, and answer.
///
/// The ID of the survey for which responses are to be retrieved.
/// The ID of the question for which responses are to be retrieved.
/// The answer for which responses are to be retrieved.
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/byanswer/{surveyid:int}/{questionid:int}/{answer:alpha}/{employeeid:int}")]
[Route("responses/byanswer/{surveyid:int}/{questionid:int}/{answer:alpha}")]
[HttpGet]
public async Task GetSurveyResponsesByAnswerAsyncAsync(int surveyid, int questionid, string answer, int? employeeid)
{
var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyid, questionid, answer, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
}
return NoContent();
}
///
/// GET request for retrieving answers from survey responses by survey ID and region.
///
/// The ID of the survey for which answers are to be retrieved.
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/byregion/{surveyid:int}")]
[Route("responses/byregion/{surveyid:int}/{employeeid}")]
[HttpGet]
public async Task GetAnswersByRegionAsync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.Answers);
}
return NoContent();
}
///
/// GET request for retrieving survey responses by survey ID and maintenance center.
///
/// The ID of the survey for which responses are to be retrieved.
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/bymaintenancecenter/{surveyid:int}/{employeeid:int}")]
[Route("responses/bymaintenancecenter/{surveyid:int}")]
[HttpGet]
public async Task GetAnswersByMaintenaceCentersync(int surveyid, int? employeeid)
{
var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyid, employeeid ?? 0);
if (result.IsSuccess)
{
return Ok(result.SurveyResponses);
}
return NoContent();
}
///
/// GET request for retrieving a survey response by response ID.
///
/// The ID of the survey response to be retrieved.
[Authorize(Roles = "admin,survey,user,report")]
[HttpGet("responses/{id}")]
public async Task GetSurveyResponseByIdAsync(int id)
{
var result = await this.surveyResponseProvider.GetSurveyResponseByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.SurveyResponse);
}
return NoContent();
}
///
/// POST request for creating a new survey response.
///
/// The survey response object to be created.
[Authorize(Roles = "admin,survey,user,report")]
[HttpPost("responses")]
public async Task PostSurveysAsync(Models.SurveyResponse surveyResponse)
{
var result = await this.surveyResponseProvider.PostSurveyResponseAsync(surveyResponse);
if (result.IsSuccess)
{
return Ok(result.SurveyResponse);
}
return BadRequest(result.ErrorMessage);
}
///
/// PUT request for updating an existing survey response.
///
/// The ID of the survey response to be updated.
/// The updated survey response object.
[Authorize(Roles = "admin,survey,user,report")]
[HttpPut("responses/{id}")]
public async Task PutSurveyResponseAsync(int id, Models.SurveyResponse surveyResponse)
{
var result = await this.surveyResponseProvider.PutSurveyResponseAsync(id, surveyResponse);
if (result.IsSuccess)
{
return Ok(result.SurveyResponse);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
///
/// DELETE request for deleting an existing survey response.
///
[Authorize(Roles = "admin,survey,user,report")]
[HttpDelete("responses/{id}")]
public async Task DeleteSurveyResponseAsync(int id)
{
var result = await this.surveyResponseProvider.DeleteSurveyResponseAsync(id);
if (result.IsSuccess)
{
return Ok(result.SurveyResponse);
}
return NotFound();
}
///
/// POST request for submitting survey with multiple answers.
///
/// The answers to be submitted for the survey.
[Authorize(Roles = "admin,survey,user,report")]
[HttpPost("responses/answers")]
public async Task PostSurveyAnswersAsync(Request request)
{
var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(request);
if (result.IsSuccess)
return Ok(result.SurveyResponse);
else
return BadRequest(result.ErrorMessage);
}
///
/// Get All active surveys .
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("responses/surveys/active")]
[Route("responses/surveys/active/{language:alpha}")]
[Route("responses/surveys/active/{employeeid:int}")]
[Route("responses/surveys/active/{employeeid:int}/{language:alpha}")]
[HttpGet]
public async Task GetActiveSurveysAsync(int? employeeid, string? language)
{
var result = await this.surveyResponseProvider.GetActiveSurveysAsync(employeeid, language);
if (result.IsSuccess)
{
return Ok(result.Surveys);
}
return NoContent();
}
///
/// Export all survey response data based on survey id.
///
[Authorize(Roles = "admin,survey,user,report")]
[HttpGet]
[Route("responses/surveys/export/{surveyid}")]
public async Task GetExcelSurveysAsync(int surveyid, string language, bool IsAdmin = false)
{
var result = await this.surveyResponseProvider.ExportSurveyResponsesAsync(surveyid, language, IsAdmin);
if (result.IsSuccess && result.surveyResponses.Count > 0)
{
byte[] fileContents = excelExportService.ExportToExcel