егор хранилище закончил свою часть

This commit is contained in:
GokaPek 2024-04-23 23:03:32 +04:00
parent 7f5cd4b778
commit b51f7df1a3
5 changed files with 79 additions and 22 deletions

View File

@ -14,5 +14,6 @@ namespace UniversityContracts.BindingModels
public int TeacherId { get; set; } public int TeacherId { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty; public string Description { get; set; } = string.Empty;
public Dictionary<int, (IStudentModel, int)> StudentDisciplines { get; set; } = new();
} }
} }

View File

@ -16,5 +16,10 @@ namespace UniversityContracts.ViewModels
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
[DisplayName("Описание дисциплины")] [DisplayName("Описание дисциплины")]
public string Description { get; set; } = string.Empty; public string Description { get; set; } = string.Empty;
public Dictionary<int, IStudentModel> StudentDisciplines
{
get;
set;
} = new();
} }
} }

View File

@ -18,7 +18,7 @@ namespace UniversityDatabaseImplement.Implements
public DisciplineViewModel? Delete(DisciplineBindingModel model) public DisciplineViewModel? Delete(DisciplineBindingModel model)
{ {
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); var element = context.Disciplines.Include(x => x.Students).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null) if (element != null)
{ {
context.Disciplines.Remove(element); context.Disciplines.Remove(element);
@ -35,7 +35,7 @@ namespace UniversityDatabaseImplement.Implements
return null; return null;
} }
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
return context.Disciplines return context.Disciplines.Include(x => x.Students).ThenInclude(x => x.Student)
.Include(x => x.Teacher) .Include(x => x.Teacher)
.FirstOrDefault(x => .FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
@ -50,6 +50,8 @@ namespace UniversityDatabaseImplement.Implements
} }
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
return context.Disciplines return context.Disciplines
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.Where(x => x.Name.Contains(model.Name) || x.Description.Contains(model.Description) || x.Id == model.Id || x.TeacherId == model.TeacherId) .Where(x => x.Name.Contains(model.Name) || x.Description.Contains(model.Description) || x.Id == model.Id || x.TeacherId == model.TeacherId)
.Include(x => x.Teacher) .Include(x => x.Teacher)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
@ -60,6 +62,9 @@ namespace UniversityDatabaseImplement.Implements
{ {
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
return context.Disciplines return context.Disciplines
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.ToList()
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
} }
@ -67,7 +72,7 @@ namespace UniversityDatabaseImplement.Implements
public DisciplineViewModel? Insert(DisciplineBindingModel model) public DisciplineViewModel? Insert(DisciplineBindingModel model)
{ {
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
var newDiscipline = Discipline.Create(model); var newDiscipline = Discipline.Create(context, model);
if (newDiscipline == null) if (newDiscipline == null)
{ {
return null; return null;
@ -80,6 +85,9 @@ namespace UniversityDatabaseImplement.Implements
public DisciplineViewModel? Update(DisciplineBindingModel model) public DisciplineViewModel? Update(DisciplineBindingModel model)
{ {
using var context = new UniversityDatabase(); using var context = new UniversityDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id); var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
if (discipline == null) if (discipline == null)
{ {
@ -87,7 +95,15 @@ namespace UniversityDatabaseImplement.Implements
} }
discipline.Update(model); discipline.Update(model);
context.SaveChanges(); context.SaveChanges();
discipline.UpdateStudents(context, model);
transaction.Commit();
return discipline.GetViewModel; return discipline.GetViewModel;
} }
catch
{
transaction.Rollback();
throw;
}
}
} }
} }

View File

@ -21,23 +21,23 @@ namespace UniversityDatabaseImplement.Models
[Required] [Required]
public string Description { get; private set; } = string.Empty; public string Description { get; private set; } = string.Empty;
public virtual Teacher Teacher { get; set; } = new(); public virtual Teacher Teacher { get; set; } = new();
private Dictionary<int, IStudentModel>? _studentDisciplines = null;
[NotMapped]
public Dictionary<int, IStudentModel> StudentDisciplines
{
get
{
if (_studentDisciplines == null)
{
_studentDisciplines = Students
.ToDictionary(recPC => recPC.StudentId, recPC => recPC.Student as IStudentModel);
}
return _studentDisciplines;
}
}
[ForeignKey("DisciplineId")] [ForeignKey("DisciplineId")]
public virtual List<StudentDiscipline> StudentDisciplines { get; set; } = new(); public virtual List<StudentDiscipline> Students { get; set; } = new();
public static Discipline? Create(DisciplineBindingModel model) public static Discipline Create(UniversityDatabase context, DisciplineBindingModel model)
{
if (model == null)
{
return null;
}
return new Discipline()
{
Id = model.Id,
TeacherId = model.TeacherId,
Name = model.Name,
Description = model.Description,
};
}
public static Discipline Create(DisciplineViewModel model)
{ {
return new Discipline() return new Discipline()
{ {
@ -45,6 +45,11 @@ namespace UniversityDatabaseImplement.Models
TeacherId = model.TeacherId, TeacherId = model.TeacherId,
Name = model.Name, Name = model.Name,
Description = model.Description, Description = model.Description,
Students = model.StudentDisciplines.Select(x => new
StudentDiscipline
{
Student = context.Students.First(y => y.Id == x.Key)
}).ToList()
}; };
} }
public void Update(DisciplineBindingModel model) public void Update(DisciplineBindingModel model)
@ -58,12 +63,41 @@ namespace UniversityDatabaseImplement.Models
Name = model.Name; Name = model.Name;
Description = model.Description; Description = model.Description;
} }
public void UpdateStudents(UniversityDatabase context,
DisciplineBindingModel model)
{
var studentDisciplines = context.StudentDisciplines.Where(rec => rec.DisciplineId == model.Id).ToList();
if (studentDisciplines != null && studentDisciplines.Count > 0)
{ // удалили те, которых нет в модели
context.StudentDisciplines.RemoveRange(studentDisciplines.Where(rec
=> !model.StudentDisciplines.ContainsKey(rec.StudentId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateStudent in studentDisciplines)
{
model.StudentDisciplines.Remove(updateStudent.StudentId);
}
context.SaveChanges();
}
var discipline = context.Disciplines.First(x => x.Id == Id);
foreach (var pc in model.StudentDisciplines)
{
context.StudentDisciplines.Add(new StudentDiscipline
{
Discipline = discipline,
Student = context.Students.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_studentDisciplines = null;
}
public DisciplineViewModel GetViewModel => new() public DisciplineViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,
TeacherId = TeacherId, TeacherId = TeacherId,
Name = Name, Name = Name,
Description = Description, Description = Description,
StudentDisciplines = StudentDisciplines,
}; };
} }
} }

View File

@ -24,5 +24,6 @@ namespace UniversityDatabaseImplement
public virtual DbSet<Teacher> Teachers { set; get; } public virtual DbSet<Teacher> Teachers { set; get; }
public virtual DbSet<Discipline> Disciplines { set; get; } public virtual DbSet<Discipline> Disciplines { set; get; }
public virtual DbSet<Statement> Statements { set; get; } public virtual DbSet<Statement> Statements { set; get; }
public virtual DbSet<StudentDiscipline> StudentDisciplines { set; get; }
} }
} }