feat(console): add UserRepository
This commit is contained in:
parent
abb5cd7c41
commit
a76f37fbc9
15
Repositories/IUserRepository.cs
Normal file
15
Repositories/IUserRepository.cs
Normal 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);
|
||||||
|
}
|
||||||
96
Repositories/UserRepository.cs
Normal file
96
Repositories/UserRepository.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user