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

104 lines
3.1 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;
2024-05-18 14:59:42 +04:00
using System.Runtime.Serialization;
2024-03-09 00:39:32 +04:00
namespace TravelCompanyDatabaseImplement.Models
{
2024-05-18 14:59:42 +04:00
[DataContract]
2024-03-09 00:39:32 +04:00
public class Order : IOrderModel
{
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
public int Id { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
[Required]
public int Count { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
[Required]
public double Sum { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
[Required]
public OrderStatus Status { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
[Required]
public DateTime DateCreate { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
2024-03-09 00:39:32 +04:00
public DateTime? DateImplement { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
[Required]
public int TravelId { get; private set; }
public virtual Travel Travel { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; }
2024-05-18 14:59:42 +04:00
[DataMember]
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
}
}