143 lines
4.3 KiB
C#
143 lines
4.3 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.ComponentModel.DataAnnotations.Schema;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using TaskTrackerContracts.BindingModels;
|
||
using TaskTrackerContracts.ViewModels;
|
||
using TaskTrackerDataModels.Models;
|
||
|
||
namespace TaskTrackerDatabase.Models
|
||
{
|
||
public class Direction : IDirectionModel
|
||
{
|
||
[Required]
|
||
public int Id { get; set; }
|
||
|
||
[Required]
|
||
public string Name { get; set; } = string.Empty;
|
||
|
||
|
||
private Dictionary<int, IStudentModel>? _directionStudents = null;
|
||
[NotMapped]
|
||
public Dictionary<int, IStudentModel> DirectionStudents
|
||
{
|
||
get
|
||
{
|
||
if (_directionStudents == null)
|
||
{
|
||
_directionStudents = Students
|
||
.ToDictionary(recPC => recPC.StudentId, recPC =>
|
||
(recPC.Student as IStudentModel));
|
||
}
|
||
return _directionStudents;
|
||
}
|
||
}
|
||
[ForeignKey("DirectionId")]
|
||
public virtual List<DirectionStudent> Students { get; set; } = new();
|
||
|
||
|
||
|
||
|
||
private Dictionary<int, ISubjectModel>? _directionSubjects = null;
|
||
[NotMapped]
|
||
public Dictionary<int, ISubjectModel> DirectionSubjects
|
||
{
|
||
get
|
||
{
|
||
if (_directionSubjects == null)
|
||
{
|
||
_directionSubjects = Subjects
|
||
.ToDictionary(recPC => recPC.SubjectId, recPC =>
|
||
(recPC.Subject as ISubjectModel));
|
||
}
|
||
return _directionSubjects;
|
||
}
|
||
}
|
||
[ForeignKey("DirectionId")]
|
||
public virtual List<DirectionSubject> Subjects { get; set; } = new();
|
||
|
||
public static Direction Create(TaskTrackerDatabase context, DirectionBindingModel model)
|
||
{
|
||
return new Direction()
|
||
{
|
||
Id = model.Id,
|
||
Name = model.Name,
|
||
Students = model.DirectionStudents.Select(x => new DirectionStudent
|
||
{
|
||
Student = context.Students.First(y => y.Id == x.Key)
|
||
}).ToList(),
|
||
Subjects = model.DirectionSubjects.Select(x => new DirectionSubject
|
||
{
|
||
Subject = context.Subjects.First(y => y.Id == x.Key)
|
||
}).ToList()
|
||
};
|
||
}
|
||
public void Update(DirectionBindingModel model)
|
||
{
|
||
Name = model.Name;
|
||
}
|
||
public DirectionViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
Name = Name,
|
||
DirectionStudents = DirectionStudents,
|
||
DirectionSubjects = DirectionSubjects
|
||
};
|
||
public void UpdateStudents(TaskTrackerDatabase context, DirectionBindingModel model)
|
||
{
|
||
var directionStudents = context.DirectionStudents.Where(rec => rec.DirectionId == model.Id).ToList();
|
||
if (directionStudents != null && DirectionStudents.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.DirectionStudents.RemoveRange(directionStudents.Where(rec => !model.DirectionStudents.ContainsKey(rec.StudentId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateStudents in directionStudents)
|
||
{
|
||
model.DirectionStudents.Remove(updateStudents.StudentId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var reinforced = context.Directions.First(x => x.Id == Id);
|
||
foreach (var rp in model.DirectionStudents)
|
||
{
|
||
context.DirectionStudents.Add(new DirectionStudent
|
||
{
|
||
Direction = reinforced,
|
||
Student = context.Students.First(x => x.Id == rp.Key)
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_directionStudents = null;
|
||
}
|
||
public void UpdateSubjects(TaskTrackerDatabase context, DirectionBindingModel model)
|
||
{
|
||
var directionSubjects = context.DirectionSubjects.Where(rec => rec.DirectionId == model.Id).ToList();
|
||
if (directionSubjects != null && DirectionSubjects.Count > 0)
|
||
{ // удалили те, которых нет в модели
|
||
context.DirectionSubjects.RemoveRange(directionSubjects.Where(rec => !model.DirectionSubjects.ContainsKey(rec.SubjectId)));
|
||
context.SaveChanges();
|
||
// обновили количество у существующих записей
|
||
foreach (var updateSubjects in directionSubjects)
|
||
{
|
||
model.DirectionSubjects.Remove(updateSubjects.SubjectId);
|
||
}
|
||
context.SaveChanges();
|
||
}
|
||
var reinforced = context.Directions.First(x => x.Id == Id);
|
||
foreach (var rp in model.DirectionSubjects)
|
||
{
|
||
context.DirectionSubjects.Add(new DirectionSubject
|
||
{
|
||
Direction = reinforced,
|
||
Subject = context.Subjects.First(x => x.Id == rp.Key)
|
||
});
|
||
context.SaveChanges();
|
||
}
|
||
_directionSubjects = null;
|
||
}
|
||
}
|
||
}
|