public SearchResults Search(SearchOptions options) { if (options == null) { throw new ArgumentNullException(Constants.ParamNotProvidedExceptionMessage); } var searchColors = options.Colors.Count == 0 ? Color.All : options.Colors; var searchsizes = options.Sizes.Count == 0?Size.All: options.Sizes; var searchResul = new SearchResults { ColorCounts = Color.All.Select(color => new ColorCount { Color = color }).ToList(), SizeCounts = Size.All.Select(size => new SizeCount { Size = size }).ToList(), Shirts = new List <Shirt>() }; foreach (var size in searchsizes) { if (_shirtCollection.ShirtSizes.ContainsKey(size)) { ShirtSize colorSizesShirt = _shirtCollection.ShirtSizes[size]; foreach (var color in searchColors) { var colorCount = searchResul.ColorCounts.First(x => x.Color == color); SizeCount sizeCount = searchResul.SizeCounts.First(s => s.Size == size); if (colorSizesShirt.ShirtColors.ContainsKey(color)) { searchResul.Shirts.AddRange(colorSizesShirt.ShirtColors[color].Shirts); sizeCount.Count += colorSizesShirt.ShirtColors[color].Shirts.Count; colorCount.Count += colorSizesShirt.ShirtColors[color].Shirts.Count; } } } } return(searchResul); }
public ShirtCollection Build(List <Shirt> shirts) { ShirtCollection shirtCollection = new ShirtCollection() { ShirtSizes = new Dictionary <Size, ShirtSize>() }; if (shirts == null || shirts.Count == 0) { return(shirtCollection); } foreach (var shirt in shirts) { if (!shirtCollection.ShirtSizes.ContainsKey(shirt.Size)) { shirtCollection.ShirtSizes.Add(shirt.Size, new ShirtSize() { Size = shirt.Size, ShirtColors = new Dictionary <Color, ShirtColor>() }); } ShirtSize shirtSize = shirtCollection.ShirtSizes[shirt.Size]; if (!shirtSize.ShirtColors.ContainsKey(shirt.Color)) { shirtSize.ShirtColors.Add(shirt.Color, new ShirtColor() { Color = shirt.Color, Shirts = new List <Shirt>() }); } ShirtColor shirtColor = shirtSize.ShirtColors[shirt.Color]; shirtColor.Shirts.Add(shirt); } return(shirtCollection); }