PIbd-22_Safiulova_K.N._Airc.../AircraftPlant/AircraftPlantDatabaseImplement/Models/Order.cs

92 lines
2.7 KiB
C#
Raw Normal View History

using AircraftPlantContracts.BindingModels;
using AircraftPlantContracts.ViewModels;
using AircraftPlantDataModels.Enums;
using AircraftPlantDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace AircraftPlantDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; set; }
2024-03-26 21:23:10 +04:00
[Required]
public int PlaneId { get; set; }
2024-03-26 21:23:10 +04:00
2024-04-22 23:18:25 +04:00
public virtual Plane Plane { get; set; } = new();
[Required]
public int ClientId { get; set; }
public Client Client { get; set; } = new();
2024-05-06 21:14:20 +04:00
public int? ImplementerId { get; set; }
public virtual Implementer? Implementer { get; set; } = new();
[Required]
public int Count { get; set; }
2024-03-26 21:23:10 +04:00
[Required]
public double Sum { get; set; }
2024-03-26 21:23:10 +04:00
[Required]
public OrderStatus Status { get; set; }
2024-03-26 21:23:10 +04:00
[Required]
public DateTime DateCreate { get; set; }
2024-03-26 21:23:10 +04:00
public DateTime? DateImplement { get; set; }
2024-03-26 21:23:10 +04:00
2024-04-22 23:18:25 +04:00
public static Order? Create(AircraftPlantDatabase context, OrderBindingModel? model)
{
if (model == null)
{
return null;
}
2024-03-26 21:23:10 +04:00
return new Order()
{
Id = model.Id,
PlaneId = model.PlaneId,
2024-04-22 23:18:25 +04:00
Plane = context.Planes.First(x => x.Id == model.PlaneId),
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
2024-05-06 21:14:20 +04:00
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
2024-03-26 21:23:10 +04:00
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2024-05-06 21:14:20 +04:00
if (model.ImplementerId.HasValue)
ImplementerId = model.ImplementerId;
}
2024-03-26 21:23:10 +04:00
public OrderViewModel GetViewModel => new()
{
Id = Id,
PlaneId = PlaneId,
2024-04-22 23:18:25 +04:00
PlaneName = Plane.PlaneName,
ClientId = ClientId,
ClientFIO = Client.ClientFIO,
2024-05-06 21:14:20 +04:00
ImplementerId = ImplementerId,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
2024-03-26 21:23:10 +04:00
DateImplement = DateImplement,
};
}
2024-03-26 21:23:10 +04:00
}