PIbd-23_Abazov_A.A._Constru.../ConstructionCompany/ConstructionCompanyMongoDBImplement/Implements/PositionStorage.cs

118 lines
4.4 KiB
C#

using Amazon.Auth.AccessControlPolicy;
using ConstructionCompanyContracts.BindingModels;
using ConstructionCompanyContracts.SearchModels;
using ConstructionCompanyContracts.StorageContracts;
using ConstructionCompanyContracts.ViewModels;
using ConstructionCompanyMongoDBImplement.Models;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConstructionCompanyMongoDBImplement.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)
{
model.Id = _source.Positions.Count > 0 ? _source.Positions.Max(x => x.Id) + 1 : 0;
var document = Position.CreateBSON(model);
if (document == null)
{
return null;
}
_source.InsertDocument(document, "Positions");
var newPosition = _source.Positions[_source.Positions.Count - 1];
return newPosition.GetViewModel;
}
public PositionViewModel? Update(PositionBindingModel model)
{
List<int> ids = new List<int>();
foreach (var employee in _source.Employees)
{
if (employee.PositionID == model.Id) ids.Add(employee.Id);
}
var document = Position.UpdateBSON(model, ids);
if (document == null)
{
return null;
}
_source.ReplaceDocument(document, new BsonDocument { {"_id" , model.Id} }, "Positions");
var updatedPosition = _source.Positions.First(x => x.Id == model.Id);
return updatedPosition.GetViewModel;
}
public PositionViewModel? Delete(PositionBindingModel model)
{
var deletedPosition = _source.Positions.First(x => x.Id == model.Id).GetViewModel;
_source.DeleteDocument(new BsonDocument { { "_id", model.Id } }, "Positions");
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;
}
}
}