PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/MaterialStorage.cs

102 lines
3.4 KiB
C#
Raw Normal View History

using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyPsqlImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyPsqlImplement.Implements
{
public class MaterialStorage : IMaterialStorage
{
private readonly ConstructionCompanyDatabase _source;
public MaterialStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<MaterialViewModel> GetFullList()
{
List<MaterialViewModel> result = new List<MaterialViewModel>();
foreach (var material in _source.Materials)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<MaterialViewModel> GetFilteredList(MaterialSearchModel model)
{
if (model == null || !model.Id.HasValue || string.IsNullOrEmpty(model.MaterialName))
{
return new();
}
List<MaterialViewModel> result = new List<MaterialViewModel>();
if (!string.IsNullOrEmpty(model.MaterialName))
{
foreach (var material in _source.Materials)
{
if (material.MaterialName.Equals(model.MaterialName)) result.Add(material.GetViewModel);
}
return result;
}
else
{
foreach (var material in _source.Materials)
{
if (material.Id == model.Id) result.Add(material.GetViewModel);
}
return result;
}
}
public MaterialViewModel? GetElement(MaterialSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
return _source.Materials.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public MaterialViewModel? Insert(MaterialBindingModel model)
{
var command = Material.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newMaterial = _source.Materials[_source.Materials.Count - 1];
return newMaterial.GetViewModel;
}
public MaterialViewModel? Update(MaterialBindingModel model)
{
var command = Material.UpdateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var updatedMaterial = _source.Materials.First(x => x.Id == model.Id);
return updatedMaterial.GetViewModel;
}
public MaterialViewModel? Delete(MaterialBindingModel model)
{
var command = Material.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedMaterial = _source.Materials.First(x => x.Id == model.Id).GetViewModel;
_source.ExecuteSql(command);
return deletedMaterial;
}
}
}