89 lines
2.8 KiB
C#
89 lines
2.8 KiB
C#
using Fitness_core_BE.Interfaces;
|
|
using Fitness_core_BE.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Fitness_core_BE.Repos
|
|
{
|
|
public class GymRoleRepository : IGymRoleRepository
|
|
{
|
|
private readonly TenantDbContext _context;
|
|
|
|
public GymRoleRepository(TenantDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<List<GymRole>> GetRolesByClientId(Guid clientId)
|
|
{
|
|
return await _context.GymRoles
|
|
.Include(r => r.Permissions)
|
|
.Where(r => r.FKClientId == clientId)
|
|
.OrderBy(r => r.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<GymRole?> GetRoleById(Guid gymRoleId)
|
|
{
|
|
return await _context.GymRoles
|
|
.Include(r => r.Permissions)
|
|
.FirstOrDefaultAsync(r => r.GymRoleId == gymRoleId);
|
|
}
|
|
|
|
public async Task<GymRole> CreateRole(GymRole role)
|
|
{
|
|
role.GymRoleId = Guid.NewGuid();
|
|
role.CreatedAt = DateTime.UtcNow;
|
|
role.IsActive = true;
|
|
_context.GymRoles.Add(role);
|
|
return role;
|
|
}
|
|
|
|
public async Task<GymRole> UpdateRole(GymRole role)
|
|
{
|
|
return role;
|
|
}
|
|
|
|
public async Task DeleteRole(GymRole role)
|
|
{
|
|
_context.GymRoles.Remove(role);
|
|
}
|
|
|
|
public async Task ReplacePermissions(Guid gymRoleId, IEnumerable<string> navKeys)
|
|
{
|
|
var existing = _context.GymRolePermissions.Where(p => p.FKGymRoleId == gymRoleId);
|
|
_context.GymRolePermissions.RemoveRange(existing);
|
|
|
|
foreach (var key in navKeys.Distinct())
|
|
{
|
|
_context.GymRolePermissions.Add(new GymRolePermission { FKGymRoleId = gymRoleId, NavItemKey = key });
|
|
}
|
|
|
|
}
|
|
|
|
public async Task<List<string>> GetPermissionKeys(Guid gymRoleId)
|
|
{
|
|
return await _context.GymRolePermissions
|
|
.AsNoTracking()
|
|
.Where(p => p.FKGymRoleId == gymRoleId)
|
|
.Select(p => p.NavItemKey)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<bool> AssignStaffRole(Guid staffId, Guid clientId, Guid? gymRoleId)
|
|
{
|
|
var staff = await _context.Staff.FirstOrDefaultAsync(s => s.StaffId == staffId && s.FKClientId == clientId);
|
|
if (staff == null) return false;
|
|
|
|
if (gymRoleId.HasValue)
|
|
{
|
|
var roleBelongsToClient = await _context.GymRoles
|
|
.AnyAsync(r => r.GymRoleId == gymRoleId.Value && r.FKClientId == clientId);
|
|
if (!roleBelongsToClient) return false;
|
|
}
|
|
|
|
staff.FKGymRoleId = gymRoleId;
|
|
return true;
|
|
}
|
|
}
|
|
}
|