From 7a1078457deb52c0bc632825492fcb7db496bf59 Mon Sep 17 00:00:00 2001 From: shept Date: Sun, 20 Apr 2025 15:21:34 +0500 Subject: [PATCH] added UserContext for easy UserId getting started writing custom services to implement users logic --- .../SurveyBackend.API/Contexts/UserContext.cs | 19 ++++++++ .../Controllers/SurveyController.cs | 8 ++-- SurveyBackend/SurveyBackend.API/Program.cs | 9 ++-- .../Contexts/IUserContext.cs | 6 +++ .../Services/SurveyService.cs | 46 +++++++++++++++++++ .../SurveyBackend.Services.csproj | 1 + 6 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs create mode 100644 SurveyBackend/SurveyBackend.Core/Contexts/IUserContext.cs create mode 100644 SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs diff --git a/SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs b/SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs new file mode 100644 index 0000000..298cc59 --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs @@ -0,0 +1,19 @@ +using System.Security.Claims; +using SurveyBackend.Core.Contexts; + +namespace SurveyBackend.Contexts; + +public class UserContext : IUserContext +{ + private readonly IHttpContextAccessor _httpContextAccessor; + + public UserContext(IHttpContextAccessor httpContextAccessor) + { + _httpContextAccessor = httpContextAccessor; + } + + public int UserId => + int.Parse( + _httpContextAccessor.HttpContext?.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier) + ?.Value ?? throw new UnauthorizedAccessException()); +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs index c98acfe..73e2af0 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/SurveyController.cs @@ -1,10 +1,10 @@ using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using SurveyBackend.Core.Contexts; using SurveyBackend.DTOs.Survey; using SurveyLib.Core.Models; using SurveyLib.Core.Services; -using SurveyLib.Infrastructure.EFCore.Services; namespace SurveyBackend.Controllers; @@ -13,10 +13,12 @@ namespace SurveyBackend.Controllers; public class SurveyController : ControllerBase { private readonly ISurveyService _surveyService; + private readonly IUserContext _userContext; - public SurveyController(ISurveyService surveyService) + public SurveyController(ISurveyService surveyService, IUserContext userContext) { _surveyService = surveyService; + _userContext = userContext; } [AllowAnonymous] @@ -39,7 +41,7 @@ public class SurveyController : ControllerBase [HttpPost] public async Task Post([FromBody] CreateSurveyDTO dto) { - var userId = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value); + var userId = _userContext.UserId; var survey = new Survey { diff --git a/SurveyBackend/SurveyBackend.API/Program.cs b/SurveyBackend/SurveyBackend.API/Program.cs index eeea7af..e5d9f51 100644 --- a/SurveyBackend/SurveyBackend.API/Program.cs +++ b/SurveyBackend/SurveyBackend.API/Program.cs @@ -1,11 +1,11 @@ -using System.Text; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.EntityFrameworkCore; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; +using SurveyBackend.Contexts; +using SurveyBackend.Core.Contexts; using SurveyBackend.Core.Repositories; using SurveyBackend.Core.Services; -using SurveyBackend.Infrastructure; using SurveyBackend.Infrastructure.Data; using SurveyBackend.Infrastructure.Repositories; using SurveyBackend.Middlewares; @@ -15,7 +15,6 @@ using SurveyLib.Core.Repositories; using SurveyLib.Core.Services; using SurveyLib.Infrastructure.EFCore.Data; using SurveyLib.Infrastructure.EFCore.Repositories; -using SurveyLib.Infrastructure.EFCore.Services; namespace SurveyBackend; @@ -34,6 +33,10 @@ public class Program builder.Services.AddScoped(provider => provider.GetRequiredService()); + builder.Services.AddHttpContextAccessor(); + + builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/SurveyBackend/SurveyBackend.Core/Contexts/IUserContext.cs b/SurveyBackend/SurveyBackend.Core/Contexts/IUserContext.cs new file mode 100644 index 0000000..4cd9127 --- /dev/null +++ b/SurveyBackend/SurveyBackend.Core/Contexts/IUserContext.cs @@ -0,0 +1,6 @@ +namespace SurveyBackend.Core.Contexts; + +public interface IUserContext +{ + int UserId { get; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs new file mode 100644 index 0000000..49d0fe9 --- /dev/null +++ b/SurveyBackend/SurveyBackend.Services/Services/SurveyService.cs @@ -0,0 +1,46 @@ +using SurveyBackend.Core.Repositories; +using SurveyLib.Core.Models; +using SurveyLib.Core.Repositories; +using SurveyLib.Core.Services; + +namespace SurveyBackend.Services.Services; + +public class SurveyService : ISurveyService +{ + private readonly ISurveyRepository _surveyRepository; + + public SurveyService(ISurveyRepository surveyRepository) + { + _surveyRepository = surveyRepository; + } + + public async Task AddSurveyAsync(Survey survey) + { + throw new NotImplementedException(); + } + + public async Task UpdateSurveyAsync(Survey survey) + { + throw new NotImplementedException(); + } + + public async Task DeleteSurveyAsync(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetSurveysAsync() + { + throw new NotImplementedException(); + } + + public async Task GetSurveyAsync(int id) + { + throw new NotImplementedException(); + } + + public async Task> GetSurveysByUserIdAsync(int userId) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/SurveyBackend.Services.csproj b/SurveyBackend/SurveyBackend.Services/SurveyBackend.Services.csproj index 5765782..5b28c52 100644 --- a/SurveyBackend/SurveyBackend.Services/SurveyBackend.Services.csproj +++ b/SurveyBackend/SurveyBackend.Services/SurveyBackend.Services.csproj @@ -7,6 +7,7 @@ +