SUBD_SchoolSchedule/SchoolSchedule/SchoolScheduleDataBaseImplement/Models/Grade.cs
2024-04-08 22:02:19 +04:00

69 lines
1.7 KiB
C#

using SchoolScheduleContracts.BindingModels;
using SchoolScheduleContracts.ViewModels;
using SchoolScheduleDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolScheduleDataBaseImplement.Models
{
public class Grade : IGradeModel
{
public int Id { get; private set; }
[Required]
public int Year { get; private set; }
[Required]
public char Letter { get; private set; }
[Required]
public int TeacherId { get; private set; }
[ForeignKey("GradeId")]
public virtual List<Student> Students { get; set; } =
new();
[ForeignKey("GradeId")]
public virtual List<Lesson> Lessons { get; set; } =
new();
public virtual Teacher Teacher { get; set; } = new();
public static Grade? Create(SchoolScheduleDataBase context, GradeBindingModel model)
{
if (model == null)
{
return null;
}
return new Grade()
{
Id = model.Id,
Year = model.Year,
TeacherId = model.TeacherId,
Teacher = context.Teachers.First(y => y.Id == model.TeacherId),
Letter = model.Letter
};
}
public void Update(SchoolScheduleDataBase context, GradeBindingModel model)
{
if (model == null)
{
return;
}
Year = model.Year;
Letter = model.Letter;
TeacherId = model.TeacherId;
Teacher = context.Teachers.First(x => x.Id == TeacherId);
}
public GradeViewModel GetViewModel => new()
{
Id = Id,
Year = Year,
Letter = Letter,
TeacherId = TeacherId,
TeacherFullName = Teacher.FullName,
FullName = Year.ToString() + Letter.ToString(),
};
}
}