CourseWork/University/UniversityDatabaseImplement/Models/Statement.cs

77 lines
2.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
{
public class Statement : IStatementModel
{
public int Id { get; private set; }
[Required]
public int UserId { get; private set; }
[Required]
public int TeacherId { get; private set; }
[Required]
public string Name { get; private set; } = string.Empty;
[Required]
public DateTime Date { get; private set; }
public virtual User User { get; set; } = new ();
public virtual Teacher Teacher { get; set; } = new ();
2024-05-27 23:18:20 +04:00
public static Statement? Create(UniversityDatabase context, StatementBindingModel model)
{
if (model == null)
{
return null;
}
return new Statement()
{
Id = model.Id,
UserId = model.UserId,
2024-05-27 23:18:20 +04:00
User = context.Users.First(x => x.Id == model.UserId),
TeacherId = model.TeacherId,
2024-05-27 23:18:20 +04:00
Teacher = context.Teachers.First(x => x.Id == model.TeacherId),
Name = model.Name,
Date = model.Date,
};
}
public static Statement Create(StatementViewModel model)
{
return new Statement()
{
Id = model.Id,
UserId = model.UserId,
TeacherId = model.TeacherId,
Name = model.Name,
Date = model.Date,
};
}
public void Update(StatementBindingModel model)
{
if (model == null)
{
return;
}
Id = model.Id;
UserId = model.UserId;
TeacherId = model.Id;
Name = model.Name;
Date = model.Date;
}
public StatementViewModel GetViewModel => new()
{
Id = Id,
UserId = UserId,
TeacherId = TeacherId,
2024-05-27 23:22:44 +04:00
TeacherName = Teacher.Name,
Name = Name,
Date = Date,
};
}
}