87 lines
2.2 KiB
C#
Raw Normal View History

using StudentEnrollmentContracts.BindingModels;
2024-05-06 20:26:16 +04:00
using StudentEnrollmentContracts.ViewModels;
using StudentEnrollmentDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace StudentEnrollmentDatabaseImplement.Models
{
2024-05-06 20:26:16 +04:00
public class Student //: IStudentModel
{
2024-05-06 20:26:16 +04:00
[Key]
public int student_id { get; private set; }
[Required]
2024-05-06 20:26:16 +04:00
public string firstname { get; private set; } = string.Empty;
[Required]
2024-05-06 20:26:16 +04:00
public string lastname { get; private set; } = string.Empty;
public string middlename { get; private set; } = string.Empty;
[Required]
2024-05-06 20:26:16 +04:00
public string email { get; private set; } = string.Empty;
[Required]
2024-05-06 20:26:16 +04:00
public long tin { get; private set; }
[Required]
2024-05-06 20:26:16 +04:00
public int exampointsid { get; private set; }
public virtual ExamPoints ExamPoints { get; private set; }
private Dictionary<int, ICourseModel>? _StudentCourse = null;
public Dictionary<int, ICourseModel> StudentCourse
{
get
{
if (_StudentCourse == null)
{
2024-05-06 20:26:16 +04:00
_StudentCourse = Courses.ToDictionary(SC => SC.courseid,
SC => SC.Course as ICourseModel);
}
return _StudentCourse;
}
}
[ForeignKey("StudentId")]
public virtual List<StudentCourse> Courses { get; set; } = new();
public static Student? Create(StudentEnrollmentDatabase context,StudentBindingModel model)
{
if (model == null)
{
return null;
}
return new Student()
{
2024-05-06 20:26:16 +04:00
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
{
2024-05-06 20:26:16 +04:00
Course = context.course.First(y => y.course_id == x.Key)
}
).ToList(),
};
}
2024-05-06 20:26:16 +04:00
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,
};
}
}