using DamageAssesment.Api.Questions.Interfaces;
using Microsoft.AspNetCore.Authorization;
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;
}
///
/// GET request for retrieving questions.
///
//get all questions
[Authorize(Roles = "admin,survey,user,report")]
[Route("questions")]
[Route("questions/{language:alpha}")]
[HttpGet]
public async Task 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
///
/// GET request for retrieving a question by ID.
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("questions/{id}/{language:alpha}")]
[Route("questions/{id:int}")]
[HttpGet]
public async Task 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
///
/// GET request for retrieving survey questions based on a survey ID.
/// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages
///
[Authorize(Roles = "admin,survey,user,report")]
[Route("questions/bysurvey/{surveyId:int}")]
[Route("questions/bysurvey/{surveyId:int}/{language:alpha}")]
[HttpGet]
public async Task GetSurveyQuestions(int surveyId,string? language)
{
var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, language);
if (result.IsSuccess)
{
return Ok(result.SurveyQuestions);
}
return NotFound();
}
///
/// PUT request for updating a question (multilingual).
///
[Authorize(Roles = "admin")]
[HttpPut("questions")]
public async Task 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);
}
///
/// POST request for creating a multiple question (multilingual).
///
[Authorize(Roles = "admin")]
[HttpPost("questions/multiple")]
public async Task CreateQuestions(List 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);
}
///
/// PUT request for update a multiple question (multilingual) for survey.
///
[HttpPut("questions/multiple/{surveyid}")]
public async Task CreateQuestions(int surveyid, List 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);
}
///
/// POST request for creating a new question (multilingual).
///
[Authorize(Roles = "admin")]
[HttpPost("questions")]
public async Task 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);
}
///
/// DELETE request for deleting a question based on ID.
///
[Authorize(Roles = "admin")]
[HttpDelete("questions/{id}")]
public async Task DeleteQuestion(int id)
{
var result = await this.questionsProvider.DeleteQuestionAsync(id);
if (result.IsSuccess)
{
return Ok(result.Question);
}
return NotFound();
}
///
/// GET request for retrieving question categories.
///
[Authorize(Roles = "admin,user,report")]
[HttpGet("questions/categories")]
[HttpGet("questions/categories/{language:alpha}")]
public async Task GetQuestionCategoriesAsync(string? language)
{
var result = await this.questionsProvider.GetQuestionCategoriesAsync(language);
if (result.IsSuccess)
{
return Ok(result.QuestionCategories);
}
return NoContent();
}
///
/// GET request for retrieving a question category by ID.
///
[Authorize(Roles = "admin,report")]
[HttpGet("questions/categories/{id:int}")]
[HttpGet("questions/categories/{id:int}/{language:alpha}")]
public async Task GetQuestionCategoryAsync(int id,string? language)
{
var result = await this.questionsProvider.GetQuestionCategoryAsync(id, language);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
///
/// PUT request for updating a question category.
///
[Authorize(Roles = "admin,survey,report")]
[HttpPut("questions/categories")]
public async Task 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);
}
///
/// POST request for creating a new question category.
///
[Authorize(Roles = "admin")]
[HttpPost("questions/categories")]
public async Task 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);
}
///
/// DELETE request for deleting a question category based on ID.
///
[Authorize(Roles = "admin")]
[HttpDelete("questions/categories/{id}")]
public async Task DeleteQuestionCategory(int id)
{
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
}
}