From 637e6c98242acb011eeb864597e295c01ec6248b Mon Sep 17 00:00:00 2001 From: shept Date: Sat, 31 May 2025 01:03:12 +0500 Subject: [PATCH] Update SurveyLib and start refactoring endpoint paths --- .../Controllers/CompletionController.cs | 5 ++--- .../Controllers/QuestionController.cs | 13 +++++++------ .../SurveyBackend.API/Mappers/QuestionMapper.cs | 8 ++++---- .../Services/QuestionService.cs | 9 ++++++++- SurveyLib | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs index cae1bc9..162fda4 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/CompletionController.cs @@ -1,10 +1,8 @@ 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; @@ -31,7 +29,8 @@ public class CompletionController : ControllerBase return Ok(result); } - [HttpGet("{id:int}")] + [HttpGet] + [Route("/api/completions/{id:int}")] [Authorize] public async Task GetCompletionAsync(int id) { diff --git a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs index ee94aab..9f1b5dc 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/QuestionController.cs @@ -64,11 +64,11 @@ public class QuestionController : ControllerBase /// /// [Authorize] - [HttpPut("{id}")] - public async Task UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id, - [FromRoute] int surveyId) + [HttpPut] + [Route("/api/questions/{id:int}")] + public async Task UpdateQuestion([FromBody] QuestionUpdateDto dto, [FromRoute] int id) { - var question = QuestionMapper.QuestionUpdateToModel(dto, surveyId, id); + var question = QuestionMapper.QuestionUpdateToModel(dto, id); await _questionService.UpdateQuestionAsync(question); var result = QuestionMapper.ModelToQuestionDto(question); return Ok(result); @@ -81,8 +81,9 @@ public class QuestionController : ControllerBase /// /// [Authorize] - [HttpDelete("{id}")] - public async Task DeleteQuestion([FromRoute] int id, [FromRoute] int surveyId) + [HttpDelete] + [Route("/api/questions/{id:int}")] + public async Task DeleteQuestion([FromRoute] int id) { await _questionService.DeleteQuestionAsync(id); return Ok(); diff --git a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs index 6d50d50..53da810 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/QuestionMapper.cs @@ -61,7 +61,7 @@ public static class QuestionMapper }; } - public static QuestionBase QuestionUpdateToModel(QuestionCreateDto dto, int surveyId, int questionId) + public static QuestionBase QuestionUpdateToModel(QuestionCreateDto dto, int questionId) { return dto.QuestionType.ToLower() switch { @@ -69,20 +69,20 @@ public static class QuestionMapper { Id = questionId, Title = dto.Title, - SurveyId = surveyId, + SurveyId = 0, // bad logic, need to do something }, "singleanswerquestion" => new SingleAnswerQuestion { Id = questionId, Title = dto.Title, - SurveyId = surveyId, + SurveyId = 0, AnswerVariants = [], }, "multipleanswerquestion" => new MultipleAnswerQuestion { Id = questionId, Title = dto.Title, - SurveyId = surveyId, + SurveyId = 0, AnswerVariants = [] }, _ => throw new BadRequestException("Unknown question type") diff --git a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs index d364fdb..003028c 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs @@ -28,7 +28,14 @@ public class QuestionService : IQuestionService public async Task UpdateQuestionAsync(QuestionBase question) { - // TODO: проверить существование вопроса + var questionBase = await _questionRepository.GetByIdAsNoTrackingAsync(question.Id); + if (questionBase is null) + { + throw new NotFoundException("Question not found"); + } + + question.SurveyId = questionBase.SurveyId; + await _questionRepository.UpdateAsync(question); } diff --git a/SurveyLib b/SurveyLib index 6349edf..f02a36d 160000 --- a/SurveyLib +++ b/SurveyLib @@ -1 +1 @@ -Subproject commit 6349edf237839ed67b596e25b7c11510e97df325 +Subproject commit f02a36dd85184c31a58e1d84081220a08b1c5d99