PIbd-23_Bogdanov_D.S._Compu.../ComputerShopDatabaseImplement/Models/Order.cs

99 lines
3.3 KiB
C#
Raw Normal View History

2023-03-26 15:40:54 +04:00
using ComputerShopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerShopContracts.BindingModels;
using ComputerShopContracts.ViewModels;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using ComputerShopDataModels.Enums;
using System.Reflection.Metadata;
2023-04-07 13:54:25 +04:00
using DocumentFormat.OpenXml.InkML;
using DocumentFormat.OpenXml.Office2010.Word;
2023-03-26 15:40:54 +04:00
namespace ComputerShopDatabaseImplement.Models
{
internal class Order : IOrderModel
{
public string ComputerName { get; private set; } = String.Empty;
public int Id { get; private set; }
[Required]
public int ComputerId { get; private set; }
[Required]
2023-04-07 13:54:25 +04:00
public int ClientId { get; private set; }
2023-04-19 15:46:04 +04:00
public int? ImplementerId { get; private set; }
2023-04-07 13:54:25 +04:00
[Required]
2023-03-26 15:40:54 +04:00
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Computer Computer { get; set; }
2023-04-07 13:54:25 +04:00
public virtual Client Client { get; set; }
2023-04-19 15:46:04 +04:00
public virtual Implementer? Implementer { get; set; }
2023-04-07 13:54:25 +04:00
2023-03-26 15:40:54 +04:00
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
ComputerId = model.ComputerId,
ComputerName = model.ComputerName,
2023-04-07 13:54:25 +04:00
ClientId = model.ClientId,
2023-04-19 15:46:04 +04:00
ImplementerId = model.ImplementerId,
2023-03-26 15:40:54 +04:00
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
Id = model.Id,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
2023-04-19 15:46:04 +04:00
ImplementerId = model.ImplementerId;
2023-03-26 15:40:54 +04:00
}
2023-04-07 13:54:25 +04:00
public OrderViewModel GetViewModel
2023-03-26 15:40:54 +04:00
{
2023-04-07 13:54:25 +04:00
get
{
var context = new ComputerShopDatabase();
return new()
{
ComputerId = ComputerId,
ClientId = ClientId,
2023-04-19 15:46:04 +04:00
ImplementerId = ImplementerId,
2023-04-07 13:54:25 +04:00
Count = Count,
Sum = Sum,
DateCreate = DateCreate,
DateImplement = DateImplement,
Id = Id,
Status = Status,
ComputerName = context.Computers.FirstOrDefault(x => x.Id == ComputerId)?.ComputerName ?? string.Empty,
2023-04-19 15:46:04 +04:00
ClientFIO = context.Clients.FirstOrDefault(x => x.Id == ClientId)?.ClientFIO ?? string.Empty,
ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty
2023-04-07 13:54:25 +04:00
};
}
}
2023-03-26 15:40:54 +04:00
}
}