using DamageAssesment.Api.SurveyResponses.Interfaces; using DamageAssesment.Api.SurveyResponses.Models; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; namespace DamageAssesment.Api.SurveyResponses.Controllers { [Route("api")] [ApiController] public class SurveyResponsesController : ControllerBase { private readonly ISurveysResponse surveyResponseProvider; public SurveyResponsesController(ISurveysResponse surveyResponseProvider) { this.surveyResponseProvider = surveyResponseProvider; } [HttpGet("SurveyResponses")] public async Task GetSurveyResponsesAsync() { var result = await this.surveyResponseProvider.GetSurveyResponsesAsync(); if (result.IsSuccess) { return Ok(result.surveyResponses); } if (result.ErrorMessage == "No Data Found") return NoContent(); return BadRequest(result.ErrorMessage); } [HttpGet("SurveyResponses/{surveyId}")] public async Task GetSurveyResponsesAsync(int surveyId) { var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAsync(surveyId); if (result.IsSuccess) { return Ok(result.SurveyResponses); } return NoContent(); } [HttpGet("Responses/{surveyId}/{locationId}")] public async Task GetSurveyResponsesBySurveyAndLocationAsync(int surveyId, string locationId) { var result = await this.surveyResponseProvider.GetSurveyResponsesBySurveyAndLocationAsync(surveyId, locationId); if (result.IsSuccess) { return Ok(result.SurveyResponses); } return NoContent(); } [HttpGet("ResponsesByAnswer/{surveyId}/{questionId}/{answer}")] public async Task GetSurveyResponsesByAnswerAsyncAsync(int surveyId, int questionId, string answer) { var result = await surveyResponseProvider.GetResponsesByAnswerAsync(surveyId, questionId, answer); if (result.IsSuccess) { return Ok(result.SurveyResponses); } return NoContent(); } [HttpGet("AnswersByRegion/{surveyId}")] public async Task GetAnswersByRegionAsync(int surveyId) { var result = await this.surveyResponseProvider.GetAnswersByRegionAsync(surveyId); if (result.IsSuccess) { return Ok(result.Answers); } return NoContent(); } [HttpGet("AnswersByMaintenanceCenter/{surveyId}")] public async Task GetAnswersByMaintenaceCentersync(int surveyId) { var result = await this.surveyResponseProvider.GetSurveyResponsesByMaintenanceCenterAsync(surveyId); if (result.IsSuccess) { return Ok(result.SurveyResponses); } return NoContent(); } [HttpGet("SurveyResponse/{responseId}")] public async Task GetSurveyResponseByIdAsync(int responseId) { var result = await this.surveyResponseProvider.GetSurveyResponseByIdAsync(responseId); if (result.IsSuccess) { return Ok(result.SurveyResponse); } return NoContent(); } [HttpPost("SurveyResponses")] 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); } [HttpPut("SurveyResponses/{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); } [HttpDelete("SurveyResponses/{Id}")] public async Task DeleteSurveyResponseAsync(int Id) { var result = await this.surveyResponseProvider.DeleteSurveyResponseAsync(Id); if (result.IsSuccess) { return Ok(result.SurveyResponse); } return NotFound(); } [HttpPost("SurveyResponses/Answers")] public async Task PostSurveyAnswersAsync(AnswerRequest answers) { /* var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(surveyAnswers); if (result.IsSuccess) { return Ok(result.SurveyResponse); } return BadRequest(result.ErrorMessage);*/ var result = await this.surveyResponseProvider.PostSurveyAnswersAsync(answers); if (result.IsSuccess) return Ok(result.SurveyResponse); else return BadRequest(result.ErrorMessage); } } }