using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using UniversityContracts.BindingModels; using UniversityContracts.ViewModels; using UniversityModels.Models; namespace UniversityDataBaseImplemet.Models { public class Stream : IStreamModel { [Required] public string Name { get; set; } = string.Empty; [Required] public int Course { get; set; } [Required] public int UserId { get; set; } public int Id { get; private set; } [ForeignKey("StreamId")] public virtual List EducationGroupStream { get; set; } = new(); [ForeignKey("StreamId")] public virtual List StreamStudents { get; set; } = new(); public virtual User User { get; set; } private List? _studentStream = null; private List? _educationGroupStream = null; [NotMapped] public List StudentStream { get { if (_studentStream == null) { _studentStream = StreamStudents.Select(x => new StudentViewModel(x.Student)).ToList(); } return _studentStream; } } public List StreamEdGroups { get { if (_educationGroupStream == null) { _educationGroupStream = EducationGroupStream.Select(x => new EducationGroupViewModel(x.EducationGroup)).ToList(); } return _educationGroupStream; } } public static Stream Create(Database context, StreamBindingModel model) { return new Stream() { Id = model.Id, Name = model.Name, Course = model.Course, UserId = model.UserId, EducationGroupStream = model.GroupStream.Select(x => new EducationGroupStream() { EducationGroup = context.EducationGroups.First(y => y.Id == x.Id), }).ToList() }; } public void Update(StreamBindingModel model) { Name = model.Name; Course = model.Course; UserId = model.UserId; } public void UpdateStreamStudents(Database context, StreamBindingModel model) { var documentStudents = context.StudentStreams.Where(x => x.StreamId == model.Id).ToList(); List currentStudents = documentStudents.Select(x => x.StudentId).ToList(); List modelStudents = model.StudentStream.Select(x => x.Id).ToList(); if (documentStudents != null && documentStudents.Count > 0) { context.StudentStreams.RemoveRange(documentStudents.Where(x => !modelStudents.Contains(x.StreamId))); model.StudentStream.RemoveAll(x => currentStudents.Contains(x.Id)); context.SaveChanges(); } var document = context.Streams.First(x => x.Id == Id); foreach (var record in model.StudentStream) { context.StudentStreams.Add(new StudentStream { Stream = document, Student = context.Students.First(x => x.Id == record.Id), }); context.SaveChanges(); } _studentStream = null; } public void UpdateGroups(Database context, StreamBindingModel model) { var documentGroups = context.EducationGroupsStreams.Where(x => x.StreamId == model.Id).ToList(); List currentGroups = documentGroups.Select(x => x.EducationGroupId).ToList(); List modelGroups = model.GroupStream.Select(x => x.Id).ToList(); if (documentGroups != null && documentGroups.Count > 0) { context.EducationGroupsStreams.RemoveRange(documentGroups.Where(x => !modelGroups.Contains(x.EducationGroupId))); model.GroupStream.RemoveAll(x => currentGroups.Contains(x.Id)); context.SaveChanges(); } var document = context.Streams.First(x => x.Id == Id); foreach (var record in model.GroupStream) { context.EducationGroupsStreams.Add(new EducationGroupStream { Stream = document, EducationGroup = context.EducationGroups.First(x => x.Id == record.Id), }); context.SaveChanges(); } _educationGroupStream = null; } public StreamViewModel GetViewModel => new StreamViewModel() { Id = Id, Name = Name, UserId = UserId, Course= Course, StudentStream = StudentStream, StreamEdGroups = StreamEdGroups }; } }