mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-02-22 22:55:01 +01:00
[ADD] Creating queries
This commit is contained in:
parent
65d625a30d
commit
eef7cfb2b9
|
@ -5,7 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
|
||||
namespace DotBased.AspNet.Authority.EFCore;
|
||||
|
||||
public static class DI
|
||||
public static class Extensions
|
||||
{
|
||||
public static IServiceCollection AddAuthorityContext(this IServiceCollection services, Action<DbContextOptionsBuilder> options)
|
||||
{
|
|
@ -1,14 +1,28 @@
|
|||
using DotBased.AspNet.Authority.Models.Authority;
|
||||
using DotBased.AspNet.Authority.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||
|
||||
public class UserRepository : IUserRepository
|
||||
public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory) : IUserRepository
|
||||
{
|
||||
public Task<ListResult<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "",
|
||||
CancellationToken? cancellationToken = null)
|
||||
public async Task<ListResult<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken? cancellationToken = null)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
await using var context = await contextFactory.CreateDbContextAsync();
|
||||
var query = context.Users.AsQueryable();
|
||||
if (!string.IsNullOrWhiteSpace(search))
|
||||
{
|
||||
query = query.Where(u => $"{u.Id} {u.Name} {u.UserName} {u.EmailAddress} {u.PhoneNumber}".Contains(search, StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
var totalCount = query.Count();
|
||||
var selected = query.Skip(offset).Take(limit).Select(u => new AuthorityUserItem()
|
||||
{
|
||||
Id = u.Id,
|
||||
UserName = u.UserName,
|
||||
EmailAddress = u.EmailAddress,
|
||||
PhoneNumber = u.PhoneNumber
|
||||
});
|
||||
return ListResult<AuthorityUserItem>.Ok(selected, totalCount, limit, offset);
|
||||
}
|
||||
|
||||
public Task<Result<AuthorityUser>> GetAuthorityUserByIdAsync(string id, CancellationToken? cancellationToken = null)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System.Text;
|
||||
using DotBased.AspNet.Authority.Attributes;
|
||||
|
||||
namespace DotBased.AspNet.Authority.Models.Authority;
|
||||
|
@ -19,7 +20,9 @@ public class AuthorityUser()
|
|||
|
||||
public DateTime LockedDate { get; set; }
|
||||
|
||||
public string? UserName { get; set; }
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public string? PasswordHash { get; set; }
|
||||
|
||||
|
@ -43,5 +46,15 @@ public class AuthorityUser()
|
|||
|
||||
public ICollection<AuthorityAttribute> Attributes { get; set; } = [];
|
||||
|
||||
public override string ToString() => UserName ?? EmailAddress ?? string.Empty;
|
||||
public override string ToString()
|
||||
{
|
||||
var strBuilder = new StringBuilder();
|
||||
strBuilder.Append(!string.IsNullOrWhiteSpace(Name) ? Name : UserName);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(EmailAddress)) return strBuilder.ToString();
|
||||
|
||||
strBuilder.Append(strBuilder.Length == 0 ? EmailAddress : $" ({EmailAddress})");
|
||||
|
||||
return strBuilder.ToString();
|
||||
}
|
||||
}
|
|
@ -2,5 +2,8 @@ namespace DotBased.AspNet.Authority.Models.Authority;
|
|||
|
||||
public class AuthorityUserItem
|
||||
{
|
||||
|
||||
public Guid Id { get; set; }
|
||||
public string UserName { get; set; } = string.Empty;
|
||||
public string? EmailAddress { get; set; } = string.Empty;
|
||||
public string? PhoneNumber { get; set; } = string.Empty;
|
||||
}
|
|
@ -79,7 +79,7 @@ public class ListResult<TItem> : Result
|
|||
/// </summary>
|
||||
public int Offset { get; }
|
||||
|
||||
public static ListResult<TItem> Ok(IEnumerable<TItem> items, int totalCount = -1) =>
|
||||
public static ListResult<TItem> Ok(IEnumerable<TItem> items, int totalCount = -1, int limit = -1, int offset = -1) =>
|
||||
new(true, string.Empty, totalCount, items);
|
||||
|
||||
public new static ListResult<TItem> Failed(string message, Exception? exception = null) =>
|
||||
|
|
Loading…
Reference in New Issue
Block a user