PIbd-21_Pyatakov_KM_Markov_.../UniversityDataBaseImplemet/Models/Stream.cs
2023-05-20 05:46:02 +04:00

128 lines
5.0 KiB
C#

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> EducationGroupStream { get; set; } = new();
[ForeignKey("StreamId")]
public virtual List<StudentStream> StreamStudents { get; set; } = new();
public virtual User User { get; set; }
private List<StudentViewModel>? _studentStream = null;
private List<EducationGroupViewModel>? _educationGroupStream = null;
[NotMapped]
public List<StudentViewModel> StudentStream
{
get
{
if (_studentStream == null)
{
_studentStream = StreamStudents.Select(x => new StudentViewModel(x.Student)).ToList();
}
return _studentStream;
}
}
public List<EducationGroupViewModel> 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<int> currentStudents = documentStudents.Select(x => x.StudentId).ToList();
List<int> 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<int> currentGroups = documentGroups.Select(x => x.EducationGroupId).ToList();
List<int> 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
};
}
}