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 Receipt : IReceiptModel { public int Id { get; set; } [Required] public float Sum { get; set; } [Required] public DateTime ReceiptDate { get; set; } = DateTime.Now; [Required] public int BossId { get; set; } public virtual Boss Boss { get; set; } = new(); public int ConfigurationId { get; set; } public virtual Configuration Configuration { get; set; } = new(); public static Receipt? Create(CarCenterDatabase context, ReceiptBindingModel model) { if (model == null) { return null; } return new Receipt() { Id = model.Id, Sum = model.Sum, ReceiptDate = model.ReceiptDate, BossId = model.BossId, Boss = context.Bosses.First(x => x.Id == model.BossId), ConfigurationId = model.ConfigurationId, Configuration = context.Configurations.First(x => x.Id == model.ConfigurationId), }; } public static Receipt? Create(CarCenterDatabase context, ReceiptViewModel model) { return new Receipt() { Id = model.Id, Sum = model.Sum, ReceiptDate = model.ReceiptDate, BossId = model.BossId, Boss = context.Bosses.First(x => x.Id == model.BossId), ConfigurationId = model.ConfigurationId, Configuration = context.Configurations.First(x => x.Id == model.ConfigurationId), }; } public void Update(ReceiptBindingModel? model) { if (model == null) { return; } ReceiptDate = model.ReceiptDate; } public ReceiptViewModel GetViewModel => new() { Id = Id, Sum = Sum, ReceiptDate = ReceiptDate, BossId = BossId, BossName = Boss.Surname + " " + Boss.Name + " " + Boss.Patronymic, ConfigurationId = ConfigurationId, ConfigurationName = Configuration.Name, }; } }