Updated QuestionService and SurveyService to pass the ID directly to the repository's DeleteAsync method. This improves clarity and ensures consistency in how deletions are handled across both services. Updated SurveyLib
65 lines
No EOL
1.9 KiB
C#
65 lines
No EOL
1.9 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)
|
|
{
|
|
await _questionRepository.AddAsync(question);
|
|
}
|
|
|
|
public async Task UpdateQuestionAsync(QuestionBase question)
|
|
{
|
|
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);
|
|
}
|
|
} |