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

@ -3,13 +3,15 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Survey;
using SurveyBackend.Mappers;
using SurveyBackend.Services.Exceptions;
using SurveyLib.Core.Models;
using SurveyLib.Core.Services;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/surveys")]
[Route("surveys")]
public class SurveyController : ControllerBase
{
private readonly ISurveyService _surveyService;
@ -23,38 +25,42 @@ public class SurveyController : ControllerBase
[AllowAnonymous]
[HttpGet]
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> Get()
{
var result = await _surveyService.GetSurveysAsync();
var surveys = await _surveyService.GetSurveysAsync();
var result = surveys.Select(SurveyMapper.ModelToOutputDto);
return Ok(result);
}
[AllowAnonymous]
[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(OutputSurveyDto), StatusCodes.Status200OK)]
public async Task<IActionResult> Get(int id)
{
var result = await _surveyService.GetSurveyAsync(id);
return result is not null ? Ok(result) : NotFound();
var survey = await _surveyService.GetSurveyAsync(id);
var result = SurveyMapper.ModelToOutputDto(survey);
return Ok(result);
}
[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
{
var userId = _userContext.UserId;
var survey = new Survey
{
Title = dto.Title,
Description = dto.Description,
CreatedBy = userId,
};
var survey = SurveyMapper.CreateDtoToModel(dto, userId);
await _surveyService.AddSurveyAsync(survey);
return Ok();
return Created();
}
[Authorize]
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> Delete(int id)
{
await _surveyService.DeleteSurveyAsync(id);
@ -62,10 +68,13 @@ public class SurveyController : ControllerBase
}
[Authorize]
[HttpGet("my_surveys")]
[HttpGet("my")]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetMySurveys()
{
var userId = _userContext.UserId;
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
return Ok(result);
}