PIbd-23_Sergunov_M.A._SUBD/BookShop/BookShopDataBaseImplement/Models/Order.cs
2023-05-12 12:35:01 +04:00

80 lines
2.2 KiB
C#

using BookShopContracts.BindingModels;
using BookShopContracts.ViewModels;
using BookShopDataModels.Models;
using System.ComponentModel.DataAnnotations;
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,
BookName = context.Books.FirstOrDefault(x => x.Id == BookId)?.BookName ?? string.Empty,
BookId = BookId,
Count = Count,
Sum = Sum,
DateCreate = DateCreate
};
}
}
}
}