added basic services

This commit is contained in:
Вячеслав 2025-04-16 18:04:55 +05:00
parent 3e570decd7
commit 6e8d6cebdd
6 changed files with 100 additions and 12 deletions

View file

@ -4,5 +4,5 @@ namespace SurveyLib.Core.Repositories;
public interface IQuestionRepository : IGenericRepository<QuestionBase>
{
public IEnumerable<QuestionBase> GetQuestionsBySurveyId(int surveyId);
public Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyId(int surveyId);
}

View file

@ -4,8 +4,9 @@ namespace SurveyLib.Core.Services;
public interface IQuestionService
{
Task<bool> AddQuestionAsync(QuestionBase question);
Task<bool> UpdateQuestionAsync(QuestionBase question);
Task<bool> DeleteQuestionAsync(int id);
Task AddQuestionAsync(QuestionBase question);
Task UpdateQuestionAsync(QuestionBase question);
Task DeleteQuestionAsync(int id);
Task<QuestionBase> GetQuestionByIdAsync(int id);
Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId);
}

View file

@ -4,11 +4,9 @@ namespace SurveyLib.Core.Services;
public interface ISurveyService
{
Task<bool> AddSurveyAsync(Survey survey);
Task<bool> UpdateSurveyAsync(Survey survey);
Task<bool> DeleteSurveyAsync(int id);
Task AddSurveyAsync(Survey survey);
Task UpdateSurveyAsync(Survey survey);
Task DeleteSurveyAsync(int id);
Task<IEnumerable<Survey>> GetSurveysAsync();
Task<Survey> GetSurveyByIdAsync(int id);
Task<Survey> GetSurveyWithQuestionsByIdAsync(int id);
Task<Survey> GetSurveyWithAnswersByIdAsync(int id);
Task<Survey?> GetSurveyAsync(int id);
}

View file

@ -42,8 +42,8 @@ public class QuestionRepository : IQuestionRepository
await _context.SaveChangesAsync();
}
public IEnumerable<QuestionBase> GetQuestionsBySurveyId(int surveyId)
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyId(int surveyId)
{
return _context.Questions.Where(q => q.SurveyId == surveyId);
return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync();
}
}

View file

@ -0,0 +1,43 @@
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Core.Services;
namespace SurveyLib.Infrastructure.EFCore.Services;
public class QuestionService : IQuestionService
{
private readonly IQuestionRepository _questionRepository;
public QuestionService(IQuestionRepository questionRepository)
{
_questionRepository = questionRepository;
}
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 GetQuestionByIdAsync(id);
await _questionRepository.DeleteAsync(question);
}
public async Task<QuestionBase> GetQuestionByIdAsync(int id)
{
var question = await _questionRepository.GetByIdAsync(id) ?? throw new NullReferenceException();
return question;
}
public async Task<IEnumerable<QuestionBase>> GetQuestionsBySurveyIdAsync(int surveyId)
{
return await _questionRepository.GetQuestionsBySurveyId(surveyId);
}
}

View file

@ -0,0 +1,46 @@
using SurveyLib.Core.Models;
using SurveyLib.Core.Repositories;
using SurveyLib.Core.Services;
namespace SurveyLib.Infrastructure.EFCore.Services;
public class SurveyService : ISurveyService
{
private readonly ISurveyRepository _surveyRepository;
public SurveyService(ISurveyRepository surveyRepository)
{
_surveyRepository = surveyRepository;
}
public async Task AddSurveyAsync(Survey survey)
{
await _surveyRepository.AddAsync(survey);
}
public async Task UpdateSurveyAsync(Survey survey)
{
await _surveyRepository.UpdateAsync(survey);
}
public async Task DeleteSurveyAsync(int id)
{
var survey = await _surveyRepository.GetByIdAsync(id);
if (survey == null)
{
throw new NullReferenceException($"Survey with id: {id} was not found");
}
await _surveyRepository.DeleteAsync(survey);
}
public async Task<IEnumerable<Survey>> GetSurveysAsync()
{
return await _surveyRepository.GetAllAsync();
}
public async Task<Survey?> GetSurveyAsync(int id)
{
return await _surveyRepository.GetByIdAsync(id);
}
}