implemented custom SurveyService.cs
This commit is contained in:
parent
7a1078457d
commit
00e93fde2e
2 changed files with 38 additions and 8 deletions
|
|
@ -60,4 +60,13 @@ public class SurveyController : ControllerBase
|
|||
await _surveyService.DeleteSurveyAsync(id);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("my_surveys")]
|
||||
public async Task<IActionResult> GetMySurveys()
|
||||
{
|
||||
var userId = _userContext.UserId;
|
||||
var result = await _surveyService.GetSurveysByUserIdAsync(userId);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,6 @@
|
|||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.Core.Repositories;
|
||||
using SurveyBackend.Services.Exceptions;
|
||||
using SurveyLib.Core.Models;
|
||||
using SurveyLib.Core.Repositories;
|
||||
using SurveyLib.Core.Services;
|
||||
|
|
@ -8,39 +10,58 @@ namespace SurveyBackend.Services.Services;
|
|||
public class SurveyService : ISurveyService
|
||||
{
|
||||
private readonly ISurveyRepository _surveyRepository;
|
||||
private readonly IUserContext _userContext;
|
||||
|
||||
public SurveyService(ISurveyRepository surveyRepository)
|
||||
public SurveyService(ISurveyRepository surveyRepository, IUserContext userContext)
|
||||
{
|
||||
_surveyRepository = surveyRepository;
|
||||
_userContext = userContext;
|
||||
}
|
||||
|
||||
public async Task AddSurveyAsync(Survey survey)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
await _surveyRepository.AddAsync(survey);
|
||||
}
|
||||
|
||||
public async Task UpdateSurveyAsync(Survey survey)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (survey.CreatedBy != _userContext.UserId)
|
||||
throw new UnauthorizedAccessException("You are not authorized to update this survey.");
|
||||
await _surveyRepository.UpdateAsync(survey);
|
||||
}
|
||||
|
||||
public async Task DeleteSurveyAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var survey = await _surveyRepository.GetByIdAsync(id);
|
||||
if (survey is null)
|
||||
{
|
||||
throw new NotFoundException("Survey not found");
|
||||
}
|
||||
|
||||
if (survey.CreatedBy != _userContext.UserId)
|
||||
{
|
||||
throw new UnauthorizedAccessException("You are not authorized to delete this survey.");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Survey>> GetSurveysAsync()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _surveyRepository.GetAllAsync();
|
||||
}
|
||||
|
||||
public async Task<Survey?> GetSurveyAsync(int id)
|
||||
public async Task<Survey> GetSurveyAsync(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var survey = await _surveyRepository.GetByIdAsync(id);
|
||||
if (survey is null)
|
||||
{
|
||||
throw new NotFoundException("Survey not found");
|
||||
}
|
||||
|
||||
return survey;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Survey>> GetSurveysByUserIdAsync(int userId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return await _surveyRepository.GetSurveysByUserIdAsync(userId);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue