131 lines
4.0 KiB
C#
131 lines
4.0 KiB
C#
using DamageAssesment.Api.Answers.Interfaces;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace DamageAssesment.Api.Answers.Controllers
|
|
{
|
|
[ApiController]
|
|
public class AnswersController: ControllerBase
|
|
{
|
|
private IAnswersProvider answerProvider;
|
|
|
|
public AnswersController(IAnswersProvider answersProvider) {
|
|
this.answerProvider=answersProvider;
|
|
}
|
|
/// <summary>
|
|
/// Get all answers
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("Answers")]
|
|
public async Task<ActionResult> GetAnswersAsync() {
|
|
|
|
var result = await answerProvider.GetAnswersAsync();
|
|
if(result.IsSuccess)
|
|
{
|
|
return Ok(result.Answers);
|
|
}
|
|
return NoContent();
|
|
|
|
}
|
|
/// <summary>
|
|
/// Get an answer based on answerId.
|
|
/// </summary>
|
|
|
|
[Authorize(Roles = "admin")]
|
|
[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();
|
|
|
|
}
|
|
/// <summary>
|
|
/// Get all answers based on responseId.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("Answers/ByResponse/{responseid}")]
|
|
public async Task<IActionResult> GetAnswersByResponseId(int responseid)
|
|
{
|
|
var result = await this.answerProvider.GetAnswersAsync(responseid);
|
|
if(result.IsSuccess)
|
|
{
|
|
return Ok(result.Answers);
|
|
}
|
|
return NoContent();
|
|
}
|
|
/// <summary>
|
|
/// Get all answers based on questionId.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpGet("Answers/ByQuestion/{questionid}")]
|
|
public async Task<IActionResult> AnswersByQuestionId(int questionid)
|
|
{
|
|
var result = await this.answerProvider.GetAnswersByQuestionAsync(questionid);
|
|
if (result.IsSuccess)
|
|
{
|
|
return Ok(result.Answers);
|
|
}
|
|
return NotFound();
|
|
}
|
|
/// <summary>
|
|
/// Update an existing answer.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpPut("Answers")]
|
|
public async Task<IActionResult> 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();
|
|
}
|
|
/// <summary>
|
|
/// Save a new answer.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[HttpPost("Answers")]
|
|
public async Task<IActionResult> 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);
|
|
}
|
|
/// <summary>
|
|
/// Delete an existing answer.
|
|
/// </summary>
|
|
[Authorize(Roles = "admin")]
|
|
[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();
|
|
}
|
|
|
|
}
|
|
}
|