added existance check (not sure if it's working)
This commit is contained in:
parent
9a7c74e307
commit
a5aad09046
4 changed files with 78 additions and 13 deletions
|
|
@ -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<AnswerVariant> 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<IEnumerable<AnswerVariant>> 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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue