98 lines
No EOL
3.3 KiB
C#
98 lines
No EOL
3.3 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 IAnswerVariantsRepository _answerVariantsRepository;
|
||
|
||
public QuestionService(IQuestionRepository questionRepository, ISurveyRepository surveyRepository,
|
||
IAnswerVariantsRepository answerVariantsRepository)
|
||
{
|
||
_questionRepository = questionRepository;
|
||
_surveyRepository = surveyRepository;
|
||
_answerVariantsRepository = answerVariantsRepository;
|
||
}
|
||
|
||
public async Task AddQuestionAsync(QuestionBase question)
|
||
{
|
||
var survey = await _surveyRepository.GetByIdAsync(question.SurveyId);
|
||
if (survey is null)
|
||
{
|
||
throw new NotFoundException("Survey not found");
|
||
}
|
||
|
||
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;
|
||
|
||
// Если изменился тип вопроса (Discriminator), используем новый тип
|
||
if (questionBase.Discriminator != question.Discriminator)
|
||
{
|
||
var answerVariants = await _answerVariantsRepository.GetAnswerVariantsByQuestionIdAsync(questionBase.Id);
|
||
// Удаляем старый вопрос
|
||
await _questionRepository.DeleteAsync(questionBase.Id);
|
||
|
||
// Добавляем новый с тем же ID
|
||
await _questionRepository.AddAsync(question);
|
||
foreach (var answerVariant in answerVariants)
|
||
{
|
||
answerVariant.QuestionId = question.Id;
|
||
await _answerVariantsRepository.AddAsync(answerVariant);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// Если тип не меняется, просто обновляем
|
||
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);
|
||
}
|
||
} |