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) { var questionBase = await _questionRepository.GetByIdAsNoTrackingAsync(question.Id); if (questionBase is null) { throw new NotFoundException("Question not found"); } question.SurveyId = questionBase.SurveyId; 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 GetQuestionByIdAsync(int id) { var question = await _questionRepository.GetByIdAsync(id); if (question is null) { throw new NotFoundException("Question not found"); } return question; } public async Task> GetQuestionsBySurveyIdAsync(int surveyId) { var survey = await _surveyRepository.GetByIdAsync(surveyId); if (survey is null) { throw new NotFoundException("Survey not found"); } return await _questionRepository.GetQuestionsBySurveyId(surveyId); } }