using StudentEnrollmentContracts.BindingModels; using StudentEnrollmentContracts.ViewModels; using StudentEnrollmentDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace StudentEnrollmentDatabaseImplement.Models { public class Student //: IStudentModel { [Key] public int student_id { get; private set; } [Required] public string firstname { get; private set; } = string.Empty; [Required] public string lastname { get; private set; } = string.Empty; public string middlename { get; private set; } = string.Empty; [Required] public string email { get; private set; } = string.Empty; [Required] public long tin { get; private set; } [Required] public int exampointsid { get; private set; } public virtual ExamPoints ExamPoints { get; private set; } private Dictionary? _StudentCourse = null; public Dictionary StudentCourse { get { if (_StudentCourse == null) { _StudentCourse = Courses.ToDictionary(SC => SC.courseid, SC => SC.Course as ICourseModel); } return _StudentCourse; } } [ForeignKey("StudentId")] public virtual List Courses { get; set; } = new(); public static Student? Create(StudentEnrollmentDatabase context,StudentBindingModel model) { if (model == null) { return null; } return new Student() { student_id = model.Id, firstname = model.FirstName, lastname = model.LastName, middlename = model.MiddleName, email = model.Email, tin = model.TIN, Courses = model.StudentCourse.Select(x => new StudentCourse { Course = context.course.First(y => y.course_id == x.Key) } ).ToList(), }; } public void Update(StudentBindingModel model) { if (model == null) { return; } firstname = model.FirstName; lastname = model.LastName; middlename = model.MiddleName; tin = model.TIN; email = model.Email; } public StudentViewModel GetViewModel => new() { Id = student_id, FirstName = firstname, LastName = lastname, MiddleName = middlename, TIN = tin, Email = email, ExamPointsId = exampointsid, ExamPoints = ExamPoints.summary, }; } }