2023-04-08 13:59:34 +04:00
|
|
|
|
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; }
|
2023-05-19 21:25:56 +04:00
|
|
|
|
private List<StudentViewModel>? _studentStream = null;
|
|
|
|
|
private List<EducationGroupViewModel>? _educationGroupStream = null;
|
2023-04-08 20:16:42 +04:00
|
|
|
|
[NotMapped]
|
2023-05-19 21:25:56 +04:00
|
|
|
|
public List<StudentViewModel> StudentStream
|
2023-04-08 20:16:42 +04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_studentStream == null)
|
|
|
|
|
{
|
2023-05-19 21:25:56 +04:00
|
|
|
|
_studentStream = StreamStudents.Select(x => new StudentViewModel(x.Student)).ToList();
|
2023-04-08 20:16:42 +04:00
|
|
|
|
}
|
|
|
|
|
return _studentStream;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-19 21:25:56 +04:00
|
|
|
|
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)
|
2023-04-08 13:59:34 +04:00
|
|
|
|
{
|
|
|
|
|
return new Stream()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Course = model.Course,
|
2023-05-19 21:25:56 +04:00
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
EducationGroupStream = model.GroupStream.Select(x => new EducationGroupStream()
|
|
|
|
|
{
|
|
|
|
|
EducationGroup = context.EducationGroups.First(y => y.Id == x.Id),
|
|
|
|
|
}).ToList()
|
2023-04-08 13:59:34 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(StreamBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Course = model.Course;
|
|
|
|
|
UserId = model.UserId;
|
|
|
|
|
}
|
2023-04-08 20:16:42 +04:00
|
|
|
|
public void UpdateStreamStudents(Database context, StreamBindingModel model)
|
|
|
|
|
{
|
2023-05-19 21:25:56 +04:00
|
|
|
|
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)
|
2023-04-08 20:16:42 +04:00
|
|
|
|
{
|
2023-05-19 21:25:56 +04:00
|
|
|
|
context.StudentStreams.RemoveRange(documentStudents.Where(x => !modelStudents.Contains(x.StreamId)));
|
|
|
|
|
model.StudentStream.RemoveAll(x => currentStudents.Contains(x.Id));
|
2023-04-08 20:16:42 +04:00
|
|
|
|
context.SaveChanges();
|
2023-05-19 21:25:56 +04:00
|
|
|
|
}
|
|
|
|
|
var document = context.Streams.First(x => x.Id == Id);
|
|
|
|
|
foreach (var record in model.StudentStream)
|
|
|
|
|
{
|
|
|
|
|
context.StudentStreams.Add(new StudentStream
|
2023-04-08 20:16:42 +04:00
|
|
|
|
{
|
2023-05-19 21:25:56 +04:00
|
|
|
|
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
|
2023-04-08 20:16:42 +04:00
|
|
|
|
{
|
2023-05-19 21:25:56 +04:00
|
|
|
|
Stream = document,
|
|
|
|
|
EducationGroup = context.EducationGroups.First(x => x.Id == record.Id),
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
2023-04-08 20:16:42 +04:00
|
|
|
|
}
|
2023-05-19 21:25:56 +04:00
|
|
|
|
_educationGroupStream = null;
|
2023-04-08 20:16:42 +04:00
|
|
|
|
}
|
2023-05-20 05:46:02 +04:00
|
|
|
|
public StreamViewModel GetViewModel => new StreamViewModel()
|
2023-04-08 13:59:34 +04:00
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
UserId = UserId,
|
2023-05-19 21:25:56 +04:00
|
|
|
|
Course= Course,
|
|
|
|
|
StudentStream = StudentStream,
|
2023-05-20 05:46:02 +04:00
|
|
|
|
StreamEdGroups = StreamEdGroups
|
2023-04-08 13:59:34 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|