PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyPsqlImplement/Implements/PositionStorage.cs
2023-04-23 18:04:13 +04:00

115 lines
4.1 KiB
C#

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 PositionStorage : IPositionStorage
{
private readonly ConstructionCompanyDatabase _source;
public PositionStorage()
{
_source = ConstructionCompanyDatabase.GetInstance();
}
public List<PositionViewModel> GetFullList()
{
List<PositionViewModel> result = new List<PositionViewModel>();
foreach (var material in _source.Positions)
{
result.Add(material.GetViewModel);
}
return result;
}
public List<PositionViewModel> GetFilteredList(PositionSearchModel model)
{
if (model == null || !model.Id.HasValue && string.IsNullOrEmpty(model.PositionName))
{
return new();
}
List<PositionViewModel> result = new List<PositionViewModel>();
if (!string.IsNullOrEmpty(model.PositionName))
{
foreach (var material in _source.Positions)
{
if (material.PositionName.Equals(model.PositionName)) result.Add(material.GetViewModel);
}
return result;
}
else
{
foreach (var material in _source.Positions)
{
if (material.Id == model.Id) result.Add(material.GetViewModel);
}
return result;
}
}
public PositionViewModel? GetElement(PositionSearchModel model)
{
if (model == null || !model.Id.HasValue)
{
return new();
}
return _source.Positions.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public PositionViewModel? Insert(PositionBindingModel model)
{
var command = Position.CreateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var newPosition = _source.Positions[_source.Positions.Count - 1];
return newPosition.GetViewModel;
}
public PositionViewModel? Update(PositionBindingModel model)
{
var command = Position.UpdateCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
_source.ExecuteSql(command);
var updatedPosition = _source.Positions.First(x => x.Id == model.Id);
return updatedPosition.GetViewModel;
}
public PositionViewModel? Delete(PositionBindingModel model)
{
var command = Position.DeleteCommand(model);
if (string.IsNullOrEmpty(command))
{
return null;
}
var deletedPosition = _source.Positions.First(x => x.Id == model.Id).GetViewModel;
_source.ExecuteSql(command);
return deletedPosition;
}
public List<BrigadeReportViewModel> GetPositionsAverage(DateTime dateFrom, DateTime dateTo)
{
var command = Position.PositionsAVGCommnad(dateFrom, dateTo);
var result = _source.ExecuteReader(command, 2);
List<BrigadeReportViewModel> positionsAverages = new List<BrigadeReportViewModel>();
foreach (var posAvgPair in result)
{
string positionName = _source.Positions.First(x => x.Id == Convert.ToInt32(posAvgPair[0])).PositionName;
positionsAverages.Add(new BrigadeReportViewModel { PositionId = Convert.ToInt32(posAvgPair[0]), PositionName = positionName, materialAvg = Convert.ToDouble(posAvgPair[1]) });
}
return positionsAverages;
}
}
}