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,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<IEnumerable<Answer>> 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<IEnumerable<Answer>> GetAnswersByQuestionIdAsync(int questionId)
|
||||
{
|
||||
var question = await _questionRepository.GetByIdAsync(questionId);
|
||||
if (question is null)
|
||||
{
|
||||
throw new NotFoundException("Question not found");
|
||||
}
|
||||
|
||||
return await _answerRepository.GetAnswersByQuestionIdAsync(questionId);
|
||||
}
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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<IEnumerable<Completion>> 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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue