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

97 lines
3.2 KiB
C#
Raw Permalink 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;
using System.Runtime.Serialization;
2024-04-03 16:59:01 +04:00
namespace SushiBarDatabaseImplement.Models
{
[DataContract]
2024-04-03 16:59:01 +04:00
public class Order : IOrderModel
{
[Required]
[DataMember]
2024-04-03 16:59:01 +04:00
public int SushiId { get; set; }
[Required]
[DataMember]
2024-04-03 16:59:01 +04:00
public int Count { get; set; }
[Required]
[DataMember]
2024-05-01 13:12:40 +04:00
public int ClientId { get; set; }
[Required]
[DataMember]
2024-04-03 16:59:01 +04:00
public double Sum { get; set; }
[Required]
[DataMember]
2024-04-03 16:59:01 +04:00
public OrderStatus Status { get; set; }
[Required]
[DataMember]
2024-04-03 16:59:01 +04:00
public DateTime DateCreate { get; set; }
[DataMember]
2024-04-03 16:59:01 +04:00
public DateTime? DateImplement { get; set; }
[DataMember]
2024-04-03 16:59:01 +04:00
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
}
2024-05-12 20:03:46 +04:00
public OrderViewModel GetViewModel
2024-04-03 16:59:01 +04:00
{
2024-05-12 20:03:46 +04:00
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,
};
}
}
2024-04-03 16:59:01 +04:00
}
}