life quality changes

- refactored SurveyController.cs
- added ProducesResponseType to every endpoint in every controller
- remade mappers
This commit is contained in:
Вячеслав 2025-04-20 23:12:20 +05:00
parent 9e50b97bc9
commit 6692a91821
11 changed files with 146 additions and 93 deletions

View file

@ -2,13 +2,13 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Question;
using SurveyBackend.Mappers.QuestionDTOs;
using SurveyBackend.Mappers;
using SurveyLib.Core.Services;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/questions")]
[Route("surveys/{surveyId}/questions")]
public class QuestionController : ControllerBase
{
private readonly IQuestionService _questionService;
@ -19,20 +19,24 @@ public class QuestionController : ControllerBase
}
[AllowAnonymous]
[HttpGet("by_survey/{id}")]
public async Task<IActionResult> GetBySurveyId(int id)
[HttpGet]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(List<OutputQuestionDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetBySurveyId([FromRoute] int surveyId)
{
var questions = await _questionService.GetQuestionsBySurveyIdAsync(id);
var result = questions.Select(QuestionOutputMapper.ModelToQuestionDTO).ToList();
var questions = await _questionService.GetQuestionsBySurveyIdAsync(surveyId);
var result = questions.Select(QuestionMapper.ModelToQuestionDto).ToList();
return Ok(result);
}
[Authorize]
[HttpPost]
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto)
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task<IActionResult> AddQuestion(CreateQuestionDto dto, [FromRoute] int surveyId)
{
var model = QuestionCreationMapper.QuestionCreationToModel(dto);
var model = QuestionMapper.QuestionCreationToModel(dto, surveyId);
await _questionService.AddQuestionAsync(model);
return Ok();
return Created();
}
}