using DamageAssesment.Api.Answers.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Any;
namespace DamageAssesment.Api.Answers.Controllers
{
[ApiController]
public class AnswersController: ControllerBase
{
private IAnswersProvider answerProvider;
public AnswersController(IAnswersProvider answersProvider) {
this.answerProvider=answersProvider;
}
///
/// Get all answers
///
[HttpGet("answers")]
public async Task GetAnswersAsync() {
var result = await answerProvider.GetAnswersAsync();
if(result.IsSuccess)
{
return Ok(result.Answers);
}
return NoContent();
}
///
/// Get an answer based on answerId.
///
[HttpGet("answers/{id}")]
public async Task GetAnswerByIdAsync(int id)
{
var result = await answerProvider.GetAnswerByIdAsync(id);
if (result.IsSuccess)
{
return Ok(result.Answer);
}
return NotFound();
}
///
/// Get all answers based on responseId.
///
[HttpGet("answers/byresponse/{responseid}")]
public async Task GetAnswersByResponseId(int responseid)
{
var result = await this.answerProvider.GetAnswersAsync(responseid);
if(result.IsSuccess)
{
return Ok(result.Answers);
}
return NoContent();
}
///
/// Get all answers based on questionId.
///
[HttpGet("answers/byquestion/{questionid}")]
public async Task AnswersByQuestionId(int questionid)
{
var result = await this.answerProvider.GetAnswersByQuestionAsync(questionid);
if (result.IsSuccess)
{
return Ok(result.Answers);
}
return NotFound();
}
///
/// Update an existing answer.
///
[HttpPut("answers")]
public async Task UpdateAnswer(Models.Answer answer)
{
if (answer != null)
{
var result = await this.answerProvider.UpdateAnswerAsync(answer);
if (result.IsSuccess)
{
return Ok(result.Answer);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return NotFound();
}
///
/// Save a new answer.
///
[HttpPost("answers")]
public async Task CreateAnswer(Models.Answer answer)
{
if (answer != null)
{
var result = await this.answerProvider.PostAnswerAsync(answer);
if (result.IsSuccess)
{
return Ok(result.Answer);
}
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", new { id = answer.Id }, answer);
}
///
/// Delete an existing answer.
///
[HttpDelete("answers/{id}")]
public async Task DeleteAnswer(int id)
{
var result = await this.answerProvider.DeleteAnswerAsync(id);
if (result.IsSuccess)
{
return Ok(result.Answer);
}
return NotFound();
}
}
}