2023-04-07 13:24:39 +04:00
|
|
|
|
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();
|
2023-04-08 13:59:34 +04:00
|
|
|
|
[ForeignKey("DocumentId")]
|
|
|
|
|
public virtual List<EducationGroupDocument> EducationGroupDocument { get; set; } = new();
|
|
|
|
|
public virtual User User { get; set; }
|
2023-04-08 20:16:42 +04:00
|
|
|
|
private Dictionary<int, IStudentModel>? _studentDocument = null;
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IStudentModel> StudentDocument
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_studentDocument == null)
|
|
|
|
|
{
|
|
|
|
|
_studentDocument = Students.ToDictionary(rec => rec.StudentId, rec => rec.Student as IStudentModel);
|
|
|
|
|
}
|
|
|
|
|
return _studentDocument;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 13:24:39 +04:00
|
|
|
|
public static Document? Create(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
return new Document()
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Date = model.Date,
|
|
|
|
|
UserId = model.UserId,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if(model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Name = model.Name;
|
|
|
|
|
Date = model.Date;
|
|
|
|
|
UserId = model.UserId;
|
|
|
|
|
}
|
2023-04-08 20:16:42 +04:00
|
|
|
|
public void UpdateStudents(Database context, DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var studentDocument = context.StudentDocuments
|
|
|
|
|
.Where(rec => rec.DocumentId == model.Id)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (studentDocument != null)
|
|
|
|
|
{
|
|
|
|
|
context.StudentDocuments
|
|
|
|
|
.RemoveRange(studentDocument
|
|
|
|
|
.Where(rec => !model.StudentDocument
|
|
|
|
|
.ContainsKey(rec.StudentId))
|
|
|
|
|
);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
var document = context.Documents
|
|
|
|
|
.First(x => x.Id == Id);
|
|
|
|
|
foreach (var sd in studentDocument)
|
|
|
|
|
{
|
|
|
|
|
model.StudentDocument.Remove(sd.StudentId);
|
|
|
|
|
}
|
|
|
|
|
foreach (var sd in model.StudentDocument)
|
|
|
|
|
{
|
|
|
|
|
context.StudentDocuments.Add(new StudentDocument
|
|
|
|
|
{
|
|
|
|
|
Document = document,
|
|
|
|
|
Student = context.Students.First(x => x.Id == sd.Key),
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_studentDocument = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-07 13:24:39 +04:00
|
|
|
|
public DocumentViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
Name = Name,
|
|
|
|
|
Date = Date,
|
2023-04-08 20:16:42 +04:00
|
|
|
|
UserId = UserId,
|
|
|
|
|
StudentDocument = StudentDocument
|
2023-04-07 13:24:39 +04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|