58 lines
No EOL
1.8 KiB
C#
58 lines
No EOL
1.8 KiB
C#
using SurveyBackend.Services.Exceptions;
|
|
using SurveyLib.Core.Models;
|
|
using SurveyLib.Core.Repositories;
|
|
using SurveyLib.Core.Services;
|
|
|
|
namespace SurveyBackend.Services.Services;
|
|
|
|
public class AnswerService : IAnswerService
|
|
{
|
|
private readonly IAnswerRepository _answerRepository;
|
|
private readonly IQuestionRepository _questionRepository;
|
|
private readonly ICompletionRepository _completionRepository;
|
|
|
|
public AnswerService(IAnswerRepository answerRepository, IQuestionRepository questionRepository,
|
|
ICompletionRepository completionRepository)
|
|
{
|
|
_answerRepository = answerRepository;
|
|
_questionRepository = questionRepository;
|
|
_completionRepository = completionRepository;
|
|
}
|
|
|
|
public async Task AddAnswerAsync(Answer answer)
|
|
{
|
|
await _answerRepository.AddAsync(answer);
|
|
}
|
|
|
|
public async Task UpdateAnswerAsync(Answer answer)
|
|
{
|
|
await _answerRepository.UpdateAsync(answer);
|
|
}
|
|
|
|
public async Task DeleteAnswerAsync(int id)
|
|
{
|
|
await _answerRepository.DeleteAsync(id);
|
|
}
|
|
|
|
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);
|
|
}
|
|
} |