survey-webapp/SurveyBackend/SurveyBackend.Services/Services/QuestionService.cs

67 lines
No EOL
2 KiB
C#

using SurveyBackend.Core.Contexts;
using SurveyBackend.Services.Exceptions;
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Core.Services;
namespace SurveyBackend.Services.Services;
public class QuestionService : IQuestionService
{
private readonly IQuestionRepository _questionRepository;
private readonly ISurveyRepository _surveyRepository;
private readonly IUserContext _userContext;
public QuestionService(IQuestionRepository questionRepository, ISurveyRepository surveyRepository,
IUserContext userContext)
{
_questionRepository = questionRepository;
_surveyRepository = surveyRepository;
_userContext = userContext;
}
public async Task AddQuestionAsync(QuestionBase question)
{
// TODO: проверить существование опроса
await _questionRepository.AddAsync(question);
}
public async Task UpdateQuestionAsync(QuestionBase question)
{
// TODO: проверить существование вопроса
await _questionRepository.UpdateAsync(question);
}
public async Task DeleteQuestionAsync(int id)
{
var question = await _questionRepository.GetByIdAsync(id);
if (question is null)
{
throw new NotFoundException("Question not found");
}
await _questionRepository.DeleteAsync(id);
}
public async Task<QuestionBase> GetQuestionByIdAsync(int id)
{
var question = await _questionRepository.GetByIdAsync(id);
if (question is null)
{
throw new NotFoundException("Question not found");
}
return question;
}
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId)
{
var survey = await _surveyRepository.GetByIdAsync(surveyId);
if (survey is null)
{
throw new NotFoundException("Survey not found");
}
return await _questionRepository.GetQuestionsBySurveyId(surveyId);
}
}