90 lines
2.9 KiB
C#
90 lines
2.9 KiB
C#
using SushiBarContracts.BindingModels;
|
||
using SushiBarContracts.ViewModels;
|
||
using SushiBarDataModels.Enums;
|
||
using SushiBarDataModels.Models;
|
||
using System.ComponentModel.DataAnnotations;
|
||
using System.Runtime.Serialization;
|
||
|
||
namespace SushiBarDatabaseImplement.Models
|
||
{
|
||
[DataContract]
|
||
public class Order : IOrderModel
|
||
{
|
||
[DataMember]
|
||
public int Id { get; private set; }
|
||
[DataMember]
|
||
[Required]
|
||
public int Count { get; set; }
|
||
[DataMember]
|
||
[Required]
|
||
public double Sum { get; set; }
|
||
[DataMember]
|
||
[Required]
|
||
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;
|
||
[DataMember]
|
||
[Required]
|
||
public DateTime DateCreate { get; set; } = DateTime.Now;
|
||
[DataMember]
|
||
public DateTime? DateImplement { get; set; }
|
||
[DataMember]
|
||
[Required]
|
||
public int SushiId { get; private set; }
|
||
public virtual Sushi Sushi { get; set; }
|
||
[DataMember]
|
||
public int ClientId { get; private set; }
|
||
public virtual Client Client { get; private set; }
|
||
[DataMember]
|
||
public int? ImplementerId { get; private set; }
|
||
public virtual Implementer? Implementer { get; set; }
|
||
|
||
public static Order Create(SushiBarDatabase context, OrderBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return null;
|
||
}
|
||
return new Order()
|
||
{
|
||
Id = model.Id,
|
||
Count = model.Count,
|
||
Sum = model.Sum,
|
||
Status = model.Status,
|
||
DateCreate = model.DateCreate,
|
||
DateImplement = model.DateImplement,
|
||
SushiId = model.SushiId,
|
||
Sushi = context.Sushis.First(x => x.Id == model.SushiId),
|
||
ClientId = model.ClientId,
|
||
Client = context.Clients.First(x => x.Id == model.ClientId),
|
||
ImplementerId = model.ImplementerId,
|
||
Implementer = model.ImplementerId == null ? null : context.Implementers.First(i => i.Id == model.ImplementerId)
|
||
};
|
||
}
|
||
public void Update(OrderBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return;
|
||
}
|
||
Status = model.Status;
|
||
DateImplement = model.DateImplement;
|
||
ClientId = model.ClientId;
|
||
ImplementerId = model.ImplementerId;
|
||
}
|
||
public OrderViewModel GetViewModel => new()
|
||
{
|
||
Id = Id,
|
||
SushiId = Sushi.Id,
|
||
Count = Count,
|
||
Sum = Sum,
|
||
Status = Status,
|
||
DateCreate = DateCreate,
|
||
DateImplement = DateImplement,
|
||
SushiName = Sushi.SushiName,
|
||
ClientId = Client.Id,
|
||
ClientFIO = Client.ClientFIO,
|
||
ImplementerId = ImplementerId,
|
||
ImplementerName = Implementer == null ? string.Empty : Implementer.ImplementerFIO
|
||
};
|
||
}
|
||
}
|