move services to separate project
+ add GetByUserId methods to Survey
This commit is contained in:
parent
4caf3de6fe
commit
cc89ff818b
8 changed files with 37 additions and 3 deletions
43
SurveyLib.Services/Services/QuestionService.cs
Normal file
43
SurveyLib.Services/Services/QuestionService.cs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Repositories;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyLib.Services.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);
|
||||
}
|
||||
}
|
||||
51
SurveyLib.Services/Services/SurveyService.cs
Normal file
51
SurveyLib.Services/Services/SurveyService.cs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Repositories;
|
||||
using SurveyLib.Core.Services;
|
||||
|
||||
namespace SurveyLib.Services.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);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Survey>> GetSurveysByUserIdAsync(int userId)
|
||||
{
|
||||
return await _surveyRepository.GetSurveysByUserIdAsync(userId);
|
||||
}
|
||||
}
|
||||
13
SurveyLib.Services/SurveyLib.Services.csproj
Normal file
13
SurveyLib.Services/SurveyLib.Services.csproj
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SurveyLib.Core\SurveyLib.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Loading…
Add table
Add a link
Reference in a new issue