diff --git a/DotBased.AspNet.Authority.EFCore/DI.cs b/DotBased.AspNet.Authority.EFCore/DI.cs new file mode 100644 index 0000000..f5baba7 --- /dev/null +++ b/DotBased.AspNet.Authority.EFCore/DI.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; + +namespace DotBased.AspNet.Authority.EFCore; + +public static class DI +{ + public static IServiceCollection AddAuthorityContext(this IServiceCollection services, Action options) + { + services.AddDbContextFactory(options); + return services; + } +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs new file mode 100644 index 0000000..438e75d --- /dev/null +++ b/DotBased.AspNet.Authority.EFCore/Repositories/AttributeRepository.cs @@ -0,0 +1,33 @@ +using DotBased.AspNet.Authority.Models.Authority; +using DotBased.AspNet.Authority.Repositories; + +namespace DotBased.AspNet.Authority.EFCore.Repositories; + +public class AttributeRepository : IAttributeRepository +{ + public Task> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", + CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetAttributeByIdAsync(string id, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs new file mode 100644 index 0000000..a6fb010 --- /dev/null +++ b/DotBased.AspNet.Authority.EFCore/Repositories/GroupRepository.cs @@ -0,0 +1,32 @@ +using DotBased.AspNet.Authority.Models.Authority; +using DotBased.AspNet.Authority.Repositories; + +namespace DotBased.AspNet.Authority.EFCore.Repositories; + +public class GroupRepository : IGroupRepository +{ + public Task> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority.EFCore/Repositories/RoleRepository.cs b/DotBased.AspNet.Authority.EFCore/Repositories/RoleRepository.cs index 6a1b1d8..bd15ed7 100644 --- a/DotBased.AspNet.Authority.EFCore/Repositories/RoleRepository.cs +++ b/DotBased.AspNet.Authority.EFCore/Repositories/RoleRepository.cs @@ -5,7 +5,27 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories; public class RoleRepository : IRoleRepository { - public Task> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + public Task> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default) + { + throw new NotImplementedException(); + } + + public Task DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default) { throw new NotImplementedException(); } diff --git a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs index 5b07cfc..363c49c 100755 --- a/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs +++ b/DotBased.AspNet.Authority/Managers/AuthorityUserManager.cs @@ -34,10 +34,10 @@ public partial class AuthorityManager return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok(); } - public async Task> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null) + public async Task> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null) { - var searchResult = await UserRepository.GetAuthorityUsersAsync(query, maxResults, offset, cancellationToken); - return searchResult.Item1 == null ? ListResult.Failed("No results!") : ListResult.Ok(searchResult.Item1, searchResult.Item2); + var result = await UserRepository.GetAuthorityUsersAsync(maxResults, offset, query, cancellationToken); + return result; } public async Task> UpdatePasswordAsync(AuthorityUser user, string password, CancellationToken? cancellationToken = null) @@ -54,7 +54,7 @@ public partial class AuthorityManager user.SecurityVersion = GenerateVersion(); var updateResult = await UserRepository.UpdateUserAsync(user, cancellationToken); - return updateResult == null ? AuthorityResult.Error("Failed to save updates!") : AuthorityResult.Ok(updateResult); + return AuthorityResult.FromResult(updateResult); } public async Task> CreateUserAsync(AuthorityUser userModel, string password, CancellationToken? cancellationToken = null) @@ -75,19 +75,17 @@ public partial class AuthorityManager userModel.PasswordHash = hashedPassword; var userCreationResult = await UserRepository.CreateUserAsync(userModel, cancellationToken); - - return userCreationResult != null - ? AuthorityResult.Ok(userCreationResult) - : AuthorityResult.Error("Failed to create user in repository!"); + + return AuthorityResult.FromResult(userCreationResult); } public async Task> UpdateUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null) { var updateResult = await UserRepository.UpdateUserAsync(model, cancellationToken); - return updateResult != null ? Result.Ok(updateResult) : Result.Failed("Failed to update user in repository!"); + return updateResult; } - public async Task DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null) + public async Task DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null) { var deleteResult = await UserRepository.DeleteUserAsync(model, cancellationToken); return deleteResult; diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityAttributeItem.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityAttributeItem.cs new file mode 100644 index 0000000..44a0211 --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityAttributeItem.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Models.Authority; + +public class AuthorityAttributeItem +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityGroupItem.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityGroupItem.cs new file mode 100644 index 0000000..2b9bbcc --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityGroupItem.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Models.Authority; + +public class AuthorityGroupItem +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityRoleItem.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityRoleItem.cs new file mode 100644 index 0000000..e285d5a --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityRoleItem.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Models.Authority; + +public class AuthorityRoleItem +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/Authority/AuthorityUserItem.cs b/DotBased.AspNet.Authority/Models/Authority/AuthorityUserItem.cs new file mode 100644 index 0000000..6afe594 --- /dev/null +++ b/DotBased.AspNet.Authority/Models/Authority/AuthorityUserItem.cs @@ -0,0 +1,6 @@ +namespace DotBased.AspNet.Authority.Models.Authority; + +public class AuthorityUserItem +{ + +} \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Models/AuthorityResult.cs b/DotBased.AspNet.Authority/Models/AuthorityResult.cs index f6c6d35..3cb3340 100755 --- a/DotBased.AspNet.Authority/Models/AuthorityResult.cs +++ b/DotBased.AspNet.Authority/Models/AuthorityResult.cs @@ -2,20 +2,23 @@ using DotBased.AspNet.Authority.Models.Validation; namespace DotBased.AspNet.Authority.Models; -public class AuthorityResult +public class AuthorityResult : Result { - public AuthorityResult(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, List? errors = null) + public static AuthorityResult FromResult(Result result) => new AuthorityResult(result); + + public AuthorityResult(Result result) : base(result) + { + Reason = ResultFailReason.Unknown; + } + + public AuthorityResult(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, List? errors = null) : base(success, errorMessage, value, null) { Success = success; - ErrorMessage = errorMessage; + Message = errorMessage; Value = value; Reason = reason; ValidationErrors = errors; } - - public bool Success { get; } - public string ErrorMessage { get; } - public TResultValue? Value { get; } public ResultFailReason Reason { get; } public List? ValidationErrors { get; } diff --git a/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs b/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs index 7e22f88..7fcca43 100755 --- a/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IAttributeRepository.cs @@ -1,6 +1,12 @@ +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> GetAttributeByIdAsync(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 f16b7d2..f8eb385 100755 --- a/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IGroupRepository.cs @@ -1,6 +1,12 @@ +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> 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 diff --git a/DotBased.AspNet.Authority/Repositories/IRoleRepository.cs b/DotBased.AspNet.Authority/Repositories/IRoleRepository.cs index 69ab90d..83cec83 100755 --- a/DotBased.AspNet.Authority/Repositories/IRoleRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IRoleRepository.cs @@ -4,5 +4,9 @@ namespace DotBased.AspNet.Authority.Repositories; public interface IRoleRepository { - public Task> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default); + public Task> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default); + public Task> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default); + public Task> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default); + public Task DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default); } \ No newline at end of file diff --git a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs index f86cdb5..ca15430 100755 --- a/DotBased.AspNet.Authority/Repositories/IUserRepository.cs +++ b/DotBased.AspNet.Authority/Repositories/IUserRepository.cs @@ -4,15 +4,14 @@ namespace DotBased.AspNet.Authority.Repositories; public interface IUserRepository { - public Task GetAuthorityUserByIdAsync(string id, CancellationToken? cancellationToken = null); - public Task GetAuthorityUserIdAsync(AuthorityUser user, CancellationToken? cancellationToken = null); - public Task?, int>> GetAuthorityUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null); - public Task GetAuthorityUserByEmailAsync(string email, CancellationToken? cancellationToken = null); - public Task SetVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null); - public Task GetVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null); - public Task SetSecurityVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null); - public Task GetSecurityVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null); - public Task CreateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); - public Task UpdateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); - public Task DeleteUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); + public Task> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken? cancellationToken = null); + public Task> GetAuthorityUserByIdAsync(string id, CancellationToken? cancellationToken = null); + public Task> CreateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); + public Task> UpdateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); + public Task DeleteUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null); + public Task> GetAuthorityUserByEmailAsync(string email, CancellationToken? cancellationToken = null); + public Task SetVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null); + public Task> GetVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null); + public Task SetSecurityVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null); + public Task> GetSecurityVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null); } \ No newline at end of file diff --git a/TestWebApi/Program.cs b/TestWebApi/Program.cs index 323ca0c..1c24460 100755 --- a/TestWebApi/Program.cs +++ b/TestWebApi/Program.cs @@ -1,7 +1,9 @@ using DotBased.AspNet.Authority; +using DotBased.AspNet.Authority.EFCore; using DotBased.Logging; using DotBased.Logging.MEL; using DotBased.Logging.Serilog; +using Microsoft.EntityFrameworkCore; using Serilog; using TestWebApi; using ILogger = Serilog.ILogger; @@ -19,7 +21,10 @@ LogService.AddLogAdapter(new BasedSerilogAdapter(serilogLogger)); builder.Logging.ClearProviders(); builder.Logging.AddDotBasedLoggerProvider(LogService.Options); - +builder.Services.AddAuthorityContext(options => +{ + options.UseSqlite("Data Source=dev-dotbased.db"); +}); builder.Services.AddAuthority(options => { diff --git a/TestWebApi/TestWebApi.csproj b/TestWebApi/TestWebApi.csproj index ef97892..6340445 100755 --- a/TestWebApi/TestWebApi.csproj +++ b/TestWebApi/TestWebApi.csproj @@ -8,11 +8,13 @@ + + diff --git a/obs_DotBased/.obsidian/workspace.json b/obs_DotBased/.obsidian/workspace.json index 7819a34..9e458b2 100644 --- a/obs_DotBased/.obsidian/workspace.json +++ b/obs_DotBased/.obsidian/workspace.json @@ -4,21 +4,17 @@ "type": "split", "children": [ { - "id": "68f3abbbf106a47b", + "id": "89533e49f06550fb", "type": "tabs", "children": [ { - "id": "9629cc68ecd8963f", + "id": "65943ca25b411f17", "type": "leaf", "state": { - "type": "markdown", - "state": { - "file": "Modules/AspNet/DotBased.Authority/Repositories/UserRepository.md", - "mode": "source", - "source": false - }, + "type": "empty", + "state": {}, "icon": "lucide-file", - "title": "UserRepository" + "title": "New tab" } } ] @@ -162,10 +158,10 @@ "command-palette:Open command palette": false } }, - "active": "9629cc68ecd8963f", + "active": "65943ca25b411f17", "lastOpenFiles": [ - "Modules/AspNet/DotBased.Authority/Repository.md", "Modules/AspNet/DotBased.Authority/Repositories/UserRepository.md", + "Modules/AspNet/DotBased.Authority/Repository.md", "Modules/AspNet/DotBased.Authority/Repositories", "Modules/AspNet/DotBased.Authority/Data diagram.canvas", "Modules/AspNet/DotBased.Authority/Models/AuthorityAttribute.md",