using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using JewelryStoreContracts.ViewModels; using JewelryStoreContracts.BindingModels; using JewelryStoreDataModels.Models; using JewelryStoreDataModels.Enums; using System.ComponentModel.DataAnnotations; namespace JewelryStoreDatabaseImplement.Models { public class Order : IOrderModel { public int Id { get; private set; } public int JewelId { get; private set; } [Required] public int ClientId { get; set; } public string JewelName { get; private set; } = string.Empty; [Required] public int Count { get; private set; } [Required] public double Sum { get; private set; } [Required] public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; [Required] public DateTime DateCreate { get; private set; } = DateTime.Now; public DateTime? DateImplement { get; private set; } public virtual Jewel Jewel { get; set; } public virtual Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } return new Order() { Id = model.Id, JewelId = model.JewelId, ClientId = model.ClientId, JewelName = model.JewelName, Count = model.Count, Sum = model.Sum, Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement }; } public void Update(OrderBindingModel? model) { if (model == null) { return; } JewelId = model.JewelId; JewelName = model.JewelName; Count = model.Count; Sum = model.Sum; Status = model.Status; DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel { get { using var context = new JewelryStoreDataBase(); return new OrderViewModel { Id = Id, JewelId = JewelId, ClientId = ClientId, ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty, JewelName = JewelName, Count = Count, Sum = Sum, Status = Status, DateCreate = DateCreate, DateImplement = DateImplement }; } } } }