PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Models/MaterialOrder.cs

68 lines
2.2 KiB
C#
Raw Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models;
2023-05-11 13:03:42 +04:00
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<Material> materials, List<Order> 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}";
}
2023-05-11 13:03:42 +04:00
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
};
}
}