- refactored SurveyController.cs - added ProducesResponseType to every endpoint in every controller - remade mappers
81 lines
No EOL
2.5 KiB
C#
81 lines
No EOL
2.5 KiB
C#
using System.Security.Claims;
|
|
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("surveys")]
|
|
public class SurveyController : ControllerBase
|
|
{
|
|
private readonly ISurveyService _surveyService;
|
|
private readonly IUserContext _userContext;
|
|
|
|
public SurveyController(ISurveyService surveyService, IUserContext userContext)
|
|
{
|
|
_surveyService = surveyService;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(List<OutputSurveyDto>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
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 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 = SurveyMapper.CreateDtoToModel(dto, userId);
|
|
await _surveyService.AddSurveyAsync(survey);
|
|
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);
|
|
return Ok();
|
|
}
|
|
|
|
[Authorize]
|
|
[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);
|
|
}
|
|
} |