PIBD-21_Lobashov_I_D_Travel.../TravelCompany/TravelCompanyDatabaseImplement/Models/Order.cs

89 lines
2.9 KiB
C#
Raw Normal View History

2024-03-09 00:39:32 +04:00
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.ViewModels;
using TravelCompanyDataModels.Enums;
using TravelCompanyDataModels.Models;
using System.ComponentModel.DataAnnotations;
namespace TravelCompanyDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
2024-05-02 01:08:11 +04:00
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; } = new();
[Required]
public int TravelId { get; private set; }
public virtual Travel Travel { get; set; } = new();
public int? ImplementerId { get; private set; }
public virtual Implementer? Implementer { get; set; } = new();
2024-03-09 00:39:32 +04:00
[Required]
public int Count { get; private set; }
2024-05-02 01:08:11 +04:00
2024-03-09 00:39:32 +04:00
[Required]
public double Sum { get; private set; }
2024-05-02 01:08:11 +04:00
2024-03-09 00:39:32 +04:00
[Required]
2024-05-02 01:08:11 +04:00
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
2024-03-09 00:39:32 +04:00
[Required]
2024-05-02 01:08:11 +04:00
public DateTime DateCreate { get; private set; } = DateTime.Now;
2024-03-09 00:39:32 +04:00
public DateTime? DateImplement { get; private set; }
2024-05-02 01:08:11 +04:00
public static Order Create(TravelCompanyDataBase context, OrderBindingModel model)
2024-03-09 00:39:32 +04:00
{
return new Order()
{
Id = model.Id,
2024-05-02 01:08:11 +04:00
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
TravelId = model.TravelId,
Travel = context.Travels.First(x => x.Id == model.TravelId),
ImplementerId = model.ImplementerId,
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null,
2024-03-09 00:39:32 +04:00
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
2024-05-02 01:08:11 +04:00
public void Update(TravelCompanyDataBase context, OrderBindingModel? model)
2024-03-09 00:39:32 +04:00
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2024-05-02 01:08:11 +04:00
ImplementerId = model.ImplementerId;
Implementer = model.ImplementerId.HasValue ? context.Implementers.First(x => x.Id == model.ImplementerId) : null;
2024-03-09 00:39:32 +04:00
}
public OrderViewModel GetViewModel => new()
{
2024-05-02 01:08:11 +04:00
Id = Id,
2024-04-18 16:34:54 +04:00
ClientId = ClientId,
2024-05-02 01:08:11 +04:00
ClientFIO = Client.ClientFIO,
TravelId = TravelId,
TravelName = Travel.TravelName,
ImplementerId = ImplementerId,
ImplementerFIO = Implementer != null ? Implementer.ImplementerFIO : null,
2024-03-09 00:39:32 +04:00
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
}
}