using DamageAssesment.Api.Surveys.Interfaces; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.Surveys.Controllers { [Route("api/[controller]")] [ApiController] public class SurveysController : ControllerBase { private ISurveyProvider surveyProvider; public SurveysController(ISurveyProvider surveyProvider) { this.surveyProvider = surveyProvider; } /// /// GET request for retrieving surveys. /// [HttpGet] public async Task GetSurveysAsync() { var result = await this.surveyProvider.GetSurveysAsync(); if (result.IsSuccess) { return Ok(result.Surveys); } return NoContent(); } /// /// GET request for retrieving surveys by ID. /// [HttpGet("{Id}")] public async Task GetSurveysAsync(int Id) { var result = await this.surveyProvider.GetSurveysAsync(Id); if (result.IsSuccess) { return Ok(result.Surveys); } return NotFound(); } /// /// POST request for creating a new survey. /// [HttpPost] 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). /// [HttpPut("{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. /// [HttpDelete("{Id}")] public async Task DeleteSurveysAsync(int Id) { var result = await this.surveyProvider.DeleteSurveyAsync(Id); if (result.IsSuccess) { return Ok(result.Survey); } return NotFound(); } } }