ISEbd-21.Gordeev.I.V.SushiB.../SushiBar/SushiBarDatabaseImplement/Models/Order.cs

97 lines
3.2 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
{
[Required]
[DataMember]
public int SushiId { get; set; }
[Required]
[DataMember]
public int Count { get; set; }
[Required]
[DataMember]
public int ClientId { get; set; }
[Required]
[DataMember]
public double Sum { get; set; }
[Required]
[DataMember]
public OrderStatus Status { get; set; }
[Required]
[DataMember]
public DateTime DateCreate { get; set; }
[DataMember]
public DateTime? DateImplement { get; set; }
[DataMember]
public int Id { get; set; }
public virtual Client? Client { get; set; }
public Sushi? Sushi { get; set; }
public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new();
public static Order? Create(SushiBarDatabase context, OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SushiId = model.SushiId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
};
}
public void Update(SushiBarDatabase context, OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
}
public OrderViewModel GetViewModel
{
get {
using var context = new SushiBarDatabase();
return new OrderViewModel
{
Id = Id,
ClientId = ClientId,
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
SushiId = SushiId,
SushiName = context.ListSushi.FirstOrDefault(x => x.Id == SushiId)?.SushiName ?? string.Empty,
ImplementerId = ImplementerId,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}
}
}