PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs
2023-05-11 13:03:42 +04:00

102 lines
3.1 KiB
C#

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 Material : IMaterialModel
{
public string MaterialName {get; private set;} = string.Empty;
public int Quantity { get; set; }
public int Id { get; private set; }
public static Material? Create(MaterialBindingModel? model)
{
if (model == null)
{
return null;
}
return new Material()
{
Id = model.Id,
MaterialName = model.MaterialName,
Quantity = model.Quantity
};
}
public static string CreateCommand(MaterialBindingModel? model)
{
if (model == null)
{
return "";
}
return $"INSERT INTO material(name, quantity) VALUES(\'{model.MaterialName}\', {model.Quantity});";
}
public static string UpdateCommand(MaterialBindingModel? model)
{
if (model == null)
{
return "";
}
return $"UPDATE material SET \"name\" = \'{model.MaterialName}\', quantity = {model.Quantity} WHERE id = {model.Id}";
}
public static string DeleteCommand(MaterialBindingModel? model)
{
if (model == null)
{
return "";
}
return $"DELETE FROM material WHERE id = {model.Id}";
}
public static string GetEmployeeCommand(MaterialBindingModel? model)
{
if (model == null)
{
return "";
}
return $"SELECT e.id FROM employee e JOIN employee_order ON employee_order.employee_id = e.id JOIN \"order\" ON \"order\".id = employee_order.order_id JOIN material_order ON material_order.order_id = \"order\".id JOIN material mat ON mat.id = material_order.material_id WHERE mat.id = {model.Id};";
}
public void Update(MaterialBindingModel? model)
{
if (model == null)
{
return;
}
MaterialName = model.MaterialName;
Quantity = model.Quantity;
}
public BsonDocument CreateBSON =>
new BsonDocument
{
{"_id", Id - 1},
{"materialName", $"{MaterialName}"},
{"quantity", $"{Quantity}" }
};
public BsonDocument UpdateBSON =>
new BsonDocument
{
{"_id", Id - 1},
{"materialName", $"{MaterialName}"},
{"quantity", $"{Quantity}" }
};
public MaterialViewModel GetViewModel => new()
{
Id = Id,
MaterialName = MaterialName,
Quantity = Quantity
};
}
}