From 134647d6c9712bd672ab9f549c2dd9fa6ac37f2b Mon Sep 17 00:00:00 2001 From: Sam <315859133@qq.com> Date: Thu, 5 Feb 2026 14:15:20 +0800 Subject: [PATCH] feat(console): add RoleRepository --- Repositories/IRoleRepository.cs | 15 ++++++ Repositories/RoleRepository.cs | 86 +++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 Repositories/IRoleRepository.cs create mode 100644 Repositories/RoleRepository.cs diff --git a/Repositories/IRoleRepository.cs b/Repositories/IRoleRepository.cs new file mode 100644 index 0000000..2674ae5 --- /dev/null +++ b/Repositories/IRoleRepository.cs @@ -0,0 +1,15 @@ +using Fengling.AuthService.Models; + +namespace Fengling.Console.Repositories; + +public interface IRoleRepository +{ + Task GetByIdAsync(long id); + Task GetByNameAsync(string name); + Task> GetAllAsync(); + Task> GetPagedAsync(int page, int pageSize, string? name = null, string? tenantId = null); + Task CountAsync(string? name = null, string? tenantId = null); + Task AddAsync(ApplicationRole role); + Task UpdateAsync(ApplicationRole role); + Task DeleteAsync(ApplicationRole role); +} diff --git a/Repositories/RoleRepository.cs b/Repositories/RoleRepository.cs new file mode 100644 index 0000000..73a2a8c --- /dev/null +++ b/Repositories/RoleRepository.cs @@ -0,0 +1,86 @@ +using Fengling.AuthService.Data; +using Fengling.AuthService.Models; +using Microsoft.EntityFrameworkCore; + +namespace Fengling.Console.Repositories; + +public class RoleRepository : IRoleRepository +{ + private readonly ApplicationDbContext _context; + + public RoleRepository(ApplicationDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(long id) + { + return await _context.Roles.FindAsync(id); + } + + public async Task GetByNameAsync(string name) + { + return await _context.Roles.FirstOrDefaultAsync(r => r.Name == name); + } + + public async Task> GetAllAsync() + { + return await _context.Roles.ToListAsync(); + } + + public async Task> GetPagedAsync(int page, int pageSize, string? name = null, string? tenantId = null) + { + var query = _context.Roles.AsQueryable(); + + if (!string.IsNullOrEmpty(name)) + { + query = query.Where(r => r.Name != null && r.Name.Contains(name)); + } + + if (!string.IsNullOrEmpty(tenantId)) + { + query = query.Where(r => r.TenantId.ToString() == tenantId); + } + + return await query + .OrderByDescending(r => r.CreatedTime) + .Skip((page - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + } + + public async Task CountAsync(string? name = null, string? tenantId = null) + { + var query = _context.Roles.AsQueryable(); + + if (!string.IsNullOrEmpty(name)) + { + query = query.Where(r => r.Name != null && r.Name.Contains(name)); + } + + if (!string.IsNullOrEmpty(tenantId)) + { + query = query.Where(r => r.TenantId.ToString() == tenantId); + } + + return await query.CountAsync(); + } + + public async Task AddAsync(ApplicationRole role) + { + _context.Roles.Add(role); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(ApplicationRole role) + { + _context.Roles.Update(role); + await _context.SaveChangesAsync(); + } + + public async Task DeleteAsync(ApplicationRole role) + { + _context.Roles.Remove(role); + await _context.SaveChangesAsync(); + } +}