117 lines
3.9 KiB
C#
117 lines
3.9 KiB
C#
|
using SchoolAgainStudyContracts.BindingModel;
|
|||
|
using SchoolAgainStudyContracts.ViewModel;
|
|||
|
using SchoolAgainStudyDataModels.Models;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel.DataAnnotations;
|
|||
|
using System.ComponentModel.DataAnnotations.Schema;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Linq;
|
|||
|
using System.Runtime.ConstrainedExecution;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace SchoolAgainStudyDataBaseImplements.Models
|
|||
|
{
|
|||
|
public class Student : IStudent
|
|||
|
{
|
|||
|
[Required]
|
|||
|
public string Name { get; set; } = string.Empty;
|
|||
|
[Required]
|
|||
|
public int Class { get; set; }
|
|||
|
[Required]
|
|||
|
public string Email { get; set; } = string.Empty;
|
|||
|
[Required]
|
|||
|
public string Login { get; set; } = string.Empty;
|
|||
|
[Required]
|
|||
|
public string Password { get; set; } = string.Empty;
|
|||
|
|
|||
|
private Dictionary<int, IInterest>? _StudentInterests = null;
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, IInterest> StudentInterests
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_StudentInterests == null)
|
|||
|
{
|
|||
|
_StudentInterests = Interests
|
|||
|
.ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest));
|
|||
|
}
|
|||
|
return _StudentInterests;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[ForeignKey("StudentId")]
|
|||
|
public virtual List<StudentInterest> Interests { get; set; } = new();
|
|||
|
[ForeignKey("StudentId")]
|
|||
|
public virtual List<Diy> Diys { get; set; } = new();
|
|||
|
[ForeignKey("StudentId")]
|
|||
|
public virtual List<Product> Products { get; set; } = new();
|
|||
|
public static Student Create(SchoolDataBase context, StudentBindingModel model)
|
|||
|
{
|
|||
|
return new Student()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
Name = model.Name,
|
|||
|
Class = model.Class,
|
|||
|
Email = model.Email,
|
|||
|
Login = model.Login,
|
|||
|
Password = model.Password,
|
|||
|
Interests = model.StudentInterests.Select(x => new StudentInterest
|
|||
|
{
|
|||
|
Interest = context.Interests.First(y => y.Id == x.Key),
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
|
|||
|
public void Update(StudentBindingModel model)
|
|||
|
{
|
|||
|
Name = model.Name;
|
|||
|
Class = model.Class;
|
|||
|
Email = model.Email;
|
|||
|
Login = model.Login;
|
|||
|
Password = model.Password;
|
|||
|
}
|
|||
|
|
|||
|
public StudentViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
Name = Name,
|
|||
|
Class = Class,
|
|||
|
Email = Email,
|
|||
|
Login = Login,
|
|||
|
Password = Password,
|
|||
|
StudentInterests = StudentInterests
|
|||
|
};
|
|||
|
|
|||
|
public void UpdateInterests(SchoolDataBase context, StudentBindingModel model)
|
|||
|
{
|
|||
|
var studentInterests = context.StudentInterests.Where(rec => rec.StudentId == model.Id).ToList();
|
|||
|
if (studentInterests != null && studentInterests.Count > 0)
|
|||
|
{
|
|||
|
context.StudentInterests.RemoveRange(studentInterests.Where(rec => !model.StudentInterests.ContainsKey(rec.InterestId)));
|
|||
|
context.SaveChanges();
|
|||
|
|
|||
|
foreach (var updateInterest in studentInterests)
|
|||
|
{
|
|||
|
model.StudentInterests.Remove(updateInterest.InterestId);
|
|||
|
}
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var student = context.Students.First(x => x.Id == Id);
|
|||
|
foreach (var pc in model.StudentInterests)
|
|||
|
{
|
|||
|
context.StudentInterests.Add(new StudentInterest
|
|||
|
{
|
|||
|
Student = student,
|
|||
|
Interest = context.Interests.First(x => x.Id == pc.Key),
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_StudentInterests = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|