Fix the sql schema (#6)

Reviewed-on: #6
This commit is contained in:
Polianin Nikita 2024-01-26 07:48:13 +03:00
commit 2d973eee3d
27 changed files with 138 additions and 191 deletions

View File

@ -3,7 +3,7 @@ using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
public interface IDayDbContext : IDbContextBase public interface IDisciplineDbContext : IDbContextBase
{ {
DbSet<Day> Days { get; set; } DbSet<Discipline> Disciplines { get; set; }
} }

View File

@ -3,7 +3,7 @@ using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule; namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
public interface IProfessorToLessonDbContext : IDbContextBase public interface ILessonAssociationDbContext : IDbContextBase
{ {
DbSet<ProfessorToLesson> ProfessorToLessons { get; set; } DbSet<LessonAssociation> LessonAssociations { get; set; }
} }

View File

@ -1,9 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
public interface ILessonToTypeOfOccupationDbContext : IDbContextBase
{
DbSet<LessonToTypeOfOccupation> LessonToTypeOfOccupations { get; set; }
}

View File

@ -1,15 +0,0 @@
using System;
namespace Mirea.Api.DataAccess.Domain.Schedule;
public class Day
{
public int Id { get; set; }
public DayOfWeek Index { get; set; }
public int PairNumber { get; set; }
public int LessonId { get; set; }
public Lesson? Lesson { get; set; }
public int GroupId { get; set; }
public Group? Group { get; set; }
}

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Domain.Schedule;
public class Discipline
{
public int Id { get; set; }
public required string Name { get; set; }
public List<Lesson>? Lessons { get; set; }
}

View File

@ -9,5 +9,6 @@ public class Faculty
public int? CampusId { get; set; } public int? CampusId { get; set; }
public Campus? Campus { get; set; } public Campus? Campus { get; set; }
public List<Group>? Groups { get; set; } public List<Group>? Groups { get; set; }
} }

View File

@ -9,5 +9,6 @@ public class Group
public int? FacultyId { get; set; } public int? FacultyId { get; set; }
public Faculty? Faculty { get; set; } public Faculty? Faculty { get; set; }
public List<Day>? Days { get; set; }
public List<Lesson>? Lessons { get; set; }
} }

View File

@ -7,7 +7,8 @@ public class LectureHall
public int Id { get; set; } public int Id { get; set; }
public required string Name { get; set; } public required string Name { get; set; }
public List<ProfessorToLesson>? ProfessorToLessons { get; set; }
public int CampusId { get; set; } public int CampusId { get; set; }
public Campus? Campus { get; set; } public Campus? Campus { get; set; }
public List<LessonAssociation>? LessonAssociations { get; set; }
} }

View File

@ -1,4 +1,5 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
namespace Mirea.Api.DataAccess.Domain.Schedule; namespace Mirea.Api.DataAccess.Domain.Schedule;
@ -6,9 +7,15 @@ public class Lesson
{ {
public int Id { get; set; } public int Id { get; set; }
public bool IsEven { get; set; } public bool IsEven { get; set; }
public required string Discipline { get; set; } public DayOfWeek DayOfWeek { get; set; }
public int PairNumber { get; set; }
public List<LessonToTypeOfOccupation>? TypeOfOccupations { get; set; } public int GroupId { get; set; }
public List<ProfessorToLesson>? ProfessorToLesson { get; set; } public Group? Group { get; set; }
public Day? Day { get; set; } public int TypeOfOccupationId { get; set; }
public TypeOfOccupation? TypeOfOccupation { get; set; }
public int DisciplineId { get; set; }
public Discipline? Discipline { get; set; }
public List<LessonAssociation>? LessonAssociations { get; set; }
} }

View File

@ -1,11 +1,10 @@
namespace Mirea.Api.DataAccess.Domain.Schedule; namespace Mirea.Api.DataAccess.Domain.Schedule;
public class ProfessorToLesson public class LessonAssociation
{ {
public int Id { get; set; } public int Id { get; set; }
public string? LinkToMeet { get; set; } public string? LinkToMeet { get; set; }
public int LessonId { get; set; } public int LessonId { get; set; }
public Lesson? Lesson { get; set; } public Lesson? Lesson { get; set; }
public int? ProfessorId { get; set; } public int? ProfessorId { get; set; }

View File

@ -1,11 +0,0 @@
namespace Mirea.Api.DataAccess.Domain.Schedule;
public class LessonToTypeOfOccupation
{
public int Id { get; set; }
public int LessonId { get; set; }
public Lesson? Lesson { get; set; }
public int TypeOfOccupationId { get; set; }
public TypeOfOccupation? TypeOfOccupation { get; set; }
}

View File

@ -8,5 +8,5 @@ public class Professor
public required string Name { get; set; } public required string Name { get; set; }
public string? AltName { get; set; } public string? AltName { get; set; }
public List<ProfessorToLesson>? ProfessorToLesson { get; set; } public List<LessonAssociation>? LessonAssociations { get; set; }
} }

View File

@ -8,5 +8,5 @@ public class TypeOfOccupation
public required string ShortName { get; set; } public required string ShortName { get; set; }
public string? FullName { get; set; } public string? FullName { get; set; }
public List<LessonToTypeOfOccupation>? Lessons { get; set; } public List<Lesson>? Lessons { get; set; }
} }

View File

@ -5,13 +5,13 @@ using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
namespace Mirea.Api.DataAccess.Persistence.Contexts.Schedule; namespace Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
public class DayDbContext(DbContextOptions<DayDbContext> options) : DbContext(options), IDayDbContext public class DisciplineDbContext(DbContextOptions<DisciplineDbContext> options) : DbContext(options), IDisciplineDbContext
{ {
public DbSet<Day> Days { get; set; } = null!; public DbSet<Discipline> Disciplines { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new DayConfiguration()); modelBuilder.ApplyConfiguration(new DisciplineConfiguration());
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }
} }

View File

@ -5,13 +5,13 @@ using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
namespace Mirea.Api.DataAccess.Persistence.Contexts.Schedule; namespace Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
public class ProfessorToLessonDbContext(DbContextOptions<ProfessorToLessonDbContext> options) : DbContext(options), IProfessorToLessonDbContext public class LessonAssociationDbContext(DbContextOptions<LessonAssociationDbContext> options) : DbContext(options), ILessonAssociationDbContext
{ {
public DbSet<ProfessorToLesson> ProfessorToLessons { get; set; } = null!; public DbSet<LessonAssociation> LessonAssociations { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new ProfessorToLessonConfiguration()); modelBuilder.ApplyConfiguration(new LessonAssociationConfiguration());
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }
} }

View File

@ -1,17 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Mirea.Api.DataAccess.Application.Interfaces.DbContexts.Schedule;
using Mirea.Api.DataAccess.Domain.Schedule;
using Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
namespace Mirea.Api.DataAccess.Persistence.Contexts.Schedule;
public class LessonToTypeOfOccupationDbContext(DbContextOptions<LessonToTypeOfOccupationDbContext> options) : DbContext(options), ILessonToTypeOfOccupationDbContext
{
public DbSet<LessonToTypeOfOccupation> LessonToTypeOfOccupations { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new LessonToTypeOfOccupationConfiguration());
base.OnModelCreating(modelBuilder);
}
}

View File

@ -1,32 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
public class DayConfiguration : IEntityTypeConfiguration<Day>
{
public void Configure(EntityTypeBuilder<Day> builder)
{
builder.ToTable(nameof(Day));
builder.HasKey(d => d.Id);
builder.HasIndex(d => d.Id).IsUnique();
builder.Property(d => d.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(d => d.PairNumber).HasColumnType("INTEGER").IsRequired();
builder.Property(d => d.Index).HasColumnType("INTEGER").IsRequired();
builder.Property(d => d.GroupId).HasColumnType("INTEGER").IsRequired();
builder.Property(d => d.LessonId).HasColumnType("INTEGER").IsRequired();
builder
.HasOne(d => d.Group)
.WithMany(g => g.Days)
.HasForeignKey(d => d.GroupId)
.OnDelete(DeleteBehavior.Restrict);
builder
.HasOne(d => d.Lesson)
.WithOne(l => l.Day)
.HasForeignKey<Day>(d => d.LessonId)
.OnDelete(DeleteBehavior.Restrict);
}
}

View File

@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
public class DisciplineConfiguration : IEntityTypeConfiguration<Discipline>
{
public void Configure(EntityTypeBuilder<Discipline> builder)
{
builder.ToTable(nameof(Discipline));
builder.HasKey(d => d.Id);
builder.HasIndex(d => d.Id).IsUnique();
builder.Property(d => d.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(d => d.Name).HasColumnType("TEXT").HasMaxLength(256).IsRequired();
}
}

View File

@ -15,6 +15,8 @@ public class FacultyConfiguration : IEntityTypeConfiguration<Faculty>
builder.Property(f => f.Name).HasColumnType("TEXT").IsRequired().HasMaxLength(256); builder.Property(f => f.Name).HasColumnType("TEXT").IsRequired().HasMaxLength(256);
builder.Property(f => f.CampusId).HasColumnType("INTEGER");
builder builder
.HasOne(f => f.Campus) .HasOne(f => f.Campus)
.WithMany(c => c.Faculties) .WithMany(c => c.Faculties)

View File

@ -13,13 +13,13 @@ public class GroupConfiguration : IEntityTypeConfiguration<Group>
builder.HasIndex(g => g.Id).IsUnique(); builder.HasIndex(g => g.Id).IsUnique();
builder.Property(g => g.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd(); builder.Property(g => g.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(g => g.FacultyId).HasColumnType("INTEGER").IsRequired(); builder.Property(g => g.FacultyId).HasColumnType("INTEGER");
builder.Property(g => g.Name).HasColumnType("TEXT").IsRequired().HasMaxLength(64); builder.Property(g => g.Name).HasColumnType("TEXT").IsRequired().HasMaxLength(64);
builder builder
.HasOne(g => g.Faculty) .HasOne(g => g.Faculty)
.WithMany(u => u.Groups) .WithMany(u => u.Groups)
.HasForeignKey(d => d.FacultyId) .HasForeignKey(d => d.FacultyId)
.OnDelete(DeleteBehavior.Restrict); .OnDelete(DeleteBehavior.SetNull);
} }
} }

View File

@ -20,6 +20,6 @@ public class LectureHallConfiguration : IEntityTypeConfiguration<LectureHall>
.HasOne(l => l.Campus) .HasOne(l => l.Campus)
.WithMany(c => c.LectureHalls) .WithMany(c => c.LectureHalls)
.HasForeignKey(d => d.CampusId) .HasForeignKey(d => d.CampusId)
.OnDelete(DeleteBehavior.Cascade); .OnDelete(DeleteBehavior.Restrict);
} }
} }

View File

@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
public class LessonAssociationConfiguration : IEntityTypeConfiguration<LessonAssociation>
{
public void Configure(EntityTypeBuilder<LessonAssociation> builder)
{
builder.ToTable(nameof(LessonAssociation));
builder.HasKey(l => l.Id);
builder.HasIndex(l => l.Id).IsUnique();
builder.Property(l => l.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(l => l.LinkToMeet).HasColumnType("TEXT").HasMaxLength(512);
builder.Property(l => l.LessonId).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.ProfessorId).HasColumnType("INTEGER");
builder.Property(l => l.LectureHallId).HasColumnType("INTEGER");
builder
.HasOne(l => l.Lesson)
.WithMany(d => d.LessonAssociations)
.HasForeignKey(l => l.LessonId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(l => l.Professor)
.WithMany(p => p.LessonAssociations)
.HasForeignKey(l => l.ProfessorId)
.OnDelete(DeleteBehavior.SetNull);
builder
.HasOne(l => l.LectureHall)
.WithMany(l => l.LessonAssociations)
.HasForeignKey(l => l.LectureHallId)
.OnDelete(DeleteBehavior.SetNull);
}
}

View File

@ -13,8 +13,31 @@ public class LessonConfiguration : IEntityTypeConfiguration<Lesson>
builder.HasIndex(l => l.Id).IsUnique(); builder.HasIndex(l => l.Id).IsUnique();
builder.Property(l => l.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd(); builder.Property(l => l.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(l => l.Discipline).HasColumnType("TEXT").IsRequired().HasMaxLength(256);
builder.Property(l => l.IsEven).HasColumnType("BIT").IsRequired(); builder.Property(l => l.IsEven).HasColumnType("BIT").IsRequired();
builder.Property(l => l.DayOfWeek).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.PairNumber).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.GroupId).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.TypeOfOccupationId).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.DisciplineId).HasColumnType("INTEGER").IsRequired();
builder
.HasOne(l => l.Group)
.WithMany(g => g.Lessons)
.HasForeignKey(d => d.GroupId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(l => l.TypeOfOccupation)
.WithMany(t => t.Lessons)
.HasForeignKey(d => d.TypeOfOccupationId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(l => l.Discipline)
.WithMany(d => d.Lessons)
.HasForeignKey(l => l.DisciplineId)
.OnDelete(DeleteBehavior.Cascade);
} }
} }

View File

@ -1,31 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
public class LessonToTypeOfOccupationConfiguration : IEntityTypeConfiguration<LessonToTypeOfOccupation>
{
public void Configure(EntityTypeBuilder<LessonToTypeOfOccupation> builder)
{
builder.ToTable(nameof(LessonToTypeOfOccupation));
builder.HasKey(l => l.Id);
builder.HasIndex(l => l.Id).IsUnique();
builder.Property(l => l.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(l => l.LessonId).HasColumnType("INTEGER").IsRequired();
builder.Property(l => l.TypeOfOccupationId).HasColumnType("INTEGER").IsRequired();
builder
.HasOne(l => l.Lesson)
.WithMany(l => l.TypeOfOccupations)
.HasForeignKey(l => l.LessonId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(l => l.TypeOfOccupation)
.WithMany(t => t.Lessons)
.HasForeignKey(l => l.TypeOfOccupationId)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -1,39 +0,0 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Mirea.Api.DataAccess.Domain.Schedule;
namespace Mirea.Api.DataAccess.Persistence.EntityTypeConfigurations.Schedule;
public class ProfessorToLessonConfiguration : IEntityTypeConfiguration<ProfessorToLesson>
{
public void Configure(EntityTypeBuilder<ProfessorToLesson> builder)
{
builder.ToTable(nameof(ProfessorToLesson));
builder.HasKey(p => p.Id);
builder.HasIndex(p => p.Id).IsUnique();
builder.Property(p => p.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(l => l.LinkToMeet).HasColumnType("TEXT").HasMaxLength(512);
builder.Property(p => p.LessonId).HasColumnType("INTEGER").IsRequired();
builder.Property(p => p.ProfessorId).HasColumnType("INTEGER");
builder.Property(l => l.LectureHallId).HasColumnType("INTEGER");
builder
.HasOne(p => p.LectureHall)
.WithMany(l => l.ProfessorToLessons)
.HasForeignKey(l => l.LectureHallId)
.OnDelete(DeleteBehavior.SetNull);
builder
.HasOne(p => p.Lesson)
.WithMany(l => l.ProfessorToLesson)
.HasForeignKey(p => p.LessonId)
.OnDelete(DeleteBehavior.Cascade);
builder
.HasOne(p => p.Professor)
.WithMany(p => p.ProfessorToLesson)
.HasForeignKey(p => p.ProfessorId)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@ -13,7 +13,7 @@ public class TypeOfOccupationConfiguration : IEntityTypeConfiguration<TypeOfOccu
builder.HasIndex(t => t.Id).IsUnique(); builder.HasIndex(t => t.Id).IsUnique();
builder.Property(t => t.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd(); builder.Property(t => t.Id).HasColumnType("INTEGER").IsRequired().ValueGeneratedOnAdd();
builder.Property(t => t.FullName).HasColumnType("TEXT").HasMaxLength(64);
builder.Property(t => t.ShortName).HasColumnType("TEXT").IsRequired().HasMaxLength(16); builder.Property(t => t.ShortName).HasColumnType("TEXT").IsRequired().HasMaxLength(16);
builder.Property(t => t.FullName).HasColumnType("TEXT").HasMaxLength(64);
} }
} }

View File

@ -6,29 +6,27 @@ namespace Mirea.Api.DataAccess.Persistence;
public class UberDbContext(DbContextOptions<UberDbContext> options) : DbContext(options) public class UberDbContext(DbContextOptions<UberDbContext> options) : DbContext(options)
{ {
public DbSet<Day> Days { get; set; } = null!; public DbSet<Campus> Campuses { get; set; } = null!;
public DbSet<Discipline> Disciplines { get; set; } = null!;
public DbSet<Faculty> Faculties { get; set; } = null!;
public DbSet<Group> Groups { get; set; } = null!; public DbSet<Group> Groups { get; set; } = null!;
public DbSet<LectureHall> LectureHalls { get; set; } = null!; public DbSet<LectureHall> LectureHalls { get; set; } = null!;
public DbSet<LessonAssociation> LessonAssociations { get; set; } = null!;
public DbSet<Lesson> Lessons { get; set; } = null!; public DbSet<Lesson> Lessons { get; set; } = null!;
public DbSet<Professor> Professors { get; set; } = null!; public DbSet<Professor> Professors { get; set; } = null!;
public DbSet<TypeOfOccupation> TypeOfOccupations { get; set; } = null!; public DbSet<TypeOfOccupation> TypeOfOccupations { get; set; } = null!;
public DbSet<Faculty> Faculties { get; set; } = null!;
public DbSet<Campus> Campuses { get; set; } = null!;
public DbSet<ProfessorToLesson> ProfessorToLessons { get; set; } = null!;
public DbSet<LessonToTypeOfOccupation> LessonToTypeOfOccupations { get; set; } = null!;
protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder)
{ {
modelBuilder.ApplyConfiguration(new DayConfiguration()); modelBuilder.ApplyConfiguration(new CampusConfiguration());
modelBuilder.ApplyConfiguration(new DisciplineConfiguration());
modelBuilder.ApplyConfiguration(new FacultyConfiguration());
modelBuilder.ApplyConfiguration(new GroupConfiguration()); modelBuilder.ApplyConfiguration(new GroupConfiguration());
modelBuilder.ApplyConfiguration(new LectureHallConfiguration()); modelBuilder.ApplyConfiguration(new LectureHallConfiguration());
modelBuilder.ApplyConfiguration(new LessonConfiguration()); modelBuilder.ApplyConfiguration(new LessonConfiguration());
modelBuilder.ApplyConfiguration(new ProfessorConfiguration()); modelBuilder.ApplyConfiguration(new ProfessorConfiguration());
modelBuilder.ApplyConfiguration(new LessonAssociationConfiguration());
modelBuilder.ApplyConfiguration(new TypeOfOccupationConfiguration()); modelBuilder.ApplyConfiguration(new TypeOfOccupationConfiguration());
modelBuilder.ApplyConfiguration(new FacultyConfiguration());
modelBuilder.ApplyConfiguration(new CampusConfiguration());
modelBuilder.ApplyConfiguration(new ProfessorToLessonConfiguration());
modelBuilder.ApplyConfiguration(new LessonToTypeOfOccupationConfiguration());
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
} }