егор хранилище закончил свою часть
This commit is contained in:
parent
7f5cd4b778
commit
b51f7df1a3
@ -14,5 +14,6 @@ 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();
|
||||
}
|
||||
}
|
||||
|
@ -16,5 +16,10 @@ 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();
|
||||
}
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace UniversityDatabaseImplement.Implements
|
||||
public DisciplineViewModel? Delete(DisciplineBindingModel model)
|
||||
{
|
||||
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)
|
||||
{
|
||||
context.Disciplines.Remove(element);
|
||||
@ -35,7 +35,7 @@ namespace UniversityDatabaseImplement.Implements
|
||||
return null;
|
||||
}
|
||||
using var context = new UniversityDatabase();
|
||||
return context.Disciplines
|
||||
return context.Disciplines.Include(x => x.Students).ThenInclude(x => x.Student)
|
||||
.Include(x => x.Teacher)
|
||||
.FirstOrDefault(x =>
|
||||
(!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();
|
||||
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)
|
||||
@ -60,6 +62,9 @@ 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();
|
||||
}
|
||||
@ -67,7 +72,7 @@ namespace UniversityDatabaseImplement.Implements
|
||||
public DisciplineViewModel? Insert(DisciplineBindingModel model)
|
||||
{
|
||||
using var context = new UniversityDatabase();
|
||||
var newDiscipline = Discipline.Create(model);
|
||||
var newDiscipline = Discipline.Create(context, model);
|
||||
if (newDiscipline == null)
|
||||
{
|
||||
return null;
|
||||
@ -80,6 +85,9 @@ 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)
|
||||
{
|
||||
@ -87,7 +95,15 @@ namespace UniversityDatabaseImplement.Implements
|
||||
}
|
||||
discipline.Update(model);
|
||||
context.SaveChanges();
|
||||
discipline.UpdateStudents(context, model);
|
||||
transaction.Commit();
|
||||
return discipline.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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> 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)
|
||||
public virtual List<StudentDiscipline> Students { get; set; } = new();
|
||||
public static Discipline Create(UniversityDatabase context, DisciplineBindingModel model)
|
||||
{
|
||||
return new Discipline()
|
||||
{
|
||||
@ -45,6 +45,11 @@ 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)
|
||||
@ -58,12 +63,41 @@ 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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -24,5 +24,6 @@ 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; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user