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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue