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

84 lines
2.6 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;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2024-04-18 16:34:54 +04:00
using TravelCompanyContracts.BindingModels;
using TravelCompanyContracts.ViewModels;
using TravelCompanyDatabaseImplement.Models;
using TravelCompanyDataModels.Enums;
using TravelCompanyDataModels.Models;
2024-03-09 00:39:32 +04:00
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; }
[Required]
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
[Required]
public int TravelId { get; private set; }
2024-04-18 16:34:54 +04:00
public virtual Travel Travel { get; private set; }
[Required]
public int ClientId { get; private set; }
public virtual Client Client { get; private set; }
public static Order? Create(TravelCompanyDataBase context, OrderBindingModel model)
2024-03-09 00:39:32 +04:00
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
TravelId = model.TravelId,
2024-04-18 16:34:54 +04:00
Travel = context.Travels.FirstOrDefault(x => x.Id == model.TravelId),
ClientId = model.ClientId,
Client = context.Clients.FirstOrDefault(x => x.Id == model.ClientId),
2024-03-09 00:39:32 +04:00
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
TravelId = TravelId,
2024-04-18 16:34:54 +04:00
ClientId = ClientId,
2024-03-09 00:39:32 +04:00
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
2024-04-18 16:34:54 +04:00
TravelName = Travel.TravelName,
2024-03-09 00:39:32 +04:00
Id = Id,
2024-04-18 16:34:54 +04:00
ClientFIO = Client.ClientFIO
2024-03-09 00:39:32 +04:00
};
}
}