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

85 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;
using System.Collections.Generic;
2024-03-09 00:39:32 +04:00
using System.ComponentModel.DataAnnotations;
namespace TravelCompanyDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; }
2024-03-09 00:39:32 +04:00
[Required]
public DateTime DateCreate { get; private set; }
2024-03-09 00:39:32 +04:00
public DateTime? DateImplement { get; private set; }
[Required]
public int TravelId { get; private set; }
public virtual Travel Travel { get; private set; }
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; }
2024-05-02 14:54:56 +04:00
public int? ImplementerId { get; private set; } = null;
public virtual Implementer? Implementer { get; private set; }
public static Order? Create(TravelCompanyDataBase context, OrderBindingModel model)
2024-03-09 00:39:32 +04:00
{
if (model == null)
{
return null;
}
2024-03-09 00:39:32 +04:00
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
TravelId = model.TravelId,
Travel = context.Travels.FirstOrDefault(x => x.Id == model.TravelId),
ClientId = model.ClientId,
Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId),
2024-05-02 14:54:56 +04:00
ImplementerId = model.ImplementerId,
Implementer = (model.ImplementerId.HasValue ? context.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId)
: null),
2024-03-09 00:39:32 +04:00
};
}
public void Update(OrderBindingModel? model)
2024-03-09 00:39:32 +04:00
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2024-05-02 14:54:56 +04:00
ImplementerId = model.ImplementerId;
2024-03-09 00:39:32 +04:00
}
public OrderViewModel GetViewModel => new()
{
2024-05-02 01:08:11 +04:00
TravelId = TravelId,
ClientId = ClientId,
2024-05-02 14:54:56 +04:00
ImplementerId = ImplementerId,
2024-03-09 00:39:32 +04:00
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
TravelName = Travel.TravelName,
Id = Id,
2024-05-02 14:54:56 +04:00
ClientFIO = Client.ClientFIO,
ImplementerFIO = (Implementer != null ? Implementer.ImplementerFIO : string.Empty)
2024-03-09 00:39:32 +04:00
};
2024-03-09 00:39:32 +04:00
}
}