diff --git a/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs new file mode 100644 index 0000000..72a8fda --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs @@ -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 GetCompletionsAsync(int surveyId) + { + return NotFound(); + } + + [HttpGet("{id:int}")] + [Authorize] + public async Task GetCompletionAsync(int id) + { + return NotFound(); + } + + [HttpPost] + [AllowAnonymous] + public async Task PostCompletionAsync([FromBody] CompletionCreateDto dto, [FromRoute] int surveyId) + { + var userId = _userContext.NullableUserId; + var model = CompletionMapper.CreateDtoToModel(dto, surveyId, userId); + await _completionService.AddCompletionAsync(model); + return Ok(); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerCreateDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerCreateDto.cs new file mode 100644 index 0000000..f2040f1 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerCreateDto.cs @@ -0,0 +1,7 @@ +namespace SurveyBackend.DTOs.Answer; + +public class AnswerCreateDto +{ + public int QuestionId { get; set; } + public required string AnswerText { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerOutputDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerOutputDto.cs new file mode 100644 index 0000000..a3dec70 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Answer/AnswerOutputDto.cs @@ -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; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionCreateDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionCreateDto.cs new file mode 100644 index 0000000..daac77f --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionCreateDto.cs @@ -0,0 +1,8 @@ +using SurveyBackend.DTOs.Answer; + +namespace SurveyBackend.DTOs.Completion; + +public class CompletionCreateDto +{ + public List Answers { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionOutputDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionOutputDto.cs new file mode 100644 index 0000000..0514242 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/Completion/CompletionOutputDto.cs @@ -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 Answers { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/AnswerMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/AnswerMapper.cs new file mode 100644 index 0000000..c5fd2bd --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Mappers/AnswerMapper.cs @@ -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, + }; +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/CompletionMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/CompletionMapper.cs new file mode 100644 index 0000000..9596947 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Mappers/CompletionMapper.cs @@ -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(), + }; +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Program.cs b/SurveyBackend/SurveyBackend.API/Program.cs index 829f13f..54545b3 100644 --- a/SurveyBackend/SurveyBackend.API/Program.cs +++ b/SurveyBackend/SurveyBackend.API/Program.cs @@ -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(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) diff --git a/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs b/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs index bd46cba..9808150 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs @@ -15,26 +15,31 @@ public class AnswerVariantsService : IAnswerVariantsService public async Task AddAnswerVariantAsync(AnswerVariant answerVariant) { + // TODO: проверка существования такого вопроса await _answerVariantsRepository.AddAsync(answerVariant); } public async Task UpdateAnswerVariantAsync(AnswerVariant answerVariant) { + // TODO: опять проверка существования, но еще и варианта await _answerVariantsRepository.UpdateAsync(answerVariant); } public async Task DeleteAnswerVariantAsync(int id) { + // TODO: проверка существования варианта await _answerVariantsRepository.DeleteAsync(id); } public async Task GetAnswerVariantByIdAsync(int id) { + // TODO: проверка существования варианта return await _answerVariantsRepository.GetByIdAsync(id); } public async Task> GetAnswerVariantsByQuestionIdAsync(int questionId) { + // TODO: проверка существования вопроса return await _answerVariantsRepository.GetAnswerVariantsByQuestionIdAsync(questionId); } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/CompletionService.cs b/SurveyBackend/SurveyBackend.Services/Services/CompletionService.cs new file mode 100644 index 0000000..8d2e633 --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Services/CompletionService.cs @@ -0,0 +1,40 @@ +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Core.Services; + +namespace SurveyBackend.Services.Services; + +public class CompletionService : ICompletionService +{ + private readonly ICompletionRepository _completionRepository; + + public CompletionService(ICompletionRepository completionRepository) + { + _completionRepository = completionRepository; + } + + public async Task AddCompletionAsync(Completion completion) + { + // TODO: проверить существование опроса + await _completionRepository.AddAsync(completion); + } + + public async Task UpdateCompletionAsync(Completion completion) + { + // TODO: лол а что вообще значит ОбновитьВыполнение, надо выпилить из SurveyLib + await _completionRepository.UpdateAsync(completion); + } + + public async Task DeleteCompletionAsync(int id) + { + // TODO: да и удалять их как-то бессмысленно + await _completionRepository.DeleteAsync(id); + } + + public async Task> GetCompletionsBySurveyIdAsync(int surveyId) + { + // TODO: проверить существование опроса + // TODO: проверить что запрашивает создатель (хз как) + return await _completionRepository.GetCompletionsBySurveyIdAsync(surveyId); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs index a726427..d364fdb 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs @@ -22,11 +22,13 @@ public class QuestionService : IQuestionService public async Task AddQuestionAsync(QuestionBase question) { + // TODO: проверить существование опроса await _questionRepository.AddAsync(question); } public async Task UpdateQuestionAsync(QuestionBase question) { + // TODO: проверить существование вопроса await _questionRepository.UpdateAsync(question); } diff --git a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs index b39c371..5190a0c 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs @@ -64,6 +64,7 @@ public class SurveyService : ISurveyService public async Task> GetSurveysByUserIdAsync(int userId) { + // TODO: проверить существование юзера return await _surveyRepository.GetSurveysByUserIdAsync(userId); } } \ No newline at end of file diff --git a/SurveyLib b/SurveyLib index 7d47ab9..14d0f36 160000 --- a/SurveyLib +++ b/SurveyLib @@ -1 +1 @@ -Subproject commit 7d47ab9e60645032bc41daee5535aba2b7eeebdf +Subproject commit 14d0f36cb801c707104af14d6cebe6da866d848a