62 lines
2.0 KiB
C#

using CarCenterContracts.BindingModels;
using CarCenterContracts.ViewModels;
using CarCenterDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarCenterDatabaseImplement.Models
{
public class Presale : IPresaleModel
{
public int Id { get; set; }
[Required]
public DateTime PresaleDate { get; set; } = DateTime.Now;
[Required]
public int EmployeeId { get; set; }
public String Name { get; set; }
public virtual Employee Employee { get; set; } = new();
public int? TypeOfJobId {get; set; }
public virtual TypeOfJob? TypeOfJob { get; set; }
[ForeignKey("PresaleId")]
public virtual List<PresaleCar> PresaleCars { get; set; } = new();
public static Presale? Create(CarCenterDatabase context, PresaleBindingModel model)
{
if (model == null)
{
return null;
}
return new Presale()
{
Id = model.Id,
Name = model.Name,
PresaleDate = model.PresaleDate,
EmployeeId = model.EmployeeId,
Employee = context.Employees.First(x => x.Id == model.EmployeeId)
};
}
public void Update(PresaleBindingModel? model)
{
if (model == null)
{
return;
}
if (model.TypeOfJobId.HasValue) TypeOfJobId = TypeOfJobId;
Name = Name;
}
public PresaleViewModel GetViewModel => new()
{
Id = Id,
TypeOfJobId = TypeOfJobId,
Name=Name,
PresaleDate = PresaleDate,
EmployeeId = EmployeeId,
EmployeeName = Employee.Surname + " " + Employee.Name + " " + Employee.Patronymic,
};
}
}