PIbd-23_Ivanov_V.N._Pizzeria/Pizzeria/PizzeriaDatabaseImplement/Models/Order.cs

79 lines
2.2 KiB
C#
Raw Normal View History

2024-03-04 23:06:34 +04:00
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Enums;
using PizzeriaDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace PizzeriaDatabaseImplement.Models
{
public class Order : IOrderModel
{
2024-03-22 10:07:28 +04:00
public int Id { get; private set; }
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public int ClientId { get; private set; }
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
public virtual Client Client { get; private set; } = new();
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public int PizzaId { get; private set; }
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
public virtual Pizza Pizza { get; set; } = new();
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public int Count { get; private set; }
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public double Sum { get; private set; }
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
2024-03-04 23:06:34 +04:00
2024-03-22 10:07:28 +04:00
public DateTime? DateImplement { get; private set; }
2024-03-21 13:21:25 +04:00
2024-03-22 10:07:28 +04:00
public static Order Create(PizzeriaDatabase context, OrderBindingModel model)
{
return new Order()
{
Id = model.Id,
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
PizzaId = model.PizzaId,
Pizza = context.Pizzas.First(x => x.Id == model.PizzaId),
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
2024-03-21 13:21:25 +04:00
2024-03-22 10:07:28 +04:00
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
2024-03-21 13:21:25 +04:00
2024-03-22 10:07:28 +04:00
public OrderViewModel GetViewModel => new()
{
Id = Id,
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
PizzaId = PizzaId,
PizzaName = Pizza.PizzaName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
2024-03-04 23:06:34 +04:00
}