feat(console): add UserRepository

This commit is contained in:
Sam 2026-02-05 14:14:43 +08:00
parent abb5cd7c41
commit a76f37fbc9
2 changed files with 111 additions and 0 deletions

View File

@ -0,0 +1,15 @@
using Fengling.AuthService.Models;
namespace Fengling.Console.Repositories;
public interface IUserRepository
{
Task<ApplicationUser?> GetByIdAsync(long id);
Task<ApplicationUser?> GetByUserNameAsync(string userName);
Task<IEnumerable<ApplicationUser>> GetAllAsync();
Task<IEnumerable<ApplicationUser>> GetPagedAsync(int page, int pageSize, string? userName = null, string? email = null, string? tenantId = null);
Task<int> CountAsync(string? userName = null, string? email = null, string? tenantId = null);
Task AddAsync(ApplicationUser user);
Task UpdateAsync(ApplicationUser user);
Task DeleteAsync(ApplicationUser user);
}

View File

@ -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<ApplicationUser?> GetByIdAsync(long id)
{
return await _context.Users.FindAsync(id);
}
public async Task<ApplicationUser?> GetByUserNameAsync(string userName)
{
return await _context.Users.FirstOrDefaultAsync(u => u.UserName == userName);
}
public async Task<IEnumerable<ApplicationUser>> GetAllAsync()
{
return await _context.Users.ToListAsync();
}
public async Task<IEnumerable<ApplicationUser>> 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<int> 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();
}
}