added UserContext for easy UserId getting
started writing custom services to implement users logic
This commit is contained in:
parent
d810101033
commit
7a1078457d
6 changed files with 83 additions and 6 deletions
19
SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs
Normal file
19
SurveyBackend/SurveyBackend.API/Contexts/UserContext.cs
Normal 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());
|
||||
}
|
||||
|
|
@ -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<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
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<SurveyDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddScoped<IUserContext, UserContext>();
|
||||
|
||||
builder.Services.AddScoped<IUserRepository, UserRepository>();
|
||||
builder.Services.AddScoped<IUserService, UserService>();
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
namespace SurveyBackend.Core.Contexts;
|
||||
|
||||
public interface IUserContext
|
||||
{
|
||||
int UserId { get; }
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\SurveyLib\SurveyLib.Core\SurveyLib.Core.csproj" />
|
||||
<ProjectReference Include="..\SurveyBackend.Core\SurveyBackend.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue