i'm sorry guys it's stupid commit...

This commit is contained in:
Вячеслав 2025-05-31 00:05:45 +05:00
parent 77cfacb95e
commit 4b4739ce89
13 changed files with 174 additions and 1 deletions

View file

@ -0,0 +1,48 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Contexts;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Completion;
using SurveyBackend.Mappers;
using SurveyLib.Core.Models;
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)
{
return NotFound();
}
[HttpGet("{id:int}")]
[Authorize]
public async Task<IActionResult> GetCompletionAsync(int id)
{
return NotFound();
}
[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();
}
}

View file

@ -0,0 +1,7 @@
namespace SurveyBackend.DTOs.Answer;
public class AnswerCreateDto
{
public int QuestionId { get; set; }
public required string AnswerText { get; set; }
}

View file

@ -0,0 +1,8 @@
namespace SurveyBackend.DTOs.Answer;
public class AnswerOutputDto
{
public int QuestionId { get; set; }
public int CompletionId { get; set; }
public required string AnswerText { get; set; }
}

View file

@ -0,0 +1,8 @@
using SurveyBackend.DTOs.Answer;
namespace SurveyBackend.DTOs.Completion;
public class CompletionCreateDto
{
public List<AnswerCreateDto> Answers { get; set; }
}

View file

@ -0,0 +1,10 @@
using SurveyBackend.DTOs.Answer;
namespace SurveyBackend.DTOs.Completion;
public class CompletionOutputDto
{
public int? CompletedBy { get; set; }
public DateTime FinishedAt { get; set; }
public List<AnswerOutputDto> Answers { get; set; }
}

View file

@ -0,0 +1,20 @@
using SurveyBackend.DTOs.Answer;
using SurveyLib.Core.Models;
namespace SurveyBackend.Mappers;
public static class AnswerMapper
{
public static Answer CreateDtoToModel(AnswerCreateDto dto) => new Answer
{
QuestionId = dto.QuestionId,
AnswerText = dto.AnswerText,
};
public static AnswerOutputDto ModelToOutputDto(Answer model) => new AnswerOutputDto
{
QuestionId = model.QuestionId,
AnswerText = model.AnswerText,
CompletionId = model.CompletionId,
};
}

View file

@ -0,0 +1,21 @@
using SurveyBackend.DTOs.Completion;
using SurveyLib.Core.Models;
namespace SurveyBackend.Mappers;
public static class CompletionMapper
{
public static Completion CreateDtoToModel(CompletionCreateDto dto, int surveyId, int? performerId) => new Completion
{
SurveyId = surveyId,
CompletedBy = performerId,
Answers = dto.Answers.Select(AnswerMapper.CreateDtoToModel).ToList(),
};
public static CompletionOutputDto ModelToOutputDto(Completion model) => new CompletionOutputDto
{
CompletedBy = model.CompletedBy,
FinishedAt = model.FinishedAt,
Answers = model.Answers.Select(AnswerMapper.ModelToOutputDto).ToList(),
};
}

View file

@ -12,6 +12,7 @@ using SurveyBackend.Infrastructure.Data;
using SurveyBackend.Infrastructure.Repositories;
using SurveyBackend.Middlewares;
using SurveyBackend.Services;
using SurveyBackend.Services.Helpers;
using SurveyBackend.Services.Services;
using SurveyLib.Core.Repositories;
using SurveyLib.Core.Services;
@ -49,10 +50,12 @@ public class Program
builder.Services.AddScoped<ISurveyRepository, SurveyRepository>();
builder.Services.AddScoped<IQuestionRepository, QuestionRepository>();
builder.Services.AddScoped<IAnswerVariantsRepository, AnswerVariantsRepository>();
builder.Services.AddScoped<ICompletionRepository, CompletionRepository>();
builder.Services.AddScoped<ISurveyService, SurveyService>();
builder.Services.AddScoped<IQuestionService, QuestionService>();
builder.Services.AddScoped<IAnswerVariantsService, AnswerVariantsService>();
builder.Services.AddScoped<ICompletionService, CompletionService>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)