62 lines
2.7 KiB
C#
62 lines
2.7 KiB
C#
using Distribution.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Distribution.Features.User;
|
|
|
|
public class UserRepository(AppDBContext context) : IUserRepository
|
|
{
|
|
public async Task<Models.User?> GetByIdAsync(Guid id) =>
|
|
await context.Users.Include(u => u.Role).FirstOrDefaultAsync(u => u.Id == id);
|
|
|
|
public async Task<Models.User?> GetByUsernameAsync(string username) =>
|
|
await context.Users.Include(u => u.Role).FirstOrDefaultAsync(u => u.Username == username);
|
|
|
|
public async Task<List<Models.User>> GetAllAsync() =>
|
|
await context.Users.Include(u => u.Role).ToListAsync();
|
|
|
|
public async Task AddAsync(Models.User entity) =>
|
|
await context.Users.AddAsync(entity);
|
|
|
|
public void Update(Models.User entity) =>
|
|
context.Users.Update(entity);
|
|
|
|
public void Remove(Models.User entity)
|
|
{
|
|
entity.IsActive = false;
|
|
context.Users.Update(entity);
|
|
}
|
|
|
|
public async Task<List<Models.UserRegionScope>> GetRegionScopesAsync(Guid userId) =>
|
|
await context.UserRegionScopes.Where(s => s.FK_UserId == userId && s.IsActive).ToListAsync();
|
|
|
|
public async Task<List<Models.UserRouteScope>> GetRouteScopesAsync(Guid userId) =>
|
|
await context.UserRouteScopes.Where(s => s.FK_UserId == userId && s.IsActive).ToListAsync();
|
|
|
|
public async Task<List<Models.UserStoreScope>> GetStoreScopesAsync(Guid userId) =>
|
|
await context.UserStoreScopes.Where(s => s.FK_UserId == userId && s.IsActive).ToListAsync();
|
|
|
|
public async Task ReplaceRegionScopesAsync(Guid userId, IEnumerable<Guid> regionIds)
|
|
{
|
|
var existing = await context.UserRegionScopes.Where(s => s.FK_UserId == userId).ToListAsync();
|
|
context.UserRegionScopes.RemoveRange(existing);
|
|
await context.UserRegionScopes.AddRangeAsync(
|
|
regionIds.Select(id => new Models.UserRegionScope { FK_UserId = userId, FK_RegionId = id }));
|
|
}
|
|
|
|
public async Task ReplaceRouteScopesAsync(Guid userId, IEnumerable<Guid> routeIds)
|
|
{
|
|
var existing = await context.UserRouteScopes.Where(s => s.FK_UserId == userId).ToListAsync();
|
|
context.UserRouteScopes.RemoveRange(existing);
|
|
await context.UserRouteScopes.AddRangeAsync(
|
|
routeIds.Select(id => new Models.UserRouteScope { FK_UserId = userId, FK_RouteId = id }));
|
|
}
|
|
|
|
public async Task ReplaceStoreScopesAsync(Guid userId, IEnumerable<Guid> storeIds)
|
|
{
|
|
var existing = await context.UserStoreScopes.Where(s => s.FK_UserId == userId).ToListAsync();
|
|
context.UserStoreScopes.RemoveRange(existing);
|
|
await context.UserStoreScopes.AddRangeAsync(
|
|
storeIds.Select(id => new Models.UserStoreScope { FK_UserId = userId, FK_StoreId = id }));
|
|
}
|
|
}
|