DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs
2023-12-07 09:51:49 -05:00

252 lines
8.9 KiB
C#

using DamageAssesment.Api.Questions.Interfaces;
using DamageAssesment.Api.Questions.Models;
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;
}
/// <summary>
/// GET request for retrieving questions.
/// </summary>
// get all questions
[Route("questions")]
[Route("questions/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetQuestionsAsync(string? language)
{
var result = await this.questionsProvider.GetQuestionsAsync(language);
if (result.IsSuccess)
{
return Ok(result.Questions);
}
return NoContent();
}
//Get questions based on question id
/// <summary>
/// GET request for retrieving a question by ID.
/// </summary>
[Route("questions/{id}/{language:alpha}")]
[Route("questions/{id:int}")]
[HttpGet]
public async Task<IActionResult> GetQuestionByIdAsync(int id, string? language)
{
var result = await this.questionsProvider.GetQuestionAsync(id, language);
if (result.IsSuccess)
{
return Ok(result.Question);
}
return NotFound();
}
//get all questions based on survey id
/// <summary>
/// GET request for retrieving survey questions based on a survey ID.
/// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages
/// </summary>
[Route("questions/bysurvey/{surveyId:int}")]
[Route("questions/bysurvey/{surveyId:int}/{language:alpha}")]
[HttpGet]
public async Task<IActionResult> GetSurveyQuestions(int surveyId,string? language)
{
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, language);
if (result.IsSuccess)
{
return Ok(result.SurveyQuestions);
}
return NotFound();
}
/// <summary>
/// PUT request for updating a question (multilingual).
/// </summary>
[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);
}
/// <summary>
/// POST request for creating a multiple question (multilingual).
/// </summary>
[HttpPost("questions/multiple")]
public async Task<IActionResult> CreateQuestions(List<Models.Question> questions)
{
if (questions != null)
{
var result = await this.questionsProvider.PostQuestionsAsync(questions);
if (result.IsSuccess)
{
return Ok(result.Question);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi",questions);
}
/// <summary>
/// PUT request for update a multiple question (multilingual) for survey.
/// </summary>
[HttpPut("questions/multiple/{surveyid}")]
public async Task<IActionResult> CreateQuestions(int surveyid, List<Models.Question> questions)
{
if (questions != null)
{
var result = await this.questionsProvider.PutQuestionsAsync(surveyid,questions);
if (result.IsSuccess)
{
return Ok(result.Question);
}
if (result.ErrorMessage == "Not Found")
return NotFound(result.ErrorMessage);
return BadRequest(result.ErrorMessage);
}
return CreatedAtRoute("DefaultApi", questions);
}
/// <summary>
/// POST request for creating a new question (multilingual).
/// </summary>
[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);
}
/// <summary>
/// DELETE request for deleting a question based on ID.
/// </summary>
[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();
}
/// <summary>
/// GET request for retrieving question categories.
/// </summary>
[HttpGet("questions/categories")]
[HttpGet("questions/categories/{language:alpha}")]
public async Task<IActionResult> GetQuestionCategoriesAsync(string? language)
{
var result = await this.questionsProvider.GetQuestionCategoriesAsync(language);
if (result.IsSuccess)
{
return Ok(result.QuestionCategories);
}
return NoContent();
}
/// <summary>
/// GET request for retrieving a question category by ID.
/// </summary>
[HttpGet("questions/categories/{id:int}")]
[HttpGet("questions/categories/{id:int}/{language:alpha}")]
public async Task<IActionResult> GetQuestionCategoryAsync(int id,string? language)
{
var result = await this.questionsProvider.GetQuestionCategoryAsync(id, language);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
/// <summary>
/// PUT request for updating a question category.
/// </summary>
[HttpPut("questions/categories")]
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);
}
/// <summary>
/// POST request for creating a new question category.
/// </summary>
[HttpPost("questions/categories")]
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);
}
/// <summary>
/// DELETE request for deleting a question category based on ID.
/// </summary>
[HttpDelete("questions/categories/{id}")]
public async Task<IActionResult> DeleteQuestionCategory(int id)
{
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
}
}