2023-08-25 07:55:11 -05:00
|
|
|
|
using DamageAssesment.Api.Questions.Interfaces;
|
2023-08-15 22:52:30 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
namespace DamageAssesment.Api.Questions.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class QuestionsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly IQuestionsProvider questionsProvider;
|
|
|
|
|
|
|
|
|
|
public QuestionsController(IQuestionsProvider questionsProvider)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
this.questionsProvider = questionsProvider;
|
|
|
|
|
|
|
|
|
|
}
|
2023-08-25 10:36:31 -05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving questions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
// get all questions
|
2023-08-25 07:55:11 -05:00
|
|
|
|
[Route("Questions")]
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[Route("Questions/{language:alpha}")]
|
2023-08-25 07:55:11 -05:00
|
|
|
|
[HttpGet]
|
2023-09-08 14:40:06 -05:00
|
|
|
|
public async Task<IActionResult> GetQuestionsAsync(string? language)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-08 14:40:06 -05:00
|
|
|
|
var result = await this.questionsProvider.GetQuestionsAsync(language);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Questions);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-08-25 10:36:31 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
//Get questions based on question id
|
2023-08-25 10:36:31 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving a question by ID.
|
|
|
|
|
/// </summary>
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[Route("Questions/{id}/{language:alpha}")]
|
|
|
|
|
[Route("Questions/{id:int}")]
|
2023-08-25 07:55:11 -05:00
|
|
|
|
[HttpGet]
|
2023-09-13 00:28:24 -05:00
|
|
|
|
public async Task<IActionResult> GetQuestionByIdAsync(int id, string? language)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-08 14:40:06 -05:00
|
|
|
|
var result = await this.questionsProvider.GetQuestionAsync(id, language);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Question);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-25 10:36:31 -05:00
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
//get all questions based on survey id
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving survey questions based on a survey ID.
|
|
|
|
|
/// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages
|
|
|
|
|
/// </summary>
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[Route("Questions/BySurvey/{surveyId:int}")]
|
|
|
|
|
[Route("Questions/BySurvey/{surveyId:int}/{language:alpha}")]
|
2023-08-25 10:36:31 -05:00
|
|
|
|
[HttpGet]
|
2023-09-08 14:40:06 -05:00
|
|
|
|
public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? language)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-08 14:40:06 -05:00
|
|
|
|
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, language);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.SurveyQuestions);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// PUT request for updating a question (multilingual).
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpPut("Questions")]
|
|
|
|
|
public async Task<IActionResult> UpdateQuestion(Models.Question question)
|
|
|
|
|
{
|
|
|
|
|
if (question != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.UpdateQuestionAsync(question);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Question);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage == "Not Found")
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// POST request for creating a new question (multilingual).
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpPost("Questions")]
|
|
|
|
|
public async Task<IActionResult> CreateQuestion(Models.Question question)
|
|
|
|
|
{
|
|
|
|
|
if (question != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.PostQuestionAsync(question);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Question);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = question.Id }, question);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// DELETE request for deleting a question based on ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-08-15 22:52:30 -05:00
|
|
|
|
[HttpDelete("Questions/{id}")]
|
|
|
|
|
public async Task<IActionResult> DeleteQuestion(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.DeleteQuestionAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.Question);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving question categories.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[HttpGet("Questions/Categories")]
|
|
|
|
|
[HttpGet("Questions/Categories/{language:alpha}")]
|
|
|
|
|
public async Task<IActionResult> GetQuestionCategoriesAsync(string? language)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-08 14:40:06 -05:00
|
|
|
|
var result = await this.questionsProvider.GetQuestionCategoriesAsync(language);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.QuestionCategories);
|
|
|
|
|
}
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GET request for retrieving a question category by ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[HttpGet("Questions/Categories/{id:int}")]
|
|
|
|
|
[HttpGet("Questions/Categories/{id:int}/{language:alpha}")]
|
|
|
|
|
public async Task<IActionResult> GetQuestionCategoryAsync(int id,string? language)
|
2023-08-15 22:52:30 -05:00
|
|
|
|
{
|
2023-09-08 14:40:06 -05:00
|
|
|
|
var result = await this.questionsProvider.GetQuestionCategoryAsync(id, language);
|
2023-08-15 22:52:30 -05:00
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.QuestionCategory);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// PUT request for updating a question category.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[HttpPut("Questions/Categories")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
public async Task<IActionResult> UpdateQuestionCategory(Models.QuestionCategory questionCategory)
|
|
|
|
|
{
|
|
|
|
|
if (questionCategory != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.UpdateQuestionCategoryAsync(questionCategory);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.QuestionCategory);
|
|
|
|
|
}
|
|
|
|
|
if (result.ErrorMessage == "Not Found")
|
|
|
|
|
return NotFound(result.ErrorMessage);
|
|
|
|
|
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// POST request for creating a new question category.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[HttpPost("Questions/Categories")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
public async Task<IActionResult> CreateQuestionCategory(Models.QuestionCategory questionCategory)
|
|
|
|
|
{
|
|
|
|
|
if (questionCategory != null)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.PostQuestionCategoryAsync(questionCategory);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.QuestionCategory);
|
|
|
|
|
}
|
|
|
|
|
return BadRequest(result.ErrorMessage);
|
|
|
|
|
}
|
|
|
|
|
return CreatedAtRoute("DefaultApi", new { id = questionCategory.Id }, questionCategory);
|
|
|
|
|
}
|
2023-08-24 20:25:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// DELETE request for deleting a question category based on ID.
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
2023-09-08 14:40:06 -05:00
|
|
|
|
[HttpDelete("Questions/Categories/{id}")]
|
2023-08-15 22:52:30 -05:00
|
|
|
|
public async Task<IActionResult> DeleteQuestionCategory(int id)
|
|
|
|
|
{
|
|
|
|
|
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);
|
|
|
|
|
if (result.IsSuccess)
|
|
|
|
|
{
|
|
|
|
|
return Ok(result.QuestionCategory);
|
|
|
|
|
}
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|