Refactor group existence checks and add group repo tests

Renamed IGroupsExist and related methods from Exists to Exist for consistency. Added integration tests for GroupRepository. Introduced entity configurations for ChatGroup, GroupAdmins, GroupInvitation, and updated GroupMembership configuration. Updated usages and tests to match new method names and improved equality checks for ChatGroup.
This commit is contained in:
Artemy
2025-07-07 20:49:20 +07:00
parent 42be589a26
commit a68feb4a17
12 changed files with 421 additions and 13 deletions
@@ -0,0 +1,31 @@
using Govor.Core.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Govor.Data.Configurations;
public class ChatGroupConfigurator : IEntityTypeConfiguration<ChatGroup>
{
public void Configure(EntityTypeBuilder<ChatGroup> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.Name).IsRequired().HasMaxLength(100);
builder.Property(e => e.Description).HasMaxLength(500);
builder.HasMany(e => e.Members)
.WithOne()
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(e => e.Admins)
.WithOne()
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(e => e.InviteCodes)
.WithOne()
.HasForeignKey(e => e.GroupId)
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,16 @@
using Govor.Core.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Govor.Data.Configurations;
public class GroupAdminsConfiguration : IEntityTypeConfiguration<GroupAdmins>
{
public void Configure(EntityTypeBuilder<GroupAdmins> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.UserId).IsRequired();
builder.Property(e => e.GroupId).IsRequired();
}
}
@@ -0,0 +1,26 @@
using Govor.Core.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Govor.Data.Configurations;
public class GroupInvitationConfiguration : IEntityTypeConfiguration<GroupInvitation>
{
public void Configure(EntityTypeBuilder<GroupInvitation> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.InvitationCode).IsRequired().HasMaxLength(200);
builder.Property(e => e.Description).HasMaxLength(500);
builder.Property(e => e.EndDate).IsRequired();
builder.Property(e => e.CreatedAt).IsRequired();
builder.HasOne(e => e.UserMaker)
.WithMany()
.HasForeignKey(e => e.UserMakerId)
.OnDelete(DeleteBehavior.Restrict);
builder.Ignore(e => e.GroupMemberships);
}
}
@@ -8,6 +8,16 @@ public class GroupMembershipConfiguration : IEntityTypeConfiguration<GroupMember
{
public void Configure(EntityTypeBuilder<GroupMembership> builder)
{
builder.HasKey(m => new { m.GroupId, m.UserId });
builder.HasKey(e => e.Id);
builder.Property(e => e.UserId).IsRequired();
builder.Property(e => e.GroupId).IsRequired();
builder.Property(e => e.InvitationId).IsRequired();
// Optional: можно добавить навигацию к GroupInvitation
builder.HasOne<GroupInvitation>()
.WithMany()
.HasForeignKey(e => e.InvitationId)
.OnDelete(DeleteBehavior.SetNull);
}
}
+4
View File
@@ -36,6 +36,10 @@ public class GovorDbContext(DbContextOptions<GovorDbContext> options) : DbContex
modelBuilder.ApplyConfiguration(new MediaAttachmentsConfiguration());
modelBuilder.ApplyConfiguration(new MessageViewConfiguration());
modelBuilder.ApplyConfiguration(new MediaFileConfiguration());
modelBuilder.ApplyConfiguration(new ChatGroupConfigurator());
modelBuilder.ApplyConfiguration(new GroupInvitationConfiguration());
modelBuilder.ApplyConfiguration(new GroupMembershipConfiguration());
modelBuilder.ApplyConfiguration(new GroupAdminsConfiguration());
base.OnModelCreating(modelBuilder);
}
+2 -2
View File
@@ -139,12 +139,12 @@ public class GroupRepository : IGroupsRepository
}
}
public bool Exists(Guid groupId)
public bool Exist(Guid groupId)
{
return _context.ChatGroups.Any(g => g.Id == groupId);
}
public bool Exists(ChatGroup chatGroup)
public bool Exist(ChatGroup chatGroup)
{
return _context.ChatGroups.Any(g => g.Id == chatGroup.Id &&
g.IsChannel == chatGroup.IsChannel &&