public static KitPricesReport Build(IReadOnlyCollection <WishlistKitModel> sourceItems, string shopName) { var result = new KitPricesReport(); var matchingKits = sourceItems .Select(x => new { Kit = x, Shop = x.AvailableShops.FirstOrDefault(y => y.Name.Equals(shopName)), MinPrice = x.AvailableShops.DefaultIfEmpty().Min(y => y?.Price ?? 0) }) .Where(x => x.Shop != null) .ToArray(); result.Kits = matchingKits .Select(x => new KitPriceReportUnit { Price = x.Shop.Price, Title = x.Kit.Info.Title, IsTop = x.Kit.Tags.Any(y => y.Name == "Top kit"), Place = x.Kit.AvailableShops.Count(y => x.Shop.Price > y.Price) + 1, Url = x.Kit.Image.PreviewImageUrl, Delta = x.Shop.Price - x.MinPrice }) .OrderBy(x => x.IsTop ? 0 : 1) .ThenBy(x => x.Place) .ThenBy(x => x.Price) .ToArray(); result.ShopName = matchingKits.First().Shop.Name; result.ShopLink = matchingKits.First().Shop.Link; result.TotalCount = result.Kits.Count(); result.TotalAmount = result.Kits.Sum(x => x.Price); return(result); }
public static KitPricesReportViewModel Build(IEnumerable <WishlistKitModel> sourceItems) { var items = sourceItems.ToArray(); var shops = items .SelectMany(x => x.AvailableShops) .Select(x => x.Name) .Distinct() .ToArray(); var model = new KitPricesReportViewModel(); var list = new List <KitPricesReport>(shops.Length); list.AddRange(shops.Select(shop => KitPricesReport.Build(items, shop))); model.Shops = list; var result = new { Shops = shops, Kits = items.Select(x => new { x.Info.Title, x.Manufacturer.Name, x.Image.PreviewImageUrl, Shops = x.AvailableShops }) }; File.WriteAllText(@"/Users/olgashilo/Work/Angular/CrossStitchNotesSPA/cross-stitch-notes/db/report.json", JsonConvert.SerializeObject(result, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new CamelCaseNamingStrategy() }, Formatting = Formatting.Indented })); return(model); }