From ae43e8c74cc42db75ed4756970b70b230b083704 Mon Sep 17 00:00:00 2001 From: shept Date: Tue, 20 May 2025 17:35:43 +0500 Subject: [PATCH] add AnswerVariant --- .../Controllers/AnswerVariantsController.cs | 84 +++++++++++++++++++ .../AnswerVariant/AnswerVariantCreateDto.cs | 6 ++ .../AnswerVariantOutputDto.cs} | 4 +- .../AnswerVariant/AnswerVariantUpdateDto.cs | 5 ++ .../DTOs/Question/QuestionOutputDto.cs | 4 +- .../DTOs/Survey/SurveyOutputDto.cs | 5 ++ .../Mappers/AnswerVariantMapper.cs | 28 +++++++ .../Mappers/QuestionMapper.cs | 5 +- .../SurveyBackend.API/Mappers/SurveyMapper.cs | 1 + SurveyBackend/SurveyBackend.API/Program.cs | 2 + 10 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 SurveyBackend/SurveyBackend.API/Controllers/AnswerVariantsController.cs create mode 100644 SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantCreateDto.cs rename SurveyBackend/SurveyBackend.API/DTOs/{Question/OutputAnswerVariantDTO.cs => AnswerVariant/AnswerVariantOutputDto.cs} (85%) create mode 100644 SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantUpdateDto.cs create mode 100644 SurveyBackend/SurveyBackend.API/Mappers/AnswerVariantMapper.cs diff --git a/SurveyBackend/SurveyBackend.API/Controllers/AnswerVariantsController.cs b/SurveyBackend/SurveyBackend.API/Controllers/AnswerVariantsController.cs new file mode 100644 index 0000000..fe44196 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Controllers/AnswerVariantsController.cs @@ -0,0 +1,84 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using SurveyBackend.DTOs.AnswerVariant; +using SurveyBackend.Mappers; +using SurveyLib.Core.Services; + +namespace SurveyBackend.Controllers; + +[ApiController] +[Route("api/surveys/{surveyId}/questions/{questionId}/answerVariants")] +public class AnswerVariantsController : ControllerBase +{ + private readonly IAnswerVariantsService _answerVariantsService; + + public AnswerVariantsController(IAnswerVariantsService answerVariantsService) + { + _answerVariantsService = answerVariantsService; + } + + /// + /// Получить варианты ответа на вопрос + /// + /// + /// + /// + [AllowAnonymous] + [HttpGet] + public async Task Get(int surveyId, int questionId) + { + var answerVariants = await _answerVariantsService.GetAnswerVariantsByQuestionIdAsync(questionId); + var result = answerVariants.Select(AnswerVariantMapper.ModelToOutputDto); + return Ok(result); + } + + /// + /// Добавить новый вариант ответа для вопроса + /// + /// Идентификатор опроса + /// Идентификатор вопроса + /// Объект с данными для создания варианта ответа + /// Результат операции добавления + [Authorize] + [HttpPost] + public async Task Add(int surveyId, int questionId, [FromBody] AnswerVariantCreateDto dto) + { + var model = AnswerVariantMapper.CreateDtoToModel(dto, questionId); + await _answerVariantsService.AddAnswerVariantAsync(model); + var result = AnswerVariantMapper.ModelToOutputDto(model); + return Ok(result); + } + + /// + /// Обновить вариант ответа на вопрос + /// + /// Идентификатор опроса + /// Идентификатор вопроса + /// Идентификатор варианта ответа + /// Объект с данными для обновления варианта ответа + /// Результат обновленного варианта ответа + [Authorize] + [HttpPut("{id}")] + public async Task Update(int surveyId, int questionId, int id, [FromBody] AnswerVariantUpdateDto dto) + { + var model = AnswerVariantMapper.UpdateDtoToModel(dto, questionId, id); + await _answerVariantsService.UpdateAnswerVariantAsync(model); + var result = AnswerVariantMapper.ModelToOutputDto(model); + return Ok(result); + } + + /// + /// Удалить вариант ответа на вопрос + /// + /// Идентификатор опроса + /// Идентификатор вопроса + /// Идентификатор варианта ответа + /// Результат операции удаления + [Authorize] + [HttpDelete("{id}")] + public async Task Delete(int surveyId, int questionId, int id) + { + await _answerVariantsService.DeleteAnswerVariantAsync(id); + return Ok(); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantCreateDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantCreateDto.cs new file mode 100644 index 0000000..1019ea3 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantCreateDto.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.DTOs.AnswerVariant; + +public class AnswerVariantCreateDto +{ + public required string Text { get; set; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantOutputDto.cs similarity index 85% rename from SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs rename to SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantOutputDto.cs index ff1b77b..ad9ba2f 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/Question/OutputAnswerVariantDTO.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantOutputDto.cs @@ -1,9 +1,9 @@ -namespace SurveyBackend.DTOs.Question; +namespace SurveyBackend.DTOs.AnswerVariant; /// /// Выходная схема вариантов ответа /// -public class OutputAnswerVariantDto +public class AnswerVariantOutputDto { /// /// ID варианта ответа diff --git a/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantUpdateDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantUpdateDto.cs new file mode 100644 index 0000000..5a242a6 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/AnswerVariant/AnswerVariantUpdateDto.cs @@ -0,0 +1,5 @@ +namespace SurveyBackend.DTOs.AnswerVariant; + +public class AnswerVariantUpdateDto : AnswerVariantCreateDto +{ +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionOutputDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionOutputDto.cs index 96eac88..bb2442b 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionOutputDto.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/Question/QuestionOutputDto.cs @@ -1,3 +1,5 @@ +using SurveyBackend.DTOs.AnswerVariant; + namespace SurveyBackend.DTOs.Question; /// @@ -28,5 +30,5 @@ public class QuestionOutputDto /// /// Варианты ответа /// - public List? AnswerVariants { get; set; } + public List? AnswerVariants { get; set; } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/Survey/SurveyOutputDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/Survey/SurveyOutputDto.cs index 6598707..61498c2 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/Survey/SurveyOutputDto.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/Survey/SurveyOutputDto.cs @@ -9,16 +9,21 @@ public class SurveyOutputDto /// ID опроса /// public required int Id { get; set; } + /// /// Название опроса /// public required string Title { get; set; } + /// /// Описание опроса /// public required string Description { get; set; } + /// /// Создатель опроса (опционально) /// public int? CreatedBy { get; set; } + + public required DateTime CreatedAt { get; set; } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/AnswerVariantMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/AnswerVariantMapper.cs new file mode 100644 index 0000000..d759415 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Mappers/AnswerVariantMapper.cs @@ -0,0 +1,28 @@ +using SurveyBackend.DTOs.AnswerVariant; +using SurveyLib.Core.Models; + +namespace SurveyBackend.Mappers; + +public static class AnswerVariantMapper +{ + public static AnswerVariant CreateDtoToModel(AnswerVariantCreateDto dto, int questionId) => new AnswerVariant + { + QuestionId = questionId, + Text = dto.Text + }; + + public static AnswerVariant UpdateDtoToModel(AnswerVariantUpdateDto dto, int answerVariantId, int questionId) => + new AnswerVariant + { + QuestionId = questionId, + Id = answerVariantId, + Text = dto.Text + }; + + public static AnswerVariantOutputDto ModelToOutputDto(AnswerVariant model) => new AnswerVariantOutputDto + { + Id = model.Id, + QuestionId = model.QuestionId, + Text = model.Text + }; +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs index 044c052..6d50d50 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs @@ -1,3 +1,4 @@ +using SurveyBackend.DTOs.AnswerVariant; using SurveyBackend.DTOs.Question; using SurveyBackend.Services.Exceptions; using SurveyLib.Core.Models; @@ -88,9 +89,9 @@ public static class QuestionMapper }; } - private static List AnswerVariantsToDto(IEnumerable answerVariants) + private static List AnswerVariantsToDto(IEnumerable answerVariants) { - return answerVariants.Select(av => new OutputAnswerVariantDto + return answerVariants.Select(av => new AnswerVariantOutputDto { Id = av.Id, QuestionId = av.QuestionId, diff --git a/SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs index be88b9c..a39ecf7 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/SurveyMapper.cs @@ -45,6 +45,7 @@ public static class SurveyMapper Title = survey.Title, Description = survey.Description, CreatedBy = survey.CreatedBy, + CreatedAt = survey.CreatedAt, }; } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Program.cs b/SurveyBackend/SurveyBackend.API/Program.cs index da19cc8..829f13f 100644 --- a/SurveyBackend/SurveyBackend.API/Program.cs +++ b/SurveyBackend/SurveyBackend.API/Program.cs @@ -48,9 +48,11 @@ public class Program builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)