using BookShopContracts.BindingModels; using BookShopContracts.ViewModels; using BookShopDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BookShopDataBaseImplement.Models { public class Order : IOrderModel { public int Id { get; private set; } public int BookId { get; private set; } [Required] public int ClientId { get; set; } [Required] public int Count { get; private set; } [Required] public double Sum { get; private set; } [Required] public DateTime DateCreate { get; private set; } = DateTime.Now; public virtual Book Book { get; set; } public Client Client { get; set; } public static Order? Create(OrderBindingModel? model) { if (model == null) { return null; } return new Order() { Id = model.Id, ClientId = model.ClientId, BookId = model.BookId, Count = model.Count, Sum = model.Sum, DateCreate = model.DateCreate }; } public void Update(OrderBindingModel? model) { if (model == null) { return; } Id = model.Id; ClientId = model.ClientId; BookId = model.BookId; Count = model.Count; Sum = model.Sum; DateCreate = model.DateCreate; } public OrderViewModel GetViewModel { get { using var context = new BookShopDatabase(); return new OrderViewModel { Id = Id, ClientId = ClientId, ClientSurname = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientSurname ?? string.Empty, BookId = BookId, BookName = context.Books.FirstOrDefault(x => x.Id == BookId)?.BookName ?? string.Empty, Count = Count, Sum = Sum, DateCreate = DateCreate }; } } } }