forked from DavidMakarov/StudentEnrollment
122 lines
3.3 KiB
C#
122 lines
3.3 KiB
C#
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<int, ICourseModel>? _studentCourse = null;
|
|
[NotMapped]
|
|
public Dictionary<int, ICourseModel> StudentCourse
|
|
{
|
|
get
|
|
{
|
|
if (_studentCourse == null)
|
|
{
|
|
_studentCourse = Courses
|
|
.ToDictionary(x => x.courseid, x => x.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;
|
|
}
|
|
var test = model.StudentCourse.Select(x => new StudentCourse
|
|
{
|
|
Course = context.course.First(y => y.course_id == x.Key)
|
|
}
|
|
).ToList();
|
|
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(),
|
|
ExamPoints = context.exampoints.First(x => x.exampoints_id == model.ExamPointsId),
|
|
};
|
|
}
|
|
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,
|
|
Summary = ExamPoints.summary,
|
|
StudentCourse = StudentCourse,
|
|
};
|
|
|
|
public void UpdateCourses(StudentEnrollmentDatabase context, StudentBindingModel model)
|
|
{
|
|
var StudentCourses = context.student_course.Where(rec => rec.studentid == model.Id).ToList();
|
|
if (StudentCourses != null)
|
|
{
|
|
context.student_course.RemoveRange(StudentCourses.Where(rec
|
|
=> !model.StudentCourse.ContainsKey(rec.courseid)));
|
|
context.SaveChanges();
|
|
foreach (var updateCourse in StudentCourses)
|
|
{
|
|
model.StudentCourse.Remove(updateCourse.courseid);
|
|
}
|
|
context.SaveChanges();
|
|
}
|
|
var Student = context.student.First(x => x.student_id == student_id);
|
|
foreach (var pc in model.StudentCourse)
|
|
{
|
|
context.student_course.Add(new StudentCourse
|
|
{
|
|
Student = Student,
|
|
Course = context.course.First(x => x.course_id == pc.Key),
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_studentCourse = null;
|
|
}
|
|
}
|
|
}
|