DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Answers/Controllers/AnswersController.cs
2023-08-15 23:52:30 -04:00

112 lines
3.5 KiB
C#

using DamageAssesment.Api.Answers.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Any;
namespace DamageAssesment.Api.Answers.Controllers
{
[Route("api")]
[ApiController]
public class AnswersController: ControllerBase
{
private IAnswersProvider answerProvider;
public AnswersController(IAnswersProvider answersProvider) {
this.answerProvider=answersProvider;
}
//get all answers
[HttpGet("Answers")]
public async Task<ActionResult> GetAnswersAsync() {
var result = await answerProvider.GetAnswersAsync();
if(result.IsSuccess)
{
return Ok(result.Answers);
}
return NoContent();
}
//get answer based on answerid
[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();
}
// get all answers based on response id
[HttpGet("AnswersByResponse/{ResponseId}")]
public async Task<IActionResult> GetAnswersByResponseId(int ResponseId)
{
var result = await this.answerProvider.GetAnswersAsync(ResponseId);
if(result.IsSuccess)
{
return Ok(result.Answers);
}
return NoContent();
}
// get all answers based on question id
[HttpGet("AnswersByQuestion/{QuestionId}")]
public async Task<IActionResult> AnswersByQuestionId(int QuestionId)
{
var result = await this.answerProvider.GetAnswersByQuestionAsync(QuestionId);
if (result.IsSuccess)
{
return Ok(result.Answers);
}
return NotFound();
}
//update existing answer
[HttpPut("Answers")]
public async Task<IActionResult> UpdateAnswer(Db.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 new answer
[HttpPost("Answers")]
public async Task<IActionResult> CreateAnswer(Db.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 existing answer
[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();
}
}
}