diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs index dd25a1f..6660de3 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs @@ -1,114 +1,86 @@ +using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Repositories; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace DotBased.AspNet.Authority.EFCore.Repositories; -public class AttributeRepository(IDbContextFactory contextFactory) : RepositoryBase, IAttributeRepository +public class AttributeRepository(IDbContextFactory contextFactory, ILogger logger) : RepositoryBase, IAttributeRepository { - public async Task> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", + public async Task> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var query = context.Attributes.AsQueryable(); + if (!string.IsNullOrEmpty(search)) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var query = context.Attributes.AsQueryable(); - if (!string.IsNullOrEmpty(search)) - { - query = query.Where(a => $"{a.AttributeKey} {a.ForeignKey} {a.AttributeValue}".Contains(search, StringComparison.CurrentCultureIgnoreCase)); - } + query = query.Where(a => $"{a.AttributeKey} {a.ForeignKey} {a.AttributeValue}".Contains(search, StringComparison.CurrentCultureIgnoreCase)); + } - var total = await query.CountAsync(cancellationToken); - var select = await query.OrderBy(a => a.AttributeKey).Skip(offset).Take(limit).Select(a => new AuthorityAttributeItem() - { - BoundId = a.ForeignKey, - AttributeKey = a.AttributeKey, - AttributeValue = a.AttributeValue - }).ToListAsync(cancellationToken); - return ListResultOld.Ok(select, total, limit, offset); - } - catch (Exception e) + var total = await query.CountAsync(cancellationToken); + var select = await query.OrderBy(a => a.AttributeKey).Skip(offset).Take(limit).Select(a => new AuthorityAttributeItem() { - return HandleExceptionListResult("Failed to get attributes", e); - } + BoundId = a.ForeignKey, + AttributeKey = a.AttributeKey, + AttributeValue = a.AttributeValue + }).ToListAsync(cancellationToken); + return QueryItems.Create(select, total, limit, offset); } - public async Task> GetAttributeByKeyAsync(string key, CancellationToken cancellationToken = default) + public async Task GetAttributeByKeyAsync(string key, CancellationToken cancellationToken = default) { - try - { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var attribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == key, cancellationToken); - return attribute == null ? ResultOld.Failed("Attribute not found") : ResultOld.Ok(attribute); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to get attribute by id", e); - } + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + return await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == key, cancellationToken); } - public async Task> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + public async Task CreateAttributeAsync(AuthorityAttribute attribute, + CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + if (string.IsNullOrWhiteSpace(attribute.AttributeKey) || attribute.ForeignKey == Guid.Empty) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if (string.IsNullOrWhiteSpace(attribute.AttributeKey) || attribute.ForeignKey == Guid.Empty) - { - return ResultOld.Failed("Attribute key and/or bound id is empty"); - } - var entry = context.Attributes.Add(attribute); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to create attribute") : ResultOld.Ok(entry.Entity); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to create attribute", e); + throw new Exception($"Attribute {attribute.AttributeKey} not found"); } + + var entry = context.Attributes.Add(attribute); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0 ? entry.Entity : null; } - public async Task> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + public async Task UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken); + if (currentAttribute == null) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken); - if (currentAttribute == null) - { - return ResultOld.Failed("Attribute not found"); - } + return null; + } - if (currentAttribute.Version != attribute.Version) - { - return ResultOld.Failed("Attribute version doesn't match"); - } + if (currentAttribute.Version != attribute.Version) + { + logger.LogError("Attribute version validation failed for attribute {attribute}", currentAttribute.AttributeKey); + return null; + } - var entry = context.Attributes.Update(currentAttribute); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to update attribute") : ResultOld.Ok(entry.Entity); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to update attribute", e); - } + var entry = context.Attributes.Update(currentAttribute); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0 ? entry.Entity : null; } - public async Task DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + public async Task DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken); + + if (currentAttribute == null) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken); - if (currentAttribute == null) - { - return ResultOld.Failed("Attribute not found"); - } - context.Attributes.Remove(currentAttribute); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to delete attribute") : ResultOld.Ok(); - } - catch (Exception e) - { - return HandleException("Failed to delete attribute", e); + logger.LogError("Attribute not found."); + return false; } + + context.Attributes.Remove(currentAttribute); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0; } } \ No newline at end of file diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs index 74e3a0c..3e3afcc 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs @@ -1,130 +1,94 @@ +using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Repositories; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; namespace DotBased.AspNet.Authority.EFCore.Repositories; -public class GroupRepository(IDbContextFactory contextFactory) : RepositoryBase, IGroupRepository +public class GroupRepository(IDbContextFactory contextFactory, ILogger logger) : RepositoryBase, IGroupRepository { - public async Task> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + public async Task> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var query = context.Groups.AsQueryable(); + if (!string.IsNullOrWhiteSpace(search)) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var query = context.Groups.AsQueryable(); - if (!string.IsNullOrWhiteSpace(search)) - { - query = query.Where(g => $"{g.Name} {g.Id}".Contains(search)); - } - var total = await query.CountAsync(cancellationToken); - var select = await query.OrderBy(g => g.Name).Skip(offset).Take(limit).Select(g => new AuthorityGroupItem() - { - Id = g.Id, - Name = g.Name - }).ToListAsync(cancellationToken); - return ListResultOld.Ok(select, total, limit, offset); + query = query.Where(g => $"{g.Name} {g.Id}".Contains(search)); } - catch (Exception e) + var total = await query.CountAsync(cancellationToken); + var select = await query.OrderBy(g => g.Name).Skip(offset).Take(limit).Select(g => new AuthorityGroupItem() { - return HandleExceptionListResult("Failed to get Groups", e); - } + Id = g.Id, + Name = g.Name + }).ToListAsync(cancellationToken); + return QueryItems.Create(select, total, limit, offset); } - public async Task> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default) + public async Task GetGroupByIdAsync(string id, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + + if (!Guid.TryParse(id, out var groupId)) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if (!Guid.TryParse(id, out var groupId)) - { - return ResultOld.Failed("Invalid group id"); - } - var group = await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken); - return ResultOld.HandleResult(group, "Group not found"); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to get Group", e); + throw new Exception($"Invalid group id: {id}"); } + + return await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken); } - public async Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) + public async Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) { - try - { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var userJoinGroups = context.UserGroups.Where(ug => ug.UserId == user.Id).Select(ug => ug.GroupId); - var userGroups = context.Groups.Where(g => userJoinGroups.Contains(g.Id)); - return ListResultOld.Ok(userGroups, userGroups.Count()); - } - catch (Exception e) - { - return HandleExceptionListResult("Failed to get Groups", e); - } + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var userJoinGroups = context.UserGroups.Where(ug => ug.UserId == user.Id).Select(ug => ug.GroupId); + var userGroups = context.Groups.Where(g => userJoinGroups.Contains(g.Id)); + return userGroups.ToList(); } - public async Task> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + public async Task CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + if (group.Id == Guid.Empty) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - if (group.Id == Guid.Empty) - { - return ResultOld.Failed("Id cannot be empty."); - } - var entry = context.Groups.Add(group); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to create group.") : ResultOld.Ok(entry.Entity); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to create group!", e); + throw new Exception($"Invalid group id: {group.Id}"); } + var entry = context.Groups.Add(group); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0 ? entry.Entity : null; } - public async Task> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + public async Task UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id ,cancellationToken); + if (currentGroup == null) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id ,cancellationToken); - if (currentGroup == null) - { - return ResultOld.Failed("Group not found."); - } + logger.LogError("Group with id {groupId} not found.", group.Id); + return null; + } - if (currentGroup.Version != group.Version) - { - return ResultOld.Failed("Group version does not match, version validation failed!"); - } + if (currentGroup.Version != group.Version) + { + logger.LogError("Group version validation failed."); + return null; + } - var entry = context.Groups.Update(group); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to update group.") : ResultOld.Ok(entry.Entity); - } - catch (Exception e) - { - return HandleExceptionResult("Failed to update group!", e); - } + var entry = context.Groups.Update(group); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0 ? entry.Entity : null; } - public async Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + public async Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) { - try + await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); + var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id, cancellationToken); + if (currentGroup == null) { - await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); - var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id, cancellationToken); - if (currentGroup == null) - { - return ResultOld.Failed("Group not found, cannot delete group!"); - } - context.Groups.Remove(currentGroup); - var saveResult = await context.SaveChangesAsync(cancellationToken); - return saveResult <= 0 ? ResultOld.Failed("Failed to delete group.") : ResultOld.Ok(); - } - catch (Exception e) - { - return HandleException("Failed to delete group!", e); + logger.LogError("Group with id {groupId} not found.", group.Id); + return false; } + context.Groups.Remove(currentGroup); + var saveResult = await context.SaveChangesAsync(cancellationToken); + return saveResult != 0; } } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs index aafc5c9..0f0a174 100755 --- a/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityGroupManager.cs @@ -1,11 +1,19 @@ using DotBased.AspNet.Authority.Models.Authority; +using DotBased.Monads; namespace DotBased.AspNet.Authority.Managers; public partial class AuthorityManager { - public async Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) + public async Task>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default) { - return await GroupRepository.GetUserGroupsAsync(user, cancellationToken); + try + { + return await GroupRepository.GetUserGroupsAsync(user, cancellationToken); + } + catch (Exception e) + { + return e; + } } } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs index 64e5f65..9250222 100755 --- a/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityRoleManager.cs @@ -109,9 +109,9 @@ public partial class AuthorityManager var searchIds = new List { user.Id }; var usrGroups = await GetUserGroupsAsync(user, cancellationToken); - if (usrGroups.Success) + if (usrGroups.IsSuccess) { - searchIds.AddRange(usrGroups.Items.Select(g => g.Id).ToList()); + searchIds.AddRange(usrGroups.Value.Select(g => g.Id).ToList()); } var linkedRolesResult = await RoleRepository.GetLinkedRolesAsync(searchIds, cancellationToken); diff --git a/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs b/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs index 2a5fd6e..fcf61a5 100755 --- a/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs @@ -1,12 +1,13 @@ +using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; namespace DotBased.AspNet.Authority.Repositories; public interface IAttributeRepository { - public Task> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); - public Task> GetAttributeByKeyAsync(string id, CancellationToken cancellationToken = default); - public Task> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); - public Task> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); - public Task DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); + public Task> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task GetAttributeByKeyAsync(string id, CancellationToken cancellationToken = default); + public Task CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); + public Task UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); + public Task DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs b/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs index 7cc8d59..66ff9c2 100755 --- a/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs @@ -1,13 +1,14 @@ +using DotBased.AspNet.Authority.Models; using DotBased.AspNet.Authority.Models.Authority; namespace DotBased.AspNet.Authority.Repositories; public interface IGroupRepository { - public Task> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); - public Task> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default); - public Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default); - public Task> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); - public Task> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); - public Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); + public Task> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task GetGroupByIdAsync(string id, CancellationToken cancellationToken = default); + public Task> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default); + public Task CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); + public Task UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); + public Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default); } \ No newline at end of file