1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-08-16 21:31:45 +02:00
Files
.config
.run
.workdir
Agent
Common
Controller
Phantom.Controller
Phantom.Controller.Database
Converters
Entities
Factories
Repositories
AuditLogRepository.Writer.cs
AuditLogRepository.cs
EventLogRepository.cs
PermissionRepository.cs
RoleRepository.cs
UserRepository.cs
UserRoleRepository.cs
ApplicationDbContext.cs
DatabaseMigrator.cs
IDbContextProvider.cs
ILazyDbContext.cs
Phantom.Controller.Database.csproj
Phantom.Controller.Database.Postgres
Phantom.Controller.Minecraft
Phantom.Controller.Services
Docker
Utils
Web
.dockerignore
.gitattributes
.gitignore
AddMigration.bat
AddMigration.sh
Directory.Build.props
Directory.Build.targets
Dockerfile
LICENSE
Packages.props
PhantomPanel.sln
README.md
global.json

51 lines
1.4 KiB
C#

using System.Collections.Immutable;
using Microsoft.EntityFrameworkCore;
using Phantom.Common.Data;
using Phantom.Common.Data.Web.Users;
using Phantom.Controller.Database.Entities;
using Phantom.Utils.Collections;
namespace Phantom.Controller.Database.Repositories;
public sealed class RoleRepository {
private const int MaxRoleNameLength = 40;
private readonly ILazyDbContext db;
public RoleRepository(ILazyDbContext db) {
this.db = db;
}
public Task<ImmutableArray<RoleEntity>> GetAll() {
return db.Ctx.Roles.AsAsyncEnumerable().ToImmutableArrayAsync();
}
public Task<ImmutableDictionary<Guid, RoleEntity>> GetByGuids(ImmutableHashSet<Guid> guids) {
return db.Ctx.Roles
.Where(role => guids.Contains(role.RoleGuid))
.AsAsyncEnumerable()
.ToImmutableDictionaryAsync(static role => role.RoleGuid, static role => role);
}
public ValueTask<RoleEntity?> GetByGuid(Guid guid) {
return db.Ctx.Roles.FindAsync(guid);
}
public async Task<Result<RoleEntity, AddRoleError>> Create(string name) {
if (string.IsNullOrWhiteSpace(name)) {
return AddRoleError.NameIsEmpty;
}
else if (name.Length > MaxRoleNameLength) {
return AddRoleError.NameIsTooLong;
}
if (await db.Ctx.Roles.AnyAsync(role => role.Name == name)) {
return AddRoleError.NameAlreadyExists;
}
var role = new RoleEntity(Guid.NewGuid(), name);
db.Ctx.Roles.Add(role);
return role;
}
}