[ADD] Added sqlite to test project, created di for ef core context. Reworked repositories to use result class.

This commit is contained in:
max 2025-02-01 01:02:27 +01:00
parent e914023c5a
commit 5b4509cac3
17 changed files with 185 additions and 44 deletions

View File

@ -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<DbContextOptionsBuilder> options)
{
services.AddDbContextFactory<AuthorityContext>(options);
return services;
}
}

View File

@ -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<ListResult<AuthorityAttributeItem>> GetAttributesAsync(int limit = 20, int offset = 0, string search = "",
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> GetAttributeByIdAsync(string id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result> DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}

View File

@ -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<ListResult<AuthorityGroupItem>> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityAttribute>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}

View File

@ -5,7 +5,27 @@ namespace DotBased.AspNet.Authority.EFCore.Repositories;
public class RoleRepository : IRoleRepository
{
public Task<ListResult<AuthorityRole>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
public Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
public Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}

View File

@ -34,10 +34,10 @@ public partial class AuthorityManager
return errors.Count > 0 ? ValidationResult.Failed(errors) : ValidationResult.Ok();
}
public async Task<ListResult<AuthorityUser>> SearchUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null)
public async Task<ListResult<AuthorityUserItem>> 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<AuthorityUser>.Failed("No results!") : ListResult<AuthorityUser>.Ok(searchResult.Item1, searchResult.Item2);
var result = await UserRepository.GetAuthorityUsersAsync(maxResults, offset, query, cancellationToken);
return result;
}
public async Task<AuthorityResult<AuthorityUser>> 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<AuthorityUser>.Error("Failed to save updates!") : AuthorityResult<AuthorityUser>.Ok(updateResult);
return AuthorityResult<AuthorityUser>.FromResult(updateResult);
}
public async Task<AuthorityResult<AuthorityUser>> CreateUserAsync(AuthorityUser userModel, string password, CancellationToken? cancellationToken = null)
@ -76,18 +76,16 @@ public partial class AuthorityManager
var userCreationResult = await UserRepository.CreateUserAsync(userModel, cancellationToken);
return userCreationResult != null
? AuthorityResult<AuthorityUser>.Ok(userCreationResult)
: AuthorityResult<AuthorityUser>.Error("Failed to create user in repository!");
return AuthorityResult<AuthorityUser>.FromResult(userCreationResult);
}
public async Task<Result<AuthorityUser>> UpdateUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null)
{
var updateResult = await UserRepository.UpdateUserAsync(model, cancellationToken);
return updateResult != null ? Result<AuthorityUser>.Ok(updateResult) : Result<AuthorityUser>.Failed("Failed to update user in repository!");
return updateResult;
}
public async Task<bool> DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null)
public async Task<Result> DeleteUserAsync(AuthorityUser model, CancellationToken? cancellationToken = null)
{
var deleteResult = await UserRepository.DeleteUserAsync(model, cancellationToken);
return deleteResult;

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityAttributeItem
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityGroupItem
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityRoleItem
{
}

View File

@ -0,0 +1,6 @@
namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityUserItem
{
}

View File

@ -2,20 +2,23 @@ using DotBased.AspNet.Authority.Models.Validation;
namespace DotBased.AspNet.Authority.Models;
public class AuthorityResult<TResultValue>
public class AuthorityResult<TResultValue> : Result<TResultValue>
{
public AuthorityResult(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, List<ValidationError>? errors = null)
public static AuthorityResult<TResultValue> FromResult(Result<TResultValue> result) => new AuthorityResult<TResultValue>(result);
public AuthorityResult(Result<TResultValue> result) : base(result)
{
Reason = ResultFailReason.Unknown;
}
public AuthorityResult(bool success, string errorMessage = "", TResultValue? value = default, ResultFailReason reason = ResultFailReason.None, List<ValidationError>? 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<ValidationError>? ValidationErrors { get; }

View File

@ -1,6 +1,12 @@
using DotBased.AspNet.Authority.Models.Authority;
namespace DotBased.AspNet.Authority.Repositories;
public interface IAttributeRepository
{
public Task<ListResult<AuthorityAttributeItem>> GetAttributesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> GetAttributeByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default);
public Task<Result> DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default);
}

View File

@ -1,6 +1,12 @@
using DotBased.AspNet.Authority.Models.Authority;
namespace DotBased.AspNet.Authority.Repositories;
public interface IGroupRepository
{
public Task<ListResult<AuthorityGroupItem>> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
public Task<Result<AuthorityAttribute>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
public Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
}

View File

@ -4,5 +4,9 @@ namespace DotBased.AspNet.Authority.Repositories;
public interface IRoleRepository
{
public Task<ListResult<AuthorityRole>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default);
public Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default);
public Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
public Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
public Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
}

View File

@ -4,15 +4,14 @@ namespace DotBased.AspNet.Authority.Repositories;
public interface IUserRepository
{
public Task<AuthorityUser?> GetAuthorityUserByIdAsync(string id, CancellationToken? cancellationToken = null);
public Task<string> GetAuthorityUserIdAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<Tuple<List<AuthorityUser>?, int>> GetAuthorityUsersAsync(string query, int maxResults = 20, int offset = 0, CancellationToken? cancellationToken = null);
public Task<AuthorityUser?> GetAuthorityUserByEmailAsync(string email, CancellationToken? cancellationToken = null);
public Task SetVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null);
public Task<long> GetVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task SetSecurityVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null);
public Task<long> GetSecurityVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<AuthorityUser?> CreateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<AuthorityUser?> UpdateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<bool> DeleteUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<ListResult<AuthorityUserItem>> GetAuthorityUsersAsync(int limit = 20, int offset = 0, string search = "", CancellationToken? cancellationToken = null);
public Task<Result<AuthorityUser>> GetAuthorityUserByIdAsync(string id, CancellationToken? cancellationToken = null);
public Task<Result<AuthorityUser>> CreateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<Result<AuthorityUser>> UpdateUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<Result> DeleteUserAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<Result<AuthorityUser>> GetAuthorityUserByEmailAsync(string email, CancellationToken? cancellationToken = null);
public Task<Result> SetVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null);
public Task<Result<long>> GetVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
public Task<Result> SetSecurityVersionAsync(AuthorityUser user, long version, CancellationToken? cancellationToken = null);
public Task<Result<long>> GetSecurityVersionAsync(AuthorityUser user, CancellationToken? cancellationToken = null);
}

View File

@ -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 =>
{

View File

@ -8,11 +8,13 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.1" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DotBased.AspNet.Authority.EFCore\DotBased.AspNet.Authority.EFCore.csproj" />
<ProjectReference Include="..\DotBased.AspNet.Authority\DotBased.AspNet.Authority.csproj" />
<ProjectReference Include="..\DotBased.Logging.MEL\DotBased.Logging.MEL.csproj" />
<ProjectReference Include="..\DotBased.Logging.Serilog\DotBased.Logging.Serilog.csproj" />

View File

@ -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",