Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactoring models #61

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ riderModule.iml
src/Argon.Api/storage/

secrets.json

src/Argon.Api/Generated/
2 changes: 1 addition & 1 deletion src/Argon.Api/Controllers/AuthorizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ namespace Argon.Api.Controllers;

using Grains.Interfaces;
using Contracts;
using Extensions;
using Microsoft.AspNetCore.Mvc;
using Argon.Features;

[ApiController]
public class AuthorizationController(IGrainFactory grainFactory) : ControllerBase
Expand Down
1 change: 1 addition & 0 deletions src/Argon.Api/Controllers/MetadataController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Argon.Api.Controllers;

using Argon.Features;
using Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand Down
152 changes: 147 additions & 5 deletions src/Argon.Api/Entities/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,154 @@
namespace Argon.Api.Entities;

using System.Drawing;
using System.Linq.Expressions;
using Contracts.Models;
using Contracts.Models.ArchetypeModel;
using Features.EF;
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
{
public DbSet<User> Users { get; set; }
public DbSet<UserAgreements> UserAgreements { get; set; }
public DbSet<Server> Servers { get; set; }
public DbSet<Channel> Channels { get; set; }
public DbSet<UsersToServerRelation> UsersToServerRelations { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserAgreements> UserAgreements { get; set; }
public DbSet<Server> Servers { get; set; }
public DbSet<Channel> Channels { get; set; }
public DbSet<ServerMember> UsersToServerRelations { get; set; }

public DbSet<ServerMemberArchetype> ServerMemberArchetypes { get; set; }
public DbSet<Archetype> Archetypes { get; set; }
public DbSet<ChannelEntitlementOverwrite> ChannelEntitlementOverwrites { get; set; }


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ServerMemberArchetype>()
.HasKey(x => new { x.ServerMemberId, x.ArchetypeId });

modelBuilder.Entity<ServerMemberArchetype>()
.HasOne(x => x.ServerMember)
.WithMany(x => x.ServerMemberArchetypes)
.HasForeignKey(x => x.ServerMemberId);

modelBuilder.Entity<ServerMemberArchetype>()
.HasOne(x => x.Archetype)
.WithMany(x => x.ServerMemberRoles)
.HasForeignKey(x => x.ArchetypeId);

modelBuilder.Entity<Archetype>()
.HasOne(x => x.Server)
.WithMany(x => x.Archetypes)
.HasForeignKey(x => x.ServerId);

modelBuilder.Entity<Archetype>()
.Property(x => x.Colour)
.HasConversion<ColourConverter>();

modelBuilder.Entity<ServerMember>()
.HasOne(x => x.Server)
.WithMany(x => x.Users)
.HasForeignKey(x => x.ServerId);

modelBuilder.Entity<ServerMember>()
.HasOne(x => x.User)
.WithMany(x => x.ServerMembers)
.HasForeignKey(x => x.UserId);

modelBuilder.Entity<Channel>()
.HasOne(c => c.Server)
.WithMany(s => s.Channels)
.HasForeignKey(c => c.ServerId);

modelBuilder.Entity<Channel>()
.HasIndex(x => new { x.Id, x.ServerId });

modelBuilder.Entity<ChannelEntitlementOverwrite>()
.HasOne(cpo => cpo.Channel)
.WithMany(c => c.EntitlementOverwrites)
.HasForeignKey(cpo => cpo.ChannelId);

modelBuilder.Entity<ChannelEntitlementOverwrite>()
.HasOne(cpo => cpo.Archetype)
.WithMany()
.HasForeignKey(cpo => cpo.ArchetypeId)
.OnDelete(DeleteBehavior.Restrict);

modelBuilder.Entity<ChannelEntitlementOverwrite>()
.HasOne(cpo => cpo.ServerMember)
.WithMany()
.HasForeignKey(cpo => cpo.ServerMemberId)
.OnDelete(DeleteBehavior.Restrict);

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
if (!typeof(ArgonEntity).IsAssignableFrom(entityType.ClrType))
continue;
modelBuilder.Entity(entityType.ClrType)
.HasQueryFilter(GetSoftDeleteFilter(entityType.ClrType));
}

modelBuilder.Entity<User>().HasData(new User
{
Username = "system",
DisplayName = "System",
Email = "[email protected]",
Id = User.SystemUser,
PasswordDigest = null
});

modelBuilder.Entity<Server>().HasData(new Server
{
Name = "system_server",
CreatorId = User.SystemUser,
Id = Server.DefaultSystemServer
});

modelBuilder.Entity<Archetype>().HasData([
new Archetype
{
Id = Archetype.DefaultArchetype_Everyone,
Colour = Color.Gray,
CreatorId = User.SystemUser,
Entitlement = ArgonEntitlement.BaseMember,
ServerId = Server.DefaultSystemServer,
Name = "everyone",
IsLocked = false,
IsMentionable = true,
IsHidden = false,
Description = "Default role for everyone in this server",
CreatedAt = DateTime.UtcNow
}
]);

modelBuilder.Entity<Archetype>().HasData([
new Archetype
{
Id = Archetype.DefaultArchetype_Owner,
Colour = Color.Gray,
CreatorId = User.SystemUser,
Entitlement = ArgonEntitlement.Administrator,
ServerId = Server.DefaultSystemServer,
Name = "owner",
IsLocked = true,
IsMentionable = false,
IsHidden = true,
Description = "Default role for owner in this server",
CreatedAt = DateTime.UtcNow
}
]);
}

private static LambdaExpression GetSoftDeleteFilter(Type type)
{
var parameter = Expression.Parameter(type, "e");
var isDeletedProperty = Expression.Property(parameter, nameof(ArgonEntity.IsDeleted));
var notDeleted = Expression.Not(isDeletedProperty);
return Expression.Lambda(notDeleted, parameter);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.AddInterceptors(new TimeStampAndSoftDeleteInterceptor());
base.OnConfiguring(optionsBuilder);
}
}
53 changes: 0 additions & 53 deletions src/Argon.Api/Entities/Channel.cs

This file was deleted.

61 changes: 0 additions & 61 deletions src/Argon.Api/Entities/Server.cs

This file was deleted.

87 changes: 0 additions & 87 deletions src/Argon.Api/Entities/User.cs

This file was deleted.

Loading
Loading