public async Task <IActionResult> GetColorSizeMatrix(Guid itemId) { ColorSizeMatrixModel result = await analyticsRepo.GetColorSizeMatrix(itemId); return(Ok(result)); }
public async Task <ColorSizeMatrixModel> GetColorSizeMatrix(Guid _itemId) { ColorSizeMatrixModel result = new ColorSizeMatrixModel(); try { #region get size, color and quantity of selected item List <ItemDetail> itemDetailsOfItem = await context.ItemDetail .Where(x => x.ItemId == _itemId) .ToListAsync(); List <SizeModel> sizesOfItem = await context.Size .Where(x => itemDetailsOfItem.Any(y => y.SizeId == x.SizeId)) .Select(x => new SizeModel { sizeId = x.SizeId, sizing = x.Sizing, usaSizing = x.UsaSizing, ukSizing = x.UkSizing, europeSizing = x.EuropeSizing, japanSizing = x.JapanSizing, australiaSizing = x.AustraliaSizing }) .OrderBy(x => x.sizeId) .ToListAsync(); List <ColorModel> colorsOfItem = await context.Color .Where(x => itemDetailsOfItem.Any(y => y.ColorId == x.ColorId)) .Select(x => new ColorModel { colorId = x.ColorId, name = x.Name, hexCode = x.HexCode }) .OrderBy(x => x.colorId) .ToListAsync(); var quantitiesOfItem = itemDetailsOfItem .GroupBy(x => new { x.SizeId, x.ColorId }) .Select(x => new { sizeId = x.Key.SizeId, colorId = x.Key.ColorId, quantity = x.Sum(y => y.Quantity) }) .ToList(); #endregion #region populate result result.sizes = sizesOfItem; result.colors = colorsOfItem; // populate result.Quantities foreach (SizeModel s in sizesOfItem) { List <int?> newRow = new List <int?>(); foreach (ColorModel c in colorsOfItem) { int?qnt = null; var isExist = quantitiesOfItem.Any(x => x.sizeId == s.sizeId && x.colorId == c.colorId); if (isExist) { qnt = quantitiesOfItem .Where(x => x.sizeId == s.sizeId && x.colorId == c.colorId) .Select(x => x.quantity) .FirstOrDefault(); } newRow.Add(qnt); } result.quantities.Add(newRow); } #endregion return((result.quantities.Count > 0) ? result: null); } catch (Exception ex) { // kali ex return(null); } }