DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.Questions/Controllers/QuestionsController.cs

173 lines
6.0 KiB
C#

using DamageAssesment.Api.Questions.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.Questions.Controllers
{
[Route("api")]
[ApiController]
public class QuestionsController : ControllerBase
{
private readonly IQuestionsProvider questionsProvider;
public QuestionsController(IQuestionsProvider questionsProvider)
{
this.questionsProvider = questionsProvider;
}
// get all questions
[Route("{Language}/Questions")]
[Route("Questions")]
[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
[Route("{Language}/Questions/{id}")]
[Route("Questions/{id}")]
[HttpGet]
public async Task<IActionResult> GetQuestionAsync(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
[Route("{Language}/GetSurveyQuestions/{surveyId}")]
[Route("GetSurveyQuestions/{surveyId}")]
[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();
}
//update existing question
[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);
}
//save new question
[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);
}
// delete existing question
[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();
}
// get all questions
[HttpGet("QuestionCategories")]
public async Task<IActionResult> GetQuestionCategoriesAsync()
{
var result = await this.questionsProvider.GetQuestionCategoriesAsync();
if (result.IsSuccess)
{
return Ok(result.QuestionCategories);
}
return NoContent();
}
//Get questions based on question id
[HttpGet("QuestionCategories/{id}")]
public async Task<IActionResult> GetQuestionCategoryAsync(int id)
{
var result = await this.questionsProvider.GetQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
//update existing question
[HttpPut("QuestionCategories")]
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);
}
//save new question
[HttpPost("QuestionCategories")]
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);
}
// delete existing question
[HttpDelete("QuestionCategories/{id}")]
public async Task<IActionResult> DeleteQuestionCategory(int id)
{
var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id);
if (result.IsSuccess)
{
return Ok(result.QuestionCategory);
}
return NotFound();
}
}
}