77 lines
No EOL
2.9 KiB
C#
77 lines
No EOL
2.9 KiB
C#
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<QuestionOutputDto>), 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.Status200OK)]
|
||
public async Task<IActionResult> 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("{id}")]
|
||
public async Task<IActionResult> UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id,
|
||
[FromRoute] int surveyId)
|
||
{
|
||
var question = QuestionMapper.QuestionUpdateToModel(dto, surveyId, id);
|
||
await _questionService.UpdateQuestionAsync(question);
|
||
var result = QuestionMapper.ModelToQuestionDto(question);
|
||
return Ok(result);
|
||
}
|
||
|
||
[Authorize]
|
||
[HttpDelete("{id}")]
|
||
public async Task<IActionResult> DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId)
|
||
{
|
||
await _questionService.DeleteQuestionAsync(id);
|
||
return Ok();
|
||
}
|
||
} |