108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using Fengling.AuthService.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Fengling.Console.Repositories;
|
|
|
|
public interface IOAuthClientRepository
|
|
{
|
|
Task<OAuthApplication?> GetByIdAsync(long id);
|
|
Task<OAuthApplication?> GetByClientIdAsync(string clientId);
|
|
Task<IEnumerable<OAuthApplication>> GetAllAsync();
|
|
Task<IEnumerable<OAuthApplication>> GetPagedAsync(int page, int pageSize, string? displayName = null, string? clientId = null, string? status = null);
|
|
Task<int> CountAsync(string? displayName = null, string? clientId = null, string? status = null);
|
|
Task AddAsync(OAuthApplication client);
|
|
Task UpdateAsync(OAuthApplication client);
|
|
Task DeleteAsync(OAuthApplication client);
|
|
}
|
|
|
|
public class OAuthClientRepository : IOAuthClientRepository
|
|
{
|
|
private readonly Fengling.AuthService.Data.ApplicationDbContext _context;
|
|
|
|
public OAuthClientRepository(Fengling.AuthService.Data.ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<OAuthApplication?> GetByIdAsync(long id)
|
|
{
|
|
return await _context.OAuthApplications.FindAsync(id);
|
|
}
|
|
|
|
public async Task<OAuthApplication?> GetByClientIdAsync(string clientId)
|
|
{
|
|
return await _context.OAuthApplications.FirstOrDefaultAsync(c => c.ClientId == clientId);
|
|
}
|
|
|
|
public async Task<IEnumerable<OAuthApplication>> GetAllAsync()
|
|
{
|
|
return await _context.OAuthApplications.ToListAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<OAuthApplication>> GetPagedAsync(int page, int pageSize, string? displayName = null, string? clientId = null, string? status = null)
|
|
{
|
|
var query = _context.OAuthApplications.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(displayName))
|
|
{
|
|
query = query.Where(c => c.DisplayName.Contains(displayName));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(clientId))
|
|
{
|
|
query = query.Where(c => c.ClientId.Contains(clientId));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(status))
|
|
{
|
|
query = query.Where(c => c.Status == status);
|
|
}
|
|
|
|
return await query
|
|
.OrderByDescending(c => c.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<int> CountAsync(string? displayName = null, string? clientId = null, string? status = null)
|
|
{
|
|
var query = _context.OAuthApplications.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(displayName))
|
|
{
|
|
query = query.Where(c => c.DisplayName.Contains(displayName));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(clientId))
|
|
{
|
|
query = query.Where(c => c.ClientId.Contains(clientId));
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(status))
|
|
{
|
|
query = query.Where(c => c.Status == status);
|
|
}
|
|
|
|
return await query.CountAsync();
|
|
}
|
|
|
|
public async Task AddAsync(OAuthApplication client)
|
|
{
|
|
_context.OAuthApplications.Add(client);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateAsync(OAuthApplication client)
|
|
{
|
|
_context.OAuthApplications.Update(client);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteAsync(OAuthApplication client)
|
|
{
|
|
_context.OAuthApplications.Remove(client);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|