using ConstructionCompanyContracts.BindingModels; using ConstructionCompanyContracts.ViewModels; using ConstructionCompanyDataModels.Models; using MongoDB.Bson; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConstructionCompanyPsqlImplement.Models { public class MaterialOrder : IMaterialOrderModel { public int MaterialId { get; set; } public int OrderId { get; set; } public int Quantity { get; set; } public Material Material { get; set; } = new(); public Order Order { get; set; } = new(); public static MaterialOrder? Create(MaterialOrderBindingModel? model, List materials, List orders) { if (model == null) { return null; } return new MaterialOrder() { MaterialId = model.MaterialId, OrderId = model.OrderId, Quantity = model.Quantity, Material = materials.First(x => x.Id == model.MaterialId), Order = orders.First(x => x.Id == model.OrderId), }; } public static string CreateCommand(MaterialOrderBindingModel? model) { if (model == null) { return ""; } return $"INSERT INTO material_order(material_id, order_id, quantiny) VALUES({model.MaterialId}, {model.OrderId}, {model.Quantity});"; } public static string DeleteCommand(MaterialOrderBindingModel? model) { if (model == null) { return ""; } return $"DELETE FROM material WHERE material_id = {model.MaterialId} AND order_id = {model.OrderId}"; } public BsonDocument CreateBSON => new BsonDocument { {"materialId", MaterialId - 1 }, {"quantity", Quantity } }; public MaterialOrderViewModel GetViewModel => new() { OrderId = OrderId, MaterialId = MaterialId, Quantity = Quantity, OrderAdress = Order.Adress, MaterialName = Material.MaterialName }; } }