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

76 lines
2.4 KiB
C#
Raw Permalink 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;
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
};
}
}