PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Models/Material.cs
2023-04-21 19:48:18 +04:00

83 lines
2.6 KiB
C#

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyDataModels.Models;
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 MaterialViewModel GetViewModel => new()
{
Id = Id,
MaterialName = MaterialName,
Quantity = Quantity
};
}
}