[ADD] Added repository implementations

This commit is contained in:
max 2025-02-17 19:58:50 +01:00
parent 13b70c22f2
commit c6e11efdf2
6 changed files with 197 additions and 27 deletions

View File

@ -1,33 +1,114 @@
using DotBased.AspNet.Authority.Models.Authority;
using DotBased.AspNet.Authority.Repositories;
using Microsoft.EntityFrameworkCore;
namespace DotBased.AspNet.Authority.EFCore.Repositories;
public class AttributeRepository : IAttributeRepository
public class AttributeRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IAttributeRepository
{
public Task<ListResult<AuthorityAttributeItem>> GetAttributesAsync(int limit = 20, int offset = 0, string search = "",
public async Task<ListResult<AuthorityAttributeItem>> GetAttributesAsync(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.Attributes.AsQueryable();
if (!string.IsNullOrEmpty(search))
{
query = query.Where(a => $"{a.AttributeKey} {a.BoundId} {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.BoundId,
AttributeKey = a.AttributeKey,
AttributeValue = a.AttributeValue
}).ToListAsync(cancellationToken);
return ListResult<AuthorityAttributeItem>.Ok(select, total, limit, offset);
}
catch (Exception e)
{
return HandleExceptionListResult<AuthorityAttributeItem>("Failed to get attributes", e);
}
}
public Task<Result<AuthorityAttribute>> GetAttributeByIdAsync(string id, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityAttribute>> GetAttributeByKeyAsync(string key, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var attribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == key, cancellationToken);
return attribute == null ? Result<AuthorityAttribute>.Failed("Attribute not found") : Result<AuthorityAttribute>.Ok(attribute);
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityAttribute>("Failed to get attribute by id", e);
}
}
public Task<Result<AuthorityAttribute>> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityAttribute>> CreateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (string.IsNullOrWhiteSpace(attribute.AttributeKey) || attribute.BoundId == Guid.Empty)
{
return Result<AuthorityAttribute>.Failed("Attribute key and/or bound id is empty");
}
var entry = context.Attributes.Add(attribute);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result<AuthorityAttribute>.Failed("Failed to create attribute") : Result<AuthorityAttribute>.Ok(entry.Entity);
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityAttribute>("Failed to create attribute", e);
}
}
public Task<Result<AuthorityAttribute>> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityAttribute>> UpdateAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken);
if (currentAttribute == null)
{
return Result<AuthorityAttribute>.Failed("Attribute not found");
}
if (currentAttribute.Version != attribute.Version)
{
return Result<AuthorityAttribute>.Failed("Attribute version doesn't match");
}
var entry = context.Attributes.Update(currentAttribute);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result<AuthorityAttribute>.Failed("Failed to update attribute") : Result<AuthorityAttribute>.Ok(entry.Entity);
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityAttribute>("Failed to update attribute", e);
}
}
public Task<Result> DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
public async Task<Result> DeleteAttributeAsync(AuthorityAttribute attribute, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var currentAttribute = await context.Attributes.FirstOrDefaultAsync(a => a.AttributeKey == attribute.AttributeKey, cancellationToken);
if (currentAttribute == null)
{
return Result.Failed("Attribute not found");
}
context.Attributes.Remove(currentAttribute);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result.Failed("Failed to delete attribute") : Result.Ok();
}
catch (Exception e)
{
return HandleException("Failed to delete attribute", e);
}
}
}

View File

@ -1,32 +1,115 @@
using DotBased.AspNet.Authority.Models.Authority;
using DotBased.AspNet.Authority.Repositories;
using Microsoft.EntityFrameworkCore;
namespace DotBased.AspNet.Authority.EFCore.Repositories;
public class GroupRepository : IGroupRepository
public class GroupRepository(IDbContextFactory<AuthorityContext> contextFactory) : RepositoryBase, IGroupRepository
{
public Task<ListResult<AuthorityGroupItem>> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
public async Task<ListResult<AuthorityGroupItem>> GetGroupsAsync(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.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 ListResult<AuthorityGroupItem>.Ok(select, total, limit, offset);
}
catch (Exception e)
{
return HandleExceptionListResult<AuthorityGroupItem>("Failed to get Groups", e);
}
}
public Task<Result<AuthorityAttribute>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityGroup>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (!Guid.TryParse(id, out var groupId))
{
return Result<AuthorityGroup>.Failed("Invalid group id");
}
var group = await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken);
return Result<AuthorityGroup>.HandleResult(group, "Group not found");
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityGroup>("Failed to get Group", e);
}
}
public Task<Result<AuthorityAttribute>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityGroup>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
if (group.Id == Guid.Empty)
{
return Result<AuthorityGroup>.Failed("Id cannot be empty.");
}
var entry = context.Groups.Add(group);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result<AuthorityGroup>.Failed("Failed to create group.") : Result<AuthorityGroup>.Ok(entry.Entity);
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityGroup>("Failed to create group!", e);
}
}
public Task<Result<AuthorityAttribute>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
public async Task<Result<AuthorityGroup>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id ,cancellationToken);
if (currentGroup == null)
{
return Result<AuthorityGroup>.Failed("Group not found.");
}
if (currentGroup.Version != group.Version)
{
return Result<AuthorityGroup>.Failed("Group version does not match, version validation failed!");
}
var entry = context.Groups.Update(group);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result<AuthorityGroup>.Failed("Failed to update group.") : Result<AuthorityGroup>.Ok(entry.Entity);
}
catch (Exception e)
{
return HandleExceptionResult<AuthorityGroup>("Failed to update group!", e);
}
}
public Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
public async Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var currentGroup = await context.Groups.FirstOrDefaultAsync(g => g.Id == group.Id, cancellationToken);
if (currentGroup == null)
{
return Result.Failed("Group not found, cannot delete group!");
}
context.Groups.Remove(currentGroup);
var saveResult = await context.SaveChangesAsync(cancellationToken);
return saveResult <= 0 ? Result.Failed("Failed to delete group.") : Result.Ok();
}
catch (Exception e)
{
return HandleException("Failed to delete group!", e);
}
}
}

View File

@ -2,5 +2,9 @@ namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityAttributeItem
{
public Guid BoundId { get; set; }
public string AttributeKey { get; set; } = string.Empty;
public string AttributeValue { get; set; } = string.Empty;
}

View File

@ -2,5 +2,7 @@ namespace DotBased.AspNet.Authority.Models.Authority;
public class AuthorityGroupItem
{
public Guid Id { get; set; } = Guid.NewGuid();
public string? Name { get; set; }
}

View File

@ -5,7 +5,7 @@ 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>> GetAttributeByKeyAsync(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

@ -5,8 +5,8 @@ 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<AuthorityGroup>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default);
public Task<Result<AuthorityGroup>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
public Task<Result<AuthorityGroup>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
public Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
}