ISEbd-21_KashtanovD.A.FishF.../FishFactory/FishFactoryDatabaseImplement/Models/Order.cs

87 lines
2.8 KiB
C#
Raw Normal View History

2024-03-21 19:57:29 +04:00
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.ViewModels;
using FishFactoryDataModels.Enums;
using FishFactoryDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace FishFactoryDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
2024-04-26 00:17:50 +04:00
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; } = new();
2024-03-21 19:57:29 +04:00
[Required]
public int CannedId { get; private set; }
public virtual Canned Canned { get; set; } = new();
2024-04-30 22:57:42 +04:00
public int? ImplementerId { get; private set; }
2024-03-21 19:57:29 +04:00
2024-04-30 22:57:42 +04:00
public virtual Implementer? Implementer { get; set; } = new();
[Required]
2024-03-21 19:57:29 +04:00
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order Create(FishFactoryDatabase context, OrderBindingModel model)
{
return new Order()
{
Id = model.Id,
2024-04-26 00:17:50 +04:00
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
2024-03-21 19:57:29 +04:00
CannedId = model.CannedId,
Canned = context.Canneds.First(x => x.Id == model.CannedId),
2024-04-30 22:57:42 +04:00
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
Count = model.Count,
2024-03-21 19:57:29 +04:00
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
2024-04-30 22:57:42 +04:00
public void Update(FishFactoryDatabase context, OrderBindingModel? model)
{
2024-03-21 19:57:29 +04:00
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2024-04-30 22:57:42 +04:00
ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
}
2024-03-21 19:57:29 +04:00
public OrderViewModel GetViewModel => new()
{
Id = Id,
2024-04-26 00:17:50 +04:00
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
2024-03-21 19:57:29 +04:00
CannedId = CannedId,
CannedName = Canned.CannedName,
2024-04-30 22:57:42 +04:00
ImplementerId = ImplementerId,
ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null,
Count = Count,
2024-03-21 19:57:29 +04:00
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}