2023-08-15 22:52:30 -05:00
|
|
|
|
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;
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all answers
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpGet("Answers")]
|
|
|
|
|
public async Task<ActionResult> GetAnswersAsync() {
|
|
|
|
|
|
|
|
|
|
var result = await answerProvider.GetAnswersAsync();
|
|
|
|
|
if(result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answers);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get an answer based on answerId.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpGet("Answers/{Id}")]
|
|
|
|
|
public async Task<ActionResult> GetAnswerByIdAsync(int Id)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var result = await answerProvider.GetAnswerByIdAsync(Id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answer);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all answers based on responseId.
|
|
|
|
|
/// </summary>
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpGet("Answers/ByResponse/{responseid}")]
|
|
|
|
|
public async Task<IActionResult> GetAnswersByResponseId(int responseid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.answerProvider.GetAnswersAsync(responseid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if(result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answers);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get all answers based on questionId.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-13 00:28:24 -05:00
|
|
|
|
[HttpGet("Answers/ByQuestion/{questionid}")]
|
|
|
|
|
public async Task<IActionResult> AnswersByQuestionId(int questionid)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-13 00:28:24 -05:00
|
|
|
|
var result = await this.answerProvider.GetAnswersByQuestionAsync(questionid);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answers);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update an existing answer.
|
|
|
|
|
/// </summary>
|
2023-08-15 22:52:30 -05:00
|
|
|
|
|
|
|
|
|
[HttpPut("Answers")]
|
2023-08-27 10:55:58 -05:00
|
|
|
|
public async Task<IActionResult> UpdateAnswer(Models.Answer answer)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (answer != null)
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
var result = await this.answerProvider.UpdateAnswerAsync(answer);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answer);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage == "Not Found")
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Save a new answer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpPost("Answers")]
|
2023-08-27 10:55:58 -05:00
|
|
|
|
public async Task<IActionResult> CreateAnswer(Models.Answer answer)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
|
|
|
|
if (answer != null)
|
|
|
|
|
{
|
2023-08-27 10:55:58 -05:00
|
|
|
|
var result = await this.answerProvider.PostAnswerAsync(answer);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answer);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = answer.Id }, answer);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Delete an existing answer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpDelete("Answers/{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteAnswer(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.answerProvider.DeleteAnswerAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Answer);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|