CourseWork/University/UniversityDatabaseImplement/Models/Student.cs

65 lines
2.2 KiB
C#
Raw Permalink Normal View History

2024-04-18 20:20:50 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
2024-04-18 20:20:50 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
2024-04-18 20:20:50 +04:00
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
{
public class Student : IStudentModel
{
public int Id { get; private set; }
[Required]
public int UserId { get; private set; }
[Required]
public int PlanOfStudyId { get; private set; }
2024-04-18 20:20:50 +04:00
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string PhoneNumber { get; private set; } = string.Empty;
[ForeignKey("StudentId")]
public virtual List<StudentDiscipline> StudentDiscipline { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Attestation> Attestations { get; set; } = new();
public virtual PlanOfStudy PlanOfStudy { get; set; } = new();
public virtual User User { get; set; } = new();
public static Student? Create(UniversityDatabase context, StudentBindingModel model)
2024-04-18 20:20:50 +04:00
{
return new Student()
{
Id = model.Id,
UserId = model.UserId,
User = context.Users.First(x => x.Id == model.UserId),
2024-04-18 20:20:50 +04:00
PlanOfStudyId = model.PlanOfStudyId,
PlanOfStudy = context.PlanOfStudys.First(x => x.Id == model.PlanOfStudyId),
2024-04-18 20:20:50 +04:00
Name = model.Name,
PhoneNumber = model.PhoneNumber
};
}
public void Update(StudentBindingModel model)
{
if (model == null)
{
return;
}
PhoneNumber = model.PhoneNumber;
Name = model.Name;
PlanOfStudyId = model.PlanOfStudyId;
}
public StudentViewModel GetViewModel => new()
{
Id = Id,
PlanOfStudyId = PlanOfStudyId,
UserId = UserId,
2024-04-18 20:20:50 +04:00
Name = Name,
PhoneNumber = PhoneNumber
};
}
}