survey-webapp/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs

51 lines
No EOL
1.6 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Completion;
using SurveyBackend.Mappers;
using SurveyLib.Core.Services;
namespace SurveyBackend.Controllers;
[ApiController]
[Route("api/surveys/{surveyId:int}/completions")]
public class CompletionController : ControllerBase
{
private readonly ICompletionService _completionService;
private readonly IUserContext _userContext;
public CompletionController(ICompletionService completionService, IUserContext userContext)
{
_completionService = completionService;
_userContext = userContext;
}
[HttpGet]
[Authorize]
public async Task<IActionResult> GetCompletionsAsync(int surveyId)
{
var models = await _completionService.GetCompletionsBySurveyIdAsync(surveyId);
var result = models.Select(CompletionMapper.ModelToOutputDto);
return Ok(result);
}
[HttpGet]
[Route("/api/completions/{id:int}")]
[Authorize]
public async Task<IActionResult> GetCompletionAsync(int id)
{
var model = await _completionService.GetCompletionByIdAsync(id);
var result = CompletionMapper.ModelToOutputDto(model);
return Ok(result);
}
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> PostCompletionAsync([FromBody] CompletionCreateDto dto, [FromRoute] int surveyId)
{
var userId = _userContext.NullableUserId;
var model = CompletionMapper.CreateDtoToModel(dto, surveyId, userId);
await _completionService.AddCompletionAsync(model);
return Ok();
}
}