PIbd-21_Pyatakov_KM_Markov_.../UniversityDataBaseImplemet/Models/Document.cs

139 lines
5.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 UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityModels.Models;
namespace UniversityDataBaseImplemet.Models
{
public class Document : IDocumentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime Date { get; set; }
[Required]
public int UserId { get; set; }
[ForeignKey("DocumentId")]
public virtual List<StudentDocument> Students { get; set; } = new();
[ForeignKey("DocumentId")]
public virtual List<EducationGroupDocument> EducationGroupDocument { get; set; } = new();
public virtual User User { get; set; }
private List<StudentViewModel>? _documentStudents = null;
[NotMapped]
public List<StudentViewModel> DocumentStudents
{
get
{
if (_documentStudents == null)
{
_documentStudents = Students.Select(x => new StudentViewModel(x.Student)).ToList();
}
return _documentStudents;
}
}
private List<EducationGroupViewModel>? _documentEdGroups = null;
[NotMapped]
public List<EducationGroupViewModel> DocumentEdGroups
{
get
{
if (_documentEdGroups == null)
{
_documentEdGroups = EducationGroupDocument.Select(x => new EducationGroupViewModel(x.EducationGroup)).ToList();
}
return _documentEdGroups;
}
}
public static Document? Create(Database context, DocumentBindingModel? model)
{
if (model == null)
{
return null;
}
return new Document()
{
Id = model.Id,
Name = model.Name,
Date = model.Date,
UserId = model.UserId,
Students = model.DocumentStudents.Select(x => new StudentDocument()
{
Student = context.Students.First(y => y.Id == x.Id),
}).ToList()
};
}
public void Update(DocumentBindingModel model)
{
if(model == null)
{
return;
}
Name = model.Name;
Date = model.Date;
UserId = model.UserId;
}
public void UpdateStudents(Database context, DocumentBindingModel model)
{
var documentStudents = context.StudentDocuments.Where(x => x.DocumentId == model.Id).ToList();
List<int> currentStudents = documentStudents.Select(x => x.StudentId).ToList();
List<int> modelStudents = model.DocumentStudents.Select(x => x.Id).ToList();
if (documentStudents != null && documentStudents.Count > 0)
{
context.StudentDocuments.RemoveRange(documentStudents.Where(x => !modelStudents.Contains(x.StudentId)));
model.DocumentStudents.RemoveAll(x => currentStudents.Contains(x.Id));
context.SaveChanges();
}
var document = context.Documents.First(x => x.Id == Id);
foreach (var record in model.DocumentStudents)
{
context.StudentDocuments.Add(new StudentDocument
{
Document = document,
Student = context.Students.First(x => x.Id == record.Id),
});
context.SaveChanges();
}
_documentStudents = null;
}
public void UpdateGroups(Database context, DocumentBindingModel model)
{
var documentGroups = context.EducationGroupsDocuments.Where(x => x.DocumentId == model.Id).ToList();
List<int> currentGroups = documentGroups.Select(x => x.EducationGroupId).ToList();
List<int> modelGroups = model.DocumentGroups.Select(x => x.Id).ToList();
if (documentGroups != null && documentGroups.Count > 0)
{
context.EducationGroupsDocuments.RemoveRange(documentGroups.Where(x => !modelGroups.Contains(x.EducationGroupId)));
model.DocumentGroups.RemoveAll(x => currentGroups.Contains(x.Id));
context.SaveChanges();
}
var document = context.Documents.First(x => x.Id == Id);
foreach (var record in model.DocumentGroups)
{
context.EducationGroupsDocuments.Add(new EducationGroupDocument
{
Document = document,
EducationGroup = context.EducationGroups.First(x => x.Id == record.Id),
});
context.SaveChanges();
}
_documentEdGroups = null;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Date = Date,
UserId = UserId,
DocumentStudents = DocumentStudents,
DocumentEdGroups = DocumentEdGroups,
};
}
}