public static void SaveDataToDb([BlobTrigger("defenitions/{name}")] CloudBlockBlob input, TextWriter log) { using (var db = new AltechContext()) { using (var xr = new XmlTextReader(input.OpenRead())) { int groupId = default(int), subGroupId = default(int); while (xr.Read()) { if (xr.NodeType != XmlNodeType.Element) { continue; } switch (xr.Name.ToLower()) { case "discountitem": ProcessDiscountItem(db, xr); break; case "groupitem": ProcessGroupItem(db, xr, out groupId); break; case "subgroupitem": ProcessSubgroupItem(db, xr, groupId, out subGroupId); break; case "goodsitem": ProcessMerchandiseItem(db, xr, subGroupId); break; } } } // сохранить изменения в БД db.SaveChanges(); // удалить blob в случае успешной обработки input.Delete(); // Check if there are no links to images from Merchandiese Table //log.WriteLine(String.Format("idDelList contains '{0}' items", idDelList.Count)); //foreach (var id in idDelList) // if (db.Merchandises.Find(id) == null) // { // log.WriteLine(String.Format("Following image: '{0}.jpg' does not has links from DB and will be deleted.", id)); // DeleteImagesFromBlobStorage(id); // } } }
public static void ProcessQueueMessage([QueueTrigger("ordersqueue")] string inputText, TextWriter log) { int orderId; var errorMessage = string.Empty; if (!Int32.TryParse(inputText, out orderId)) { errorMessage = "Failed to parse inputText to Int32."; log.WriteLine(errorMessage); throw new ApplicationException(errorMessage); } using (var db = new AltechContext()) { var costCalculation = new OrderCostCalculation() { Db = db }; #region 1. Get Order from DB by id (inputText) var order = db.Orders.SingleOrDefault(o => o.ID == orderId); if (order == null) { errorMessage = "Failed to get Order form DB."; log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } if (!order.Completed) { errorMessage = "Order marked as NOT completed at DB."; log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } #endregion #region 2. Get customer from DB var customer = db.Customers.SingleOrDefault(c => c.ID == order.CustomerID); if (customer == null) { errorMessage = "Failed to get Customer form DB."; log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } #endregion #region 3. Calculate Order Sum with possible discount double cost, costDiscount; costCalculation.GetOrderCostWithDiscount(orderId, out cost, out costDiscount); #endregion #region 4. Get access to Storage (Orders container) var xmlOrderFileName = String.Format("Order_{0}.xml", order.ID); var htmlOrderFileName = String.Format("Order_{0}.html", order.ID); var ordersContainer = GetOrdersBlobContainer(); if (ordersContainer == null) { errorMessage = String.Format("Failed to get Blob container: '{0}'.", ordersContainerName); log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } var xmlBlob = ordersContainer.GetBlockBlobReference(xmlOrderFileName); if (xmlBlob == null) { errorMessage = String.Format("Failed to get Blob with name: '{0}'.", xmlOrderFileName); log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } var htmlBlob = ordersContainer.GetBlockBlobReference(htmlOrderFileName); if (htmlBlob == null) { errorMessage = String.Format("Failed to get Blob with name: '{0}'.", htmlOrderFileName); log.WriteLine(errorMessage); CleanUp(costCalculation); throw new ApplicationException(errorMessage); } #endregion #region 5. Generate XML file with data from order try { using (var stream = xmlBlob.OpenWrite()) { GenerateXmlFile(stream, order, customer, db.Merchandises, costCalculation); } } catch (Exception ex) { errorMessage = String.Format("Failed to generate xml order file: '{0}'.", ex.Message); log.WriteLine(errorMessage); CleanUp(costCalculation); throw ex; } #endregion #region 6. Transform XML to HTML with XSLT transformation try { using (var xmlStream = xmlBlob.OpenRead()) { using (var htmlStream = htmlBlob.OpenWrite()) { TransformXmlToHtml(xmlStream, htmlStream, costDiscount); } } } catch (Exception ex) { errorMessage = String.Format("Failed to transform xml order file to html: '{0}'.", ex.Message); log.WriteLine(errorMessage); CleanUp(costCalculation); throw ex; } #endregion #region 7. Send 2-emails: 1st without attachment to Customer and 2nd with attachment (xml) to Backoffice try { using (var htmlStream = htmlBlob.OpenRead()) { using (var htmlReader = new StreamReader(htmlStream)) { using (var xmlStream = xmlBlob.OpenRead()) { SendEmailNotifications(customer, htmlReader.ReadToEnd(), xmlStream, String.Format("order_{0}.xml", order.ID)); } } } } catch (Exception ex) { errorMessage = String.Format("Failed to send e-mails: '{0}'.", ex.Message); log.WriteLine(errorMessage); CleanUp(costCalculation); throw ex; } #endregion #region 8. Delete blob files order_[#].xml and order_[#].html xmlBlob.Delete(); htmlBlob.Delete(); #endregion #region 9. Clean up (Order + OrderDetails) and Save changes // Decided to comment, because of having plans to purchase 2GB Database plan for storing historical data. //////db.Orders.Remove(order); //////db.SaveChanges(); #endregion CleanUp(costCalculation); } log.WriteLine("Message successfully processed."); }