diff --git a/Repositories/IUserRepository.cs b/Repositories/IUserRepository.cs new file mode 100644 index 0000000..009f5f5 --- /dev/null +++ b/Repositories/IUserRepository.cs @@ -0,0 +1,15 @@ +using Fengling.AuthService.Models; + +namespace Fengling.Console.Repositories; + +public interface IUserRepository +{ + Task GetByIdAsync(long id); + Task GetByUserNameAsync(string userName); + Task> GetAllAsync(); + Task> GetPagedAsync(int page, int pageSize, string? userName = null, string? email = null, string? tenantId = null); + Task CountAsync(string? userName = null, string? email = null, string? tenantId = null); + Task AddAsync(ApplicationUser user); + Task UpdateAsync(ApplicationUser user); + Task DeleteAsync(ApplicationUser user); +} diff --git a/Repositories/UserRepository.cs b/Repositories/UserRepository.cs new file mode 100644 index 0000000..9677e64 --- /dev/null +++ b/Repositories/UserRepository.cs @@ -0,0 +1,96 @@ +using Fengling.AuthService.Data; +using Fengling.AuthService.Models; +using Microsoft.EntityFrameworkCore; + +namespace Fengling.Console.Repositories; + +public class UserRepository : IUserRepository +{ + private readonly ApplicationDbContext _context; + + public UserRepository(ApplicationDbContext context) + { + _context = context; + } + + public async Task GetByIdAsync(long id) + { + return await _context.Users.FindAsync(id); + } + + public async Task GetByUserNameAsync(string userName) + { + return await _context.Users.FirstOrDefaultAsync(u => u.UserName == userName); + } + + public async Task> GetAllAsync() + { + return await _context.Users.ToListAsync(); + } + + public async Task> GetPagedAsync(int page, int pageSize, string? userName = null, string? email = null, string? tenantId = null) + { + var query = _context.Users.AsQueryable(); + + if (!string.IsNullOrEmpty(userName)) + { + query = query.Where(u => u.UserName != null && u.UserName.Contains(userName)); + } + + if (!string.IsNullOrEmpty(email)) + { + query = query.Where(u => u.Email != null && u.Email.Contains(email)); + } + + if (!string.IsNullOrEmpty(tenantId)) + { + query = query.Where(u => u.TenantInfo.Id.ToString() == tenantId); + } + + return await query + .OrderByDescending(u => u.CreatedTime) + .Skip((page - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + } + + public async Task CountAsync(string? userName = null, string? email = null, string? tenantId = null) + { + var query = _context.Users.AsQueryable(); + + if (!string.IsNullOrEmpty(userName)) + { + query = query.Where(u => u.UserName != null && u.UserName.Contains(userName)); + } + + if (!string.IsNullOrEmpty(email)) + { + query = query.Where(u => u.Email != null && u.Email.Contains(email)); + } + + if (!string.IsNullOrEmpty(tenantId)) + { + query = query.Where(u => u.TenantInfo.Id.ToString() == tenantId); + } + + return await query.CountAsync(); + } + + public async Task AddAsync(ApplicationUser user) + { + _context.Users.Add(user); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(ApplicationUser user) + { + _context.Users.Update(user); + await _context.SaveChangesAsync(); + } + + public async Task DeleteAsync(ApplicationUser user) + { + _context.Users.Remove(user); + await _context.SaveChangesAsync(); + } +}