survey-webapp/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs
2025-05-13 18:08:51 +05:00

112 lines
No EOL
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
/// <inheritdoc />
[ApiController]
[Route("api/surveys")]
public class SurveyController : ControllerBase
{
private readonly ISurveyService _surveyService;
private readonly IUserContext _userContext;
/// <inheritdoc />
public SurveyController(ISurveyService surveyService, IUserContext userContext)
{
_surveyService = surveyService;
_userContext = userContext;
}
/// <summary>
/// Получить ВСЕ опросы
/// </summary>
/// <remarks>Возвращает массив вообще всех опросов</remarks>
/// <returns></returns>
[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);
}
/// <summary>
/// Получить опрос по ID
/// </summary>
/// <remarks>А что тут говорить то</remarks>
/// <param name="id"></param>
/// <returns></returns>
[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);
}
/// <summary>
/// Добавить новый опрос
/// </summary>
/// <remarks></remarks>
/// <param name="dto"></param>
/// <returns></returns>
[Authorize]
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<IActionResult> Post([FromBody] CreateSurveyDto dto)
{
var userId = _userContext.UserId;
var survey = SurveyMapper.CreateDtoToModel(dto, userId);
await _surveyService.AddSurveyAsync(survey);
var result = SurveyMapper.ModelToOutputDto(survey);
return Ok(result);
}
/// <summary>
/// Удалить опрос по ID
/// </summary>
/// <remarks>Опрос должен быть создан тобой чтоб его удалить</remarks>
/// <param name="id"></param>
/// <returns></returns>
[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();
}
/// <summary>
/// Получить МОИ опроса
/// </summary>
/// <remarks>Возвращает только опросы созданные нынешним юзером</remarks>
/// <returns></returns>
[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);
}
}