mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-02-22 22:55:01 +01:00
[CHANGE] Updated queries, exception handler, role repository.
This commit is contained in:
parent
6c67276dca
commit
13b70c22f2
|
@ -0,0 +1,19 @@
|
||||||
|
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||||
|
|
||||||
|
public abstract class RepositoryBase
|
||||||
|
{
|
||||||
|
protected Result<T> HandleExceptionResult<T>(string message, Exception ex) => new(HandleException(message, ex));
|
||||||
|
|
||||||
|
protected ListResult<T> HandleExceptionListResult<T>(string message, Exception ex) =>
|
||||||
|
new(HandleException(message, ex));
|
||||||
|
|
||||||
|
protected Result HandleException(string message, Exception ex)
|
||||||
|
{
|
||||||
|
if (ex is OperationCanceledException oce)
|
||||||
|
{
|
||||||
|
return Result.Failed("Operation cancelled.", oce);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result.Failed(message, ex);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,32 +1,117 @@
|
||||||
using DotBased.AspNet.Authority.Models.Authority;
|
using DotBased.AspNet.Authority.Models.Authority;
|
||||||
using DotBased.AspNet.Authority.Repositories;
|
using DotBased.AspNet.Authority.Repositories;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||||
|
|
||||||
public class RoleRepository : IRoleRepository
|
public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IRoleRepository
|
||||||
{
|
{
|
||||||
public Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
public async Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var query = context.Roles.AsQueryable();
|
||||||
|
if (!string.IsNullOrWhiteSpace(search))
|
||||||
|
{
|
||||||
|
query = query.Where(r =>
|
||||||
|
$"{r.Name} {r.Id}".Contains(search, StringComparison.CurrentCultureIgnoreCase));
|
||||||
|
}
|
||||||
|
|
||||||
|
var total = await query.CountAsync(cancellationToken);
|
||||||
|
var select = await query.OrderBy(r => r.Name).Skip(offset).Take(limit).Select(r => new AuthorityRoleItem()
|
||||||
|
{
|
||||||
|
Id = r.Id,
|
||||||
|
Name = r.Name
|
||||||
|
}).ToListAsync(cancellationToken: cancellationToken);
|
||||||
|
return ListResult<AuthorityRoleItem>.Ok(select, total, limit, offset);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleExceptionListResult<AuthorityRoleItem>("Failed to get roles.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default)
|
public async Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
if (!Guid.TryParse(id, out var guid))
|
||||||
|
{
|
||||||
|
return Result<AuthorityRole>.Failed("Invalid id!");
|
||||||
|
}
|
||||||
|
var role = await context.Roles.Where(r => r.Id == guid).Include(r => r.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken);
|
||||||
|
return Result<AuthorityRole>.HandleResult(role, "Role not found!");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleExceptionResult<AuthorityRole>("Failed to get role!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
public async Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
if (role.Id == Guid.Empty)
|
||||||
|
{
|
||||||
|
return Result<AuthorityRole>.Failed("Id cannot be empty!");
|
||||||
|
}
|
||||||
|
var entity = context.Roles.Add(role);
|
||||||
|
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||||
|
return saveResult <= 0 ? Result<AuthorityRole>.Failed("Failed to create role!") : Result<AuthorityRole>.Ok(entity.Entity);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleExceptionResult<AuthorityRole>("Failed to create role!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
public async Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var currentRole = await context.Roles.FirstOrDefaultAsync(r => r.Id == role.Id, cancellationToken: cancellationToken);
|
||||||
|
if (currentRole == null)
|
||||||
|
{
|
||||||
|
return Result<AuthorityRole>.Failed("Role not found!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (role.Version != currentRole.Version)
|
||||||
|
{
|
||||||
|
return Result<AuthorityRole>.Failed("Role version does not match, version validation failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
var entity = context.Roles.Update(role);
|
||||||
|
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||||
|
return saveResult <= 0 ? Result<AuthorityRole>.Failed("Failed to update role!") : Result<AuthorityRole>.Ok(entity.Entity);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleExceptionResult<AuthorityRole>("Failed to update role!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
public async Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
|
var currentRole = await context.Roles.FirstOrDefaultAsync(r => r.Id == role.Id, cancellationToken: cancellationToken);
|
||||||
|
if (currentRole == null)
|
||||||
|
{
|
||||||
|
return Result.Failed("Role not found, could not delete!");
|
||||||
|
}
|
||||||
|
context.Roles.Remove(currentRole);
|
||||||
|
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||||
|
return saveResult <= 0 ? Result.Failed("Failed to delete role!") : Result.Ok();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
return HandleException("Failed to delete role!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||||
|
|
||||||
public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory) : IUserRepository
|
public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IUserRepository
|
||||||
{
|
{
|
||||||
public async Task<ListResult<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
public async Task<ListResult<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
|
@ -18,20 +18,19 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
$"{u.Id} {u.Name} {u.UserName} {u.EmailAddress} {u.PhoneNumber}".Contains(search,
|
$"{u.Id} {u.Name} {u.UserName} {u.EmailAddress} {u.PhoneNumber}".Contains(search,
|
||||||
StringComparison.CurrentCultureIgnoreCase));
|
StringComparison.CurrentCultureIgnoreCase));
|
||||||
}
|
}
|
||||||
|
|
||||||
var totalCount = query.Count();
|
var totalCount = query.Count();
|
||||||
var selected = query.Skip(offset).Take(limit).Select(u => new AuthorityUserItem()
|
var selected = await query.OrderBy(u => u.UserName).Skip(offset).Take(limit).Select(u => new AuthorityUserItem()
|
||||||
{
|
{
|
||||||
Id = u.Id,
|
Id = u.Id,
|
||||||
UserName = u.UserName,
|
UserName = u.UserName,
|
||||||
EmailAddress = u.EmailAddress,
|
EmailAddress = u.EmailAddress,
|
||||||
PhoneNumber = u.PhoneNumber
|
PhoneNumber = u.PhoneNumber
|
||||||
});
|
}).ToListAsync(cancellationToken: cancellationToken);
|
||||||
return ListResult<AuthorityUserItem>.Ok(selected, totalCount, limit, offset);
|
return ListResult<AuthorityUserItem>.Ok(selected, totalCount, limit, offset);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return ListResult<AuthorityUserItem>.Failed("Failed to get users.", e);
|
return HandleExceptionListResult<AuthorityUserItem>("Failed to get users.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,12 +44,12 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
return Result<AuthorityUser>.Failed("Invalid id!");
|
return Result<AuthorityUser>.Failed("Invalid id!");
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = await context.Users.FirstOrDefaultAsync(u => u.Id == guid, cancellationToken: cancellationToken);
|
var user = await context.Users.Where(u => u.Id == guid).Include(u => u.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken);
|
||||||
return Result<AuthorityUser>.HandleResult(user, "User not found.");
|
return Result<AuthorityUser>.HandleResult(user, "User not found.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<AuthorityUser>.Failed("Failed to get user.", e);
|
return HandleExceptionResult<AuthorityUser>("Failed to get user.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,7 +68,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<AuthorityUser>.Failed("Failed to create user.", e);
|
return HandleExceptionResult<AuthorityUser>("Failed to create user.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +94,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<AuthorityUser>.Failed("Failed to update user!", e);
|
return HandleExceptionResult<AuthorityUser>("Failed to update user!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +114,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result.Failed("Failed to delete user!", e);
|
return HandleException("Failed to delete user!", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -124,12 +123,12 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||||
var usr = await context.Users.FirstOrDefaultAsync(u => u.EmailAddress == email, cancellationToken: cancellationToken);
|
var usr = await context.Users.Where(u => u.EmailAddress == email).Include(u => u.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken);
|
||||||
return Result<AuthorityUser>.HandleResult(usr, "User not found by given email address.");
|
return Result<AuthorityUser>.HandleResult(usr, "User not found by given email address.");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<AuthorityUser>.Failed("An error occured while getting the user.", e);
|
return HandleExceptionResult<AuthorityUser>("An error occured while getting the user.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,7 +155,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result.Failed("An error occured while updating the version.", e);
|
return HandleException("An error occured while updating the version.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -170,7 +169,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<long>.Failed("An error occured while getting the user version.", e);
|
return HandleExceptionResult<long>("An error occured while getting the user version.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,7 +196,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result.Failed("An error occured while updating the security version.", e);
|
return HandleException("An error occured while updating the security version.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +210,7 @@ public class UserRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
return Result<long>.Failed("An error occured while getting the user security version.", e);
|
return HandleExceptionResult<long>("An error occured while getting the user security version.", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -2,5 +2,7 @@ namespace DotBased.AspNet.Authority.Models.Authority;
|
||||||
|
|
||||||
public class AuthorityRoleItem
|
public class AuthorityRoleItem
|
||||||
{
|
{
|
||||||
|
public Guid Id { get; set; } = Guid.NewGuid();
|
||||||
|
|
||||||
|
public string? Name { get; set; }
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user