survey-webapp/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs
2025-04-27 15:58:51 +05:00

57 lines
No EOL
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/// <inheritdoc />
[ApiController]
[Route("api/surveys/{surveyId}/questions")]
public class QuestionController : ControllerBase
{
private readonly IQuestionService _questionService;
/// <inheritdoc />
public QuestionController(IQuestionService questionService, IUserContext userContext)
{
_questionService = questionService;
}
/// <summary>
/// Возвращает список вопросов из опроса по его ID
/// </summary>
/// <remarks>Получение вопросов по ID опроса. В случае отсутствия опроса с таким идентификатором выкидывает 404</remarks>
/// <param name="surveyId"></param>
/// <returns></returns>
[AllowAnonymous]
[HttpGet]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(List<OutputQuestionDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetBySurveyId([FromRoute] int surveyId)
{
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList();
return Ok(result);
}
/// <summary>
/// Добавить вопрос к опросу
/// </summary>
/// <remarks>К опросу с указанным ID добавляет вопрос. Если я правильно написал, при отсутствии такого опроса кинет 404</remarks>
/// <param name="dto"></param>
/// <param name="surveyId"></param>
/// <returns></returns>
[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto, [FromRoute] int surveyId)
{
var model = QuestionMapper.QuestionCreationToModel(dto, surveyId);
await _questionService.AddQuestionAsync(model);
return Created();
}
}