Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions InventoryAPI/InventoryAPI.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions InventoryAPI/Products/ProductsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.AspNetCore.Authorization;
using InventoryDAL.Interfaces;
using System.Collections.Generic;
using System;

namespace InventoryAPI.Products
{
Expand All @@ -29,11 +30,24 @@ public List<ProductRequestModel> GetAll()
{
var products = productsFacade.GetAll();

var productRequestModels = products.ConvertAll(new System.Converter<ProductDTO, ProductRequestModel>(ProductRequestModel.ProductDTOToProductRequestModel));
var productRequestModels = products.ConvertAll(new Converter<ProductDTO, ProductRequestModel>(ProductRequestModel.ProductDTOToProductRequestModel));

return productRequestModels;
}

/// <summary>
/// Get a list of all Product definitions with a specified tag
/// </summary>
[HttpGet]
[Route("filter/{tagId}")]
public List<ProductRequestModel> GetAll(int tagId)
{
var products = productsFacade.GetAll(tagId);

return products.ConvertAll(new Converter<ProductDTO, ProductRequestModel>(
ProductRequestModel.ProductDTOToProductRequestModel));
}

/// <summary>
/// Get a specified Product definition
/// </summary>
Expand Down Expand Up @@ -91,7 +105,7 @@ public bool ApplyTag(int id, int tagId)
/// <summary>
/// Apply a tag to a product
/// </summary>
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "inventory_product_removetag")]
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "inventory_product_removetag")]
[HttpPost]
[Route("{id}/removetag")]
public bool RemoveTag(int id, int tagId)
Expand Down
15 changes: 1 addition & 14 deletions InventoryDAL/Factories/ConverterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
using InventoryDAL.Products;
using InventoryDAL.Stocks;
using InventoryLogic.Interfaces;
using InventoryLogic.Products;
using InventoryLogic.Stocks;
using InventoryLogic.Tags;

namespace InventoryDAL.Factories
{
public class ConverterFactory : IConverterFactory
{
private readonly IDomainFactory domainFactory;
private readonly IRepositoryFactory repositoryFactory;
private readonly IEntityFactory entityFactory;
private readonly IDAOFactory daoFactory;
public ProductConverter productConverter { get; private set; }
public ProductEntityConverter productEntityConverter { get; private set; }
public StockConverter stockConverter { get; private set; }
Expand All @@ -27,18 +20,12 @@ public ConverterFactory(IDomainFactory domainFactory,
IRepositoryFactory repositoryFactory,
IDAOFactory daoFactory)
{
this.domainFactory = domainFactory;
this.entityFactory = entityFactory;
this.repositoryFactory = repositoryFactory;
this.daoFactory = daoFactory;
productConverter = new ProductConverter(this.domainFactory, this.repositoryFactory);
productConverter = new ProductConverter(domainFactory, repositoryFactory);
productEntityConverter = new ProductEntityConverter(entityFactory, daoFactory);
stockConverter = new StockConverter(domainFactory, repositoryFactory);
stockEntityConverter = new StockEntityConverter(entityFactory, daoFactory);
tagConverter = new TagConverter(domainFactory, repositoryFactory);
tagEntityConverter = new TagEntityConverter(entityFactory, daoFactory);

}

}
}
10 changes: 6 additions & 4 deletions InventoryDAL/Products/Converters/ProductConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ public Product Convert(IProductEntity productEntity, OnObjectCreation onObjectCr
List<Tag> tags = new List<Tag>();
List<Stock> stocks = new List<Stock>();
//compose product
Product product = new Product(productEntity.Id
, productEntity.Name
, productEntity.Price
, productEntity.Sku,tags,stocks);
Product product = domainFactory.CreateProduct(productEntity.Id,
productEntity.Name,
productEntity.Price,
productEntity.Sku,
tags,
stocks);

// handle instantiated products. Needed by repository to prevent looping.
onObjectCreation(product, productEntity);
Expand Down
36 changes: 28 additions & 8 deletions InventoryDAL/Products/Repository/ProductsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ProductsRepository : IProductsRepository
private readonly IConverterFactory converterFactory;
private readonly IProductEntityDAO productEntityDAO;

private readonly Dictionary<Product,IProductEntity> productCache;
private readonly Dictionary<Product, IProductEntity> productCache;


public ProductsRepository(IProductEntityDAO productEntityDAO,
Expand All @@ -24,29 +24,41 @@ public ProductsRepository(IProductEntityDAO productEntityDAO,
// Handle cacheing of object on instantiation
private void OnObjectCreation(Product product, IProductEntity productEntity)
{
RemoveFromCache(product);
productCache.Add(product, productEntity);
}

public List<Product> GetAll()
{
{
List<ProductEntity> productEntities = productEntityDAO.GetAll();

// Trigger with where, only products not cached, and then select all uncached product entities to convert Products that will be added
// to the cache with the OnObjectCreation delegate.
productEntities.Where(productEntity => productCache.Values.Any(cacheEntity => cacheEntity.Id == productEntity.Id) == false
).ToList().ForEach(productEntity => converterFactory.productConverter.Convert(productEntity, OnObjectCreation));

var entitiesNotCached = productEntities
.Where(productEntity => productCache.Values.Any(cacheEntity => cacheEntity.Id == productEntity.Id) == false)
.ToList();
entitiesNotCached.ForEach(productEntity => converterFactory.productConverter.Convert(productEntity, OnObjectCreation));


return productCache.Keys.ToList<Product>();
}

public List<Product> GetAll(int tagId)
{
List<Product> products = this.GetAll();

return products.Where(product => product.Tags.Any(tag => tag.Id == tagId)).ToList();
}

public Product Get(int id)
{
Product product = productCache.Keys.Where(p => p.Id == id).FirstOrDefault();
if (product == null) {
if (product == null)
{
ProductEntity productEntity = productEntityDAO.Get(id);
return converterFactory.productConverter.Convert(productEntity, OnObjectCreation);
} else
}
else
{
return product;
}
Expand All @@ -61,16 +73,24 @@ public Product Add(Product product)

public void Modify(Product product)
{
RemoveFromCache(product);

ProductEntity productEntity = converterFactory.productEntityConverter.Convert(product);
productEntityDAO.Modify(productEntity);
}

private void RemoveFromCache(Product product) // used in Facade. TODO: solve in DAL
{
Product productInCache = productCache.Keys.Where(p => p.Id == product.Id).FirstOrDefault();
if (productInCache != null) productCache.Remove(productInCache);
}

public void Remove(int id)
{
productCache.Remove(productCache.Where(cacheEntity => cacheEntity.Key.Id == id).First().Key);
productEntityDAO.Remove(id);

//TODO : cleanup childs.
//TODO: cleanup childs.
}

public Product CreateNew()
Expand Down
22 changes: 15 additions & 7 deletions InventoryDAL/Stocks/Repository/StocksRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace InventoryDAL.Stocks
{
public class StocksRepository : IStocksRepository
{
private readonly IConverterFactory converterFactory;
private readonly IConverterFactory converterFactory;
private readonly IStockEntityDAO stockEntityDAO;
private readonly Dictionary<Stock, IStockEntity> stockCache;


public StocksRepository(IStockEntityDAO stockEntityDAO, IConverterFactory converterFactory)
{
this.converterFactory = converterFactory;
this.converterFactory = converterFactory;
this.stockEntityDAO = stockEntityDAO;
stockCache = new Dictionary<Stock, IStockEntity>();
}
Expand All @@ -24,19 +24,19 @@ public StocksRepository(IStockEntityDAO stockEntityDAO, IConverterFactory conver
// Handle cacheing of object on instantiation
private void OnObjectCreation(Stock stock, IStockEntity stockEntity)
{
if(!stockCache.ContainsValue(stockEntity))
stockCache.Add(stock, stockEntity);
RemoveFromCache(stock);
stockCache.Add(stock, stockEntity);
}

public List<Stock> GetAll()
{
List<StockEntity> stockEntities = stockEntityDAO.GetAll();

// Trigger with where, only stocks not cached, and then select all uncached stock entities to convert Stocks that will be added
// to the cache with the OnObjectCreation delegate.
stockEntities.Where(stockEntity => stockCache.Values.Any(cacheEntity => stockEntity.Id == cacheEntity.Id) == false)
.ToList().ForEach(stockEntity => converterFactory.stockConverter.Convert(stockEntity,OnObjectCreation));
.ToList().ForEach(stockEntity => converterFactory.stockConverter.Convert(stockEntity, OnObjectCreation));

return stockCache.Keys.ToList<Stock>();
}

Expand Down Expand Up @@ -64,10 +64,18 @@ public Stock Add(Stock stock)

public void Modify(Stock stock)
{
RemoveFromCache(stock);

StockEntity stockEntity = converterFactory.stockEntityConverter.Convert(stock);
stockEntityDAO.Modify(stockEntity);
}

private void RemoveFromCache(Stock stock) // used in Facade. TODO: solve in DAL
{
Stock stockInCache = stockCache.Keys.Where(s => s.Id == stock.Id).FirstOrDefault();
if (stockInCache != null) stockCache.Remove(stockInCache);
}

public void Remove(int id)
{
stockCache.Remove(stockCache.Where(cacheEntity => cacheEntity.Key.Id == id).First().Key);
Expand Down
11 changes: 10 additions & 1 deletion InventoryDAL/Tags/Repository/TagsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public TagsRepository(ITagEntityDAO tagEntityDAO, IConverterFactory converterFac
// Handle cacheing of object on instantiation
private void OnObjectCreation(Tag tag, ITagEntity tagEntity)
{
RemoveFromCache(tag);
tagCache.Add(tag, tagEntity);
}

Expand Down Expand Up @@ -56,13 +57,21 @@ public Tag Add(Tag tag)

public void Modify(Tag tag)
{
RemoveFromCache(tag);
TagEntity tagEntity = converterFactory.tagEntityConverter.Convert(tag);
tagEntityDAO.Modify(tagEntity);
}

public void RemoveFromCache(Tag tag) // used in Facade. TODO: solve in DAL
{
Tag tagInCache = tagCache.Keys.Where(t => t.Id == tag.Id).FirstOrDefault();
if (tagInCache != null) tagCache.Remove(tagInCache);
}

public void Remove(int id)
{
tagCache.Remove(tagCache.Where(cacheEntity => cacheEntity.Key.Id == id).First().Key);
Tag tagInCache = tagCache.Keys.Where(t => t.Id == id).FirstOrDefault();
if (tagInCache != null) tagCache.Remove(tagInCache);
tagEntityDAO.Remove(id);
}

Expand Down
Loading