Compare commits

..

No commits in common. "51ed3d3af4f52869ec528e79425ffffcf27c4f00" and "c768b36b5c93efabf9f7adb97da42de8acd7b029" have entirely different histories.

5 changed files with 25 additions and 82 deletions

View File

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

View File

@ -16,10 +16,5 @@ namespace UniversityContracts.ViewModels
public string Name { get; set; } = string.Empty;
[DisplayName("Описание дисциплины")]
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)
{
using var context = new UniversityDatabase();
var element = context.Disciplines.Include(x => x.Students).FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Disciplines.Remove(element);
@ -35,7 +35,7 @@ namespace UniversityDatabaseImplement.Implements
return null;
}
using var context = new UniversityDatabase();
return context.Disciplines.Include(x => x.Students).ThenInclude(x => x.Student)
return context.Disciplines
.Include(x => x.Teacher)
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel;
@ -50,8 +50,6 @@ namespace UniversityDatabaseImplement.Implements
}
using var context = new UniversityDatabase();
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)
.Include(x => x.Teacher)
.Select(x => x.GetViewModel)
@ -62,9 +60,6 @@ namespace UniversityDatabaseImplement.Implements
{
using var context = new UniversityDatabase();
return context.Disciplines
.Include(x => x.Students)
.ThenInclude(x => x.Student)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -72,7 +67,7 @@ namespace UniversityDatabaseImplement.Implements
public DisciplineViewModel? Insert(DisciplineBindingModel model)
{
using var context = new UniversityDatabase();
var newDiscipline = Discipline.Create(context, model);
var newDiscipline = Discipline.Create(model);
if (newDiscipline == null)
{
return null;
@ -85,9 +80,6 @@ namespace UniversityDatabaseImplement.Implements
public DisciplineViewModel? Update(DisciplineBindingModel model)
{
using var context = new UniversityDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var discipline = context.Disciplines.FirstOrDefault(x => x.Id == model.Id);
if (discipline == null)
{
@ -95,15 +87,7 @@ namespace UniversityDatabaseImplement.Implements
}
discipline.Update(model);
context.SaveChanges();
discipline.UpdateStudents(context, model);
transaction.Commit();
return discipline.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -21,23 +21,23 @@ namespace UniversityDatabaseImplement.Models
[Required]
public string Description { get; private set; } = string.Empty;
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")]
public virtual List<StudentDiscipline> Students { get; set; } = new();
public static Discipline Create(UniversityDatabase context, DisciplineBindingModel model)
public virtual List<StudentDiscipline> StudentDisciplines { get; set; } = new();
public static Discipline? Create(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()
{
@ -45,11 +45,6 @@ namespace UniversityDatabaseImplement.Models
TeacherId = model.TeacherId,
Name = model.Name,
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)
@ -63,41 +58,12 @@ namespace UniversityDatabaseImplement.Models
Name = model.Name;
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()
{
Id = Id,
TeacherId = TeacherId,
Name = Name,
Description = Description,
StudentDisciplines = StudentDisciplines,
};
}
}

View File

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