[CHANGE] Reworking Result monads
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using DotBased.AspNet.Authority.EFCore.Models;
|
||||
using DotBased.AspNet.Authority.Models;
|
||||
using DotBased.AspNet.Authority.Models.Authority;
|
||||
using DotBased.AspNet.Authority.Repositories;
|
||||
using DotBased.Monads;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||
@@ -8,7 +10,7 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories;
|
||||
|
||||
public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IRoleRepository
|
||||
{
|
||||
public async Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
public async Task<Result<QueryItems<AuthorityRoleItem>>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -26,29 +28,29 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
Id = r.Id,
|
||||
Name = r.Name
|
||||
}).ToListAsync(cancellationToken: cancellationToken);
|
||||
return ListResult<AuthorityRoleItem>.Ok(select, total, limit, offset);
|
||||
return QueryItems<AuthorityRoleItem>.Create(select, total, limit, offset);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<AuthorityRoleItem>("Failed to get roles.", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default)
|
||||
public async Task<Result<AuthorityRole>> GetRoleByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
if (!Guid.TryParse(id, out var guid))
|
||||
var role = await context.Roles.Where(r => r.Id == id).Include(r => r.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken);
|
||||
if (role != null)
|
||||
{
|
||||
return Result<AuthorityRole>.Failed("Invalid id!");
|
||||
return role;
|
||||
}
|
||||
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!");
|
||||
return ResultError.Fail("Role not found!");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionResult<AuthorityRole>("Failed to get role!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,15 +61,15 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
if (role.Id == Guid.Empty)
|
||||
{
|
||||
return Result<AuthorityRole>.Failed("Id cannot be empty!");
|
||||
return ResultError.Fail("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);
|
||||
return saveResult <= 0 ? ResultError.Fail("Failed to create role!") : entity.Entity;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionResult<AuthorityRole>("Failed to create role!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,21 +81,21 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
var currentRole = await context.Roles.FirstOrDefaultAsync(r => r.Id == role.Id, cancellationToken: cancellationToken);
|
||||
if (currentRole == null)
|
||||
{
|
||||
return Result<AuthorityRole>.Failed("Role not found!");
|
||||
return ResultError.Fail("Role not found!");
|
||||
}
|
||||
|
||||
if (role.Version != currentRole.Version)
|
||||
{
|
||||
return Result<AuthorityRole>.Failed("Role version does not match, version validation failed!");
|
||||
return ResultError.Fail("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);
|
||||
return saveResult <= 0 ? ResultError.Fail("Failed to update role!") : entity.Entity;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionResult<AuthorityRole>("Failed to update role!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,16 +109,16 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
context.Roles.RemoveRange(roles);
|
||||
context.RoleLinks.RemoveRange(context.RoleLinks.Where(rg => roleIds.Contains(rg.RoleId)));
|
||||
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to delete roles!") : Result.Ok();
|
||||
var removeResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return removeResult == roles.Count? Result.Success() : ResultError.Fail($"Not all roles have been removed! {removeResult} of {roles.Count} roles removed!");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException("Failed to delete role!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityRoleItem>> GetUserRolesAsync(AuthorityUser user, int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
public async Task<ListResultOld<AuthorityRoleItem>> GetUserRolesAsync(AuthorityUser user, int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -133,7 +135,7 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
Id = r.Id,
|
||||
Name = r.Name
|
||||
});
|
||||
return ListResult<AuthorityRoleItem>.Ok(roles, limit, offset);
|
||||
return ListResultOld<AuthorityRoleItem>.Ok(roles, limit, offset);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -151,26 +153,26 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
context.RoleLinks.Add(new RoleLink() { LinkId = linkId, RoleId = role.Id });
|
||||
}
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to ad role link!") : Result.Ok();
|
||||
return saveResult == roles.Count ? Result.Success() : ResultError.Fail($"Not all roles have been linked! {saveResult} of {roles.Count} roles linked!");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException("Failed to add role link!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityRole>> GetLinkedRolesAsync(List<Guid> linkIds, CancellationToken cancellationToken = default)
|
||||
public async Task<Result<List<AuthorityRole>>> GetLinkedRolesAsync(List<Guid> linkIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var linkedRoles = context.RoleLinks.Where(r => linkIds.Contains(r.LinkId)).Select(r => r.RoleId);
|
||||
var roleList = await context.Roles.Where(r => linkedRoles.Contains(r.Id)).ToListAsync(cancellationToken);
|
||||
return ListResult<AuthorityRole>.Ok(roleList.DistinctBy(r => r.Id));
|
||||
return roleList.DistinctBy(r => r.Id).ToList();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<AuthorityRole>("Failed to get linked roles!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,25 +184,25 @@ public class RoleRepository(IDbContextFactory<AuthorityContext> contextFactory)
|
||||
var roleIds = roles.Select(r => r.Id).ToList();
|
||||
context.RoleLinks.RemoveRange(context.RoleLinks.Where(rg => rg.LinkId == linkId && roleIds.Contains(rg.RoleId)));
|
||||
var saveResult = await context.SaveChangesAsync(cancellationToken);
|
||||
return saveResult <= 0 ? Result.Failed("Failed to delete role links!") : Result.Ok();
|
||||
return saveResult == roles.Count ? Result.Success() : ResultError.Fail($"Not all roles have been unlinked! {saveResult} of {roles.Count} roles unlinked!");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleException("Failed to delete role link!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ListResult<Guid>> HasRolesAsync(Guid linkId, List<AuthorityRole> roles, CancellationToken cancellationToken = default)
|
||||
public async Task<Result<List<Guid>>> HasRolesAsync(Guid linkId, List<AuthorityRole> roles, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
|
||||
var hasRoles = await context.RoleLinks.Where(r => r.LinkId == linkId && roles.Any(ar => ar.Id == r.RoleId)).Select(r => r.RoleId).ToListAsync(cancellationToken);
|
||||
return ListResult<Guid>.Ok(hasRoles);
|
||||
return hasRoles;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return HandleExceptionListResult<Guid>("Failed to determine role for user!", e);
|
||||
return e;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user