From a5aad0904635a6e2fc9c737ce0b0b7d5e11234e6 Mon Sep 17 00:00:00 2001 From: shept Date: Sat, 31 May 2025 01:41:54 +0500 Subject: [PATCH] added existance check (not sure if it's working) --- .../Services/AnswerService.cs | 20 +++++++- .../Services/AnswerVariantsService.cs | 46 +++++++++++++++---- .../Services/CompletionService.cs | 18 ++++++-- .../Services/QuestionService.cs | 7 ++- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/SurveyBackend/SurveyBackend.Services/Services/AnswerService.cs b/SurveyBackend/SurveyBackend.Services/Services/AnswerService.cs index 758a513..e3fba4e 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/AnswerService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/AnswerService.cs @@ -1,3 +1,4 @@ +using SurveyBackend.Services.Exceptions; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Core.Services; @@ -7,10 +8,15 @@ namespace SurveyBackend.Services.Services; public class AnswerService : IAnswerService { private readonly IAnswerRepository _answerRepository; + private readonly IQuestionRepository _questionRepository; + private readonly ICompletionRepository _completionRepository; - public AnswerService(IAnswerRepository answerRepository) + public AnswerService(IAnswerRepository answerRepository, IQuestionRepository questionRepository, + ICompletionRepository completionRepository) { _answerRepository = answerRepository; + _questionRepository = questionRepository; + _completionRepository = completionRepository; } public async Task AddAnswerAsync(Answer answer) @@ -30,11 +36,23 @@ public class AnswerService : IAnswerService public async Task> GetAnswersByCompletionIdAsync(int completionId) { + var completion = await _completionRepository.GetByIdAsync(completionId); + if (completion is null) + { + throw new NotFoundException("Completion not found"); + } + return await _answerRepository.GetAnswersByCompletionIdAsync(completionId); } public async Task> GetAnswersByQuestionIdAsync(int questionId) { + var question = await _questionRepository.GetByIdAsync(questionId); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + return await _answerRepository.GetAnswersByQuestionIdAsync(questionId); } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs b/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs index cd02fbc..091d1f7 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/AnswerVariantsService.cs @@ -1,3 +1,4 @@ +using SurveyBackend.Services.Exceptions; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Core.Services; @@ -7,25 +8,39 @@ namespace SurveyBackend.Services.Services; public class AnswerVariantsService : IAnswerVariantsService { private readonly IAnswerVariantsRepository _answerVariantsRepository; + private readonly IQuestionRepository _questionRepository; - public AnswerVariantsService(IAnswerVariantsRepository answerVariantsRepository) + public AnswerVariantsService(IAnswerVariantsRepository answerVariantsRepository, + IQuestionRepository questionRepository) { _answerVariantsRepository = answerVariantsRepository; + _questionRepository = questionRepository; } + // TODO: любой кто будет читать этот код, я понимаю проблему дублирования, не злитесь пожалуйста, я потом пишу на адекватные валидаторы и не будет дублирования public async Task AddAnswerVariantAsync(AnswerVariant answerVariant) { - // TODO: проверка существования такого вопроса + var question = await _questionRepository.GetByIdAsync(answerVariant.QuestionId); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + await _answerVariantsRepository.AddAsync(answerVariant); } public async Task UpdateAnswerVariantAsync(AnswerVariant answerVariant) { - // TODO: опять проверка существования, но еще и варианта + var question = await _questionRepository.GetByIdAsync(answerVariant.QuestionId); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + var answerVariantBase = await _answerVariantsRepository.GetByIdAsNoTrackingAsync(answerVariant.Id); if (answerVariantBase is null) { - throw new Exception("Answer Variant not found"); + throw new NotFoundException("Answer Variant not found"); } answerVariant.QuestionId = answerVariantBase.QuestionId; @@ -35,19 +50,34 @@ public class AnswerVariantsService : IAnswerVariantsService public async Task DeleteAnswerVariantAsync(int id) { - // TODO: проверка существования варианта + var answerVariantBase = await _answerVariantsRepository.GetByIdAsNoTrackingAsync(id); + if (answerVariantBase is null) + { + throw new NotFoundException("Answer Variant not found"); + } + await _answerVariantsRepository.DeleteAsync(id); } public async Task GetAnswerVariantByIdAsync(int id) { - // TODO: проверка существования варианта - return await _answerVariantsRepository.GetByIdAsync(id); + var answerVariant = await _answerVariantsRepository.GetByIdAsync(id); + if (answerVariant is null) + { + throw new NotFoundException("Answer Variant not found"); + } + + return answerVariant; } public async Task> GetAnswerVariantsByQuestionIdAsync(int questionId) { - // TODO: проверка существования вопроса + var question = await _questionRepository.GetByIdAsync(questionId); + if (question is null) + { + throw new NotFoundException("Question not found"); + } + 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 index 655c9dd..6aee2dc 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/CompletionService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/CompletionService.cs @@ -8,15 +8,22 @@ namespace SurveyBackend.Services.Services; public class CompletionService : ICompletionService { private readonly ICompletionRepository _completionRepository; + private readonly ISurveyRepository _surveyRepository; - public CompletionService(ICompletionRepository completionRepository) + public CompletionService(ICompletionRepository completionRepository, ISurveyRepository surveyRepository) { _completionRepository = completionRepository; + _surveyRepository = surveyRepository; } public async Task AddCompletionAsync(Completion completion) { - // TODO: проверить существование опроса + var survey = await _surveyRepository.GetByIdAsync(completion.SurveyId); + if (survey is null) + { + throw new NotFoundException("Survey not found"); + } + await _completionRepository.AddAsync(completion); } @@ -45,7 +52,12 @@ public class CompletionService : ICompletionService public async Task> GetCompletionsBySurveyIdAsync(int surveyId) { - // TODO: проверить существование опроса + var survey = await _surveyRepository.GetByIdAsync(surveyId); + if (survey is null) + { + throw new NotFoundException("Survey not found"); + } + // TODO: проверить что запрашивает создатель (хз как) return await _completionRepository.GetCompletionsBySurveyIdAsync(surveyId); } diff --git a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs index 003028c..2dcf297 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs @@ -22,7 +22,12 @@ public class QuestionService : IQuestionService public async Task AddQuestionAsync(QuestionBase question) { - // TODO: проверить существование опроса + var survey = await _surveyRepository.GetByIdAsync(question.SurveyId); + if (survey is null) + { + throw new NotFoundException("Survey not found"); + } + await _questionRepository.AddAsync(question); }