using DamageAssesment.Api.Questions.Db; using DamageAssesment.Api.Questions.Interfaces; using DamageAssesment.Api.Questions.Models; using DamageAssesment.Api.Questions.Providers; using Microsoft.AspNetCore.Http; 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 request for retrieving questions. /// [HttpGet("Questions")] public async Task GetQuestionsAsync() { var result = await this.questionsProvider.GetQuestionsAsync(); if (result.IsSuccess) { return Ok(result.Questions); } return NoContent(); } /// /// GET request for retrieving a question by ID. /// [HttpGet("Questions/{id}")] public async Task GetQuestionAsync(int id) { var result = await this.questionsProvider.GetQuestionAsync(id); if (result.IsSuccess) { return Ok(result.Question); } return NotFound(); } /// /// GET request for retrieving survey questions based on a survey ID. /// Uri: {Optional language}/GetSurveyQuestions/{surveyId} :Default returns question in all languages /// [HttpGet("GetSurveyQuestions/{surveyId}")] public async Task GetSurveyQuestions(int surveyId,string? Language) { if (string.IsNullOrEmpty(Language)) Language = "en"; var result = await this.questionsProvider.GetSurveyQuestionAsync(surveyId, Language); if (result.IsSuccess) { return Ok(result.SurveyQuestions); } return NotFound(); } /// /// PUT request for updating a question (multilingual). /// [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 new question (multilingual). /// [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. /// [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. /// [HttpGet("QuestionCategories")] public async Task GetQuestionCategoriesAsync() { var result = await this.questionsProvider.GetQuestionCategoriesAsync(); if (result.IsSuccess) { return Ok(result.QuestionCategories); } return NoContent(); } /// /// GET request for retrieving a question category by ID. /// [HttpGet("QuestionCategories/{id}")] public async Task GetQuestionCategoryAsync(int id) { var result = await this.questionsProvider.GetQuestionCategoryAsync(id); if (result.IsSuccess) { return Ok(result.QuestionCategory); } return NotFound(); } /// /// PUT request for updating a question category. /// [HttpPut("QuestionCategories")] 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. /// [HttpPost("QuestionCategories")] 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. /// [HttpDelete("QuestionCategories/{id}")] public async Task DeleteQuestionCategory(int id) { var result = await this.questionsProvider.DeleteQuestionCategoryAsync(id); if (result.IsSuccess) { return Ok(result.QuestionCategory); } return NotFound(); } } }