Exemplo n.º 1
0
        private static void SaveJSONReports()
        {
            List<JSONReport> JSONReports  = new List<JSONReport>();
            using (ProductsDBContext sqlContext = new ProductsDBContext())
            {
                var products = sqlContext.Products.ToList();
                foreach (var prod in products)
                {
                    JSONReport report = new JSONReport()
                    {
                        ProductId = prod.ProductId,
                        ProductName = prod.ProductName,
                        VendorName = prod.Vendor.VendorName,
                    };

                    foreach (var salesReport in sqlContext.SalesReports)
                    {
                        if (salesReport.ProductId == prod.ProductId)
                        {
                            report.TotalQuantitySold += salesReport.Quantity;
                            report.TotalIncome += report.TotalQuantitySold * salesReport.UnitPrice;
                        }
                    }

                    JSONReports.Add(report);
                }
            }

            StreamWriter writer = new StreamWriter(@"..\..\Data\JSONReports.json");
            StringBuilder sb = new StringBuilder();

            foreach (var report in JSONReports)
            {
                var json = new JavaScriptSerializer().Serialize(report);
                sb.AppendLine(json);
            }
            using (writer)
            {
                writer.Write(sb.ToString());
            }
        }
Exemplo n.º 2
0
        private static void GenerateJSONReports()
        {
            using (ProductsDBContext sqlContext = new ProductsDBContext())
            {
                string cString = "mongodb://localhost/";
                MongoClient client = new MongoClient(cString);
                MongoServer server = client.GetServer();
                var productsDB = server.GetDatabase("ProductsDB");

                MongoCollection<JSONReport> reportsMongo = productsDB.GetCollection<JSONReport>("JSONReports");
                var products = sqlContext.Products.ToList();
                foreach (var prod in products)
                {
                    JSONReport report = new JSONReport()
                    {
                        ProductId = prod.ProductId,
                        ProductName = prod.ProductName,
                        VendorName = prod.Vendor.VendorName,
                    };

                    foreach (var salesReport in sqlContext.SalesReports)
                    {
                        if (salesReport.ProductId == prod.ProductId)
                        {
                            report.TotalQuantitySold += salesReport.Quantity;
                            report.TotalIncome += report.TotalQuantitySold * salesReport.UnitPrice;
                        }
                    }

                    reportsMongo.Insert(report);
                }
            }
        }