using DotBased.AspNet.Authority.Models.Authority; using DotBased.AspNet.Authority.Repositories; using Microsoft.EntityFrameworkCore; namespace DotBased.AspNet.Authority.EFCore.Repositories; public class GroupRepository(IDbContextFactory contextFactory) : RepositoryBase, IGroupRepository { 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)) { 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.Ok(select, total, limit, offset); } catch (Exception e) { return HandleExceptionListResult("Failed to get Groups", e); } } 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)) { return Result.Failed("Invalid group id"); } var group = await context.Groups.Where(g => g.Id == groupId).Include(g => g.Attributes).FirstOrDefaultAsync(cancellationToken: cancellationToken); return Result.HandleResult(group, "Group not found"); } catch (Exception e) { return HandleExceptionResult("Failed to get Group", e); } } public async Task> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default) { try { await using var context = await contextFactory.CreateDbContextAsync(cancellationToken); if (group.Id == Guid.Empty) { return Result.Failed("Id cannot be empty."); } var entry = context.Groups.Add(group); var saveResult = await context.SaveChangesAsync(cancellationToken); return saveResult <= 0 ? Result.Failed("Failed to create group.") : Result.Ok(entry.Entity); } catch (Exception e) { return HandleExceptionResult("Failed to create group!", e); } } 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) { return Result.Failed("Group not found."); } if (currentGroup.Version != group.Version) { return Result.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.Failed("Failed to update group.") : Result.Ok(entry.Entity); } catch (Exception e) { return HandleExceptionResult("Failed to update group!", e); } } 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) { 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); } } }