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 GetFullList() { List result = new List(); foreach (var material in _source.Positions) { result.Add(material.GetViewModel); } return result; } public List GetFilteredList(PositionSearchModel model) { if (model == null || !model.Id.HasValue && string.IsNullOrEmpty(model.PositionName)) { return new(); } List result = new List(); 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; } } }