cs
[Authorize]
public class CategoryService : ICategoryService
{
private readonly DataDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly string? _userId;
public CategoryService(DataDbContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
_userId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
}
public async Task<List<CategoryDTO>> GetAll()
{
return await _context.Categories.Where( c => c.UserId == _userId ).Select( c => new CategoryDTO()
{
Id = c.Id,
Name = c.Name
} ).ToListAsync();
}
cs
[Authorize]
public class CategoryService : ICategoryService
{
private readonly DataDbContext _context;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly string? _userId;
public CategoryService(DataDbContext context, IHttpContextAccessor httpContextAccessor)
{
_context = context;
_httpContextAccessor = httpContextAccessor;
_userId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
}
public async Task<List<CategoryDTO>> GetAll()
{
return await _context.Categories.Where( c => c.UserId == _userId ).Select( c => new CategoryDTO()
{
Id = c.Id,
Name = c.Name
} ).ToListAsync();
}