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

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.DTOs;
using SurveyBackend.Mappers.UserDTOs;
using SurveyBackend.Mappers;
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
namespace SurveyBackend.Controllers;
@ -19,6 +19,8 @@ public class AuthController : ControllerBase
[AllowAnonymous]
[HttpPost("login")]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> LogIn([FromBody] UserLoginDto loginData)
{
var token = await _authorizationService.LogInUser(loginData.Email, loginData.Password);
@ -27,10 +29,12 @@ public class AuthController : ControllerBase
[AllowAnonymous]
[HttpPost("register")]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
{
var token = await _authorizationService.RegisterUser(
UserRegistrationMapper.UserRegistrationToModel(registerData));
AuthMapper.UserRegistrationToModel(registerData));
return Ok(new { token = token });
}
}

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();
}
}

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);
}