From 00e93fde2e106820dc76aa85f23f6f1622a6f560 Mon Sep 17 00:00:00 2001 From: shept Date: Sun, 20 Apr 2025 15:30:20 +0500 Subject: [PATCH] implemented custom SurveyService.cs --- .../Controllers/SurveyController.cs | 9 +++++ .../Services/SurveyService.cs | 37 +++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs index 73e2af0..938b2b7 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs @@ -60,4 +60,13 @@ public class SurveyController : ControllerBase await _surveyService.DeleteSurveyAsync(id); return Ok(); } + + [Authorize] + [HttpGet("my_surveys")] + public async Task GetMySurveys() + { + var userId = _userContext.UserId; + var result = await _surveyService.GetSurveysByUserIdAsync(userId); + return Ok(result); + } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs index 49d0fe9..90f51c9 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs @@ -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> GetSurveysAsync() { - throw new NotImplementedException(); + return await _surveyRepository.GetAllAsync(); } - public async Task GetSurveyAsync(int id) + public async Task 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> GetSurveysByUserIdAsync(int userId) { - throw new NotImplementedException(); + return await _surveyRepository.GetSurveysByUserIdAsync(userId); } } \ No newline at end of file