added UserContext for easy UserId getting

started writing custom services to implement users logic
This commit is contained in:
Вячеслав 2025-04-20 15:21:34 +05:00
parent d810101033
commit 7a1078457d
6 changed files with 83 additions and 6 deletions

View file

@ -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());
}

View file

@ -1,10 +1,10 @@
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using SurveyBackend.Core.Contexts;
using SurveyBackend.DTOs.Survey; using SurveyBackend.DTOs.Survey;
using SurveyLib.Core.Models; using SurveyLib.Core.Models;
using SurveyLib.Core.Services; using SurveyLib.Core.Services;
using SurveyLib.Infrastructure.EFCore.Services;
namespace SurveyBackend.Controllers; namespace SurveyBackend.Controllers;
@ -13,10 +13,12 @@ namespace SurveyBackend.Controllers;
public class SurveyController : ControllerBase public class SurveyController : ControllerBase
{ {
private readonly ISurveyService _surveyService; private readonly ISurveyService _surveyService;
private readonly IUserContext _userContext;
public SurveyController(ISurveyService surveyService) public SurveyController(ISurveyService surveyService, IUserContext userContext)
{ {
_surveyService = surveyService; _surveyService = surveyService;
_userContext = userContext;
} }
[AllowAnonymous] [AllowAnonymous]
@ -39,7 +41,7 @@ public class SurveyController : ControllerBase
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody] CreateSurveyDTO dto) public async Task<IActionResult> 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 var survey = new Survey
{ {

View file

@ -1,11 +1,11 @@
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using SurveyBackend.Contexts;
using SurveyBackend.Core.Contexts;
using SurveyBackend.Core.Repositories; using SurveyBackend.Core.Repositories;
using SurveyBackend.Core.Services; using SurveyBackend.Core.Services;
using SurveyBackend.Infrastructure;
using SurveyBackend.Infrastructure.Data; using SurveyBackend.Infrastructure.Data;
using SurveyBackend.Infrastructure.Repositories; using SurveyBackend.Infrastructure.Repositories;
using SurveyBackend.Middlewares; using SurveyBackend.Middlewares;
@ -15,7 +15,6 @@ using SurveyLib.Core.Repositories;
using SurveyLib.Core.Services; using SurveyLib.Core.Services;
using SurveyLib.Infrastructure.EFCore.Data; using SurveyLib.Infrastructure.EFCore.Data;
using SurveyLib.Infrastructure.EFCore.Repositories; using SurveyLib.Infrastructure.EFCore.Repositories;
using SurveyLib.Infrastructure.EFCore.Services;
namespace SurveyBackend; namespace SurveyBackend;
@ -34,6 +33,10 @@ public class Program
builder.Services.AddScoped<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>()); builder.Services.AddScoped<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IUserContext, UserContext>();
builder.Services.AddScoped<IUserRepository, UserRepository>(); builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IUserService, UserService>();

View file

@ -0,0 +1,6 @@
namespace SurveyBackend.Core.Contexts;
public interface IUserContext
{
int UserId { get; }
}

View file

@ -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<IEnumerable<Survey>> GetSurveysAsync()
{
throw new NotImplementedException();
}
public async Task<Survey?> GetSurveyAsync(int id)
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Survey>> GetSurveysByUserIdAsync(int userId)
{
throw new NotImplementedException();
}
}

View file

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\SurveyLib\SurveyLib.Core\SurveyLib.Core.csproj" />
<ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj" /> <ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj" />
</ItemGroup> </ItemGroup>