using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SurveyBackend.Core.Contexts; using SurveyBackend.DTOs.Question; using SurveyBackend.Mappers; using SurveyLib.Core.Services; namespace SurveyBackend.Controllers; /// [ApiController] [Route("api/surveys/{surveyId}/questions")] public class QuestionController : ControllerBase { private readonly IQuestionService _questionService; /// public QuestionController(IQuestionService questionService, IUserContext userContext) { _questionService = questionService; } /// /// Возвращает список вопросов из опроса по его ID /// /// Получение вопросов по ID опроса. В случае отсутствия опроса с таким идентификатором выкидывает 404 /// /// [AllowAnonymous] [HttpGet] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] public async Task GetBySurveyId([FromRoute] int surveyId) { var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId); var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList(); return Ok(result); } /// /// Добавить вопрос к опросу /// /// К опросу с указанным ID добавляет вопрос. Если я правильно написал, при отсутствии такого опроса кинет 404 /// /// /// [Authorize] [HttpPost] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status200OK)] public async Task AddQuestion(QuestionCreateDto dto, [FromRoute] int surveyId) { var model = QuestionMapper.QuestionCreationToModel(dto, surveyId); await _questionService.AddQuestionAsync(model); var result = QuestionMapper.ModelToQuestionDto(model); return Ok(result); } /// /// Обновляет вопрос целиком /// /// /// /// /// [Authorize] [HttpPut] [Route("/api/questions/{id:int}")] public async Task UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id) { var question = QuestionMapper.QuestionUpdateToModel(dto, id); await _questionService.UpdateQuestionAsync(question); var result = QuestionMapper.ModelToQuestionDto(question); return Ok(result); } /// /// Удаляет вопрос по его ID /// /// /// /// [Authorize] [HttpDelete] [Route("/api/questions/{id:int}")] public async Task DeleteQuestion([FromRoute] int id) { await _questionService.DeleteQuestionAsync(id); return Ok(); } }