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

81 lines
2.6 KiB
C#
Raw Normal View History

2024-04-03 16:59:01 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.ViewModels;
using SushiBarDataModels.Enums;
using SushiBarDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace SushiBarDatabaseImplement.Models
{
public class Order : IOrderModel
{
[Required]
public int SushiId { get; set; }
[Required]
public int Count { get; set; }
[Required]
2024-05-01 13:12:40 +04:00
public int ClientId { get; set; }
[Required]
2024-04-03 16:59:01 +04:00
public double Sum { get; set; }
[Required]
public OrderStatus Status { get; set; }
[Required]
public DateTime DateCreate { get; set; }
public DateTime? DateImplement { get; set; }
public int Id { get; set; }
2024-05-01 13:12:40 +04:00
public virtual Client? Client { get; set; }
public Sushi? Sushi { get; set; }
2024-05-12 19:04:40 +04:00
public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new();
public static Order? Create(SushiBarDatabase context, OrderBindingModel? model)
2024-04-03 16:59:01 +04:00
{
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,
2024-05-12 19:04:40 +04:00
ClientId = model.ClientId,
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
2024-04-03 16:59:01 +04:00
};
}
2024-05-12 19:04:40 +04:00
public void Update(SushiBarDatabase context, OrderBindingModel? model)
2024-04-03 16:59:01 +04:00
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2024-05-12 19:04:40 +04:00
ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
2024-04-03 16:59:01 +04:00
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
2024-04-03 16:59:01 +04:00
SushiId = SushiId,
SushiName=Sushi.SushiName,
2024-04-03 16:59:01 +04:00
Count = Count,
Sum = Sum,
Status = Status,
2024-04-03 16:59:01 +04:00
DateCreate = DateCreate,
DateImplement = DateImplement,
2024-05-01 13:12:40 +04:00
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
2024-05-12 19:04:40 +04:00
ImplementerId = ImplementerId,
ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null,
2024-04-03 16:59:01 +04:00
};
}
}