public ActionResult Edit(int?id) { if (id == null) { ViewData["Message"] = "The order you are looking for does not exist. Ensure you have the correct Order ID, and try again."; return(View("Error")); } try { Order orderModel = db.Orders.Where(m => m.ID == id).Include(o => o.Advertiser).Include(o => o.SalesPerson).Include(o => o.AccountManager).FirstOrDefault(); var currentUserID = User.Identity.GetUserId(); if (orderModel.AgentName is null) { var user = db.Users.Where(u => u.Id == currentUserID).FirstOrDefault(); orderModel.AgentName = user.Name; orderModel.AgentEmail = user.Email; } orderModel.LineItems = orderModel.LineItems.OrderBy(l => l.Creative.Select(c => c.SortOrder).FirstOrDefault()).ToArray(); foreach (var l in orderModel.LineItems) { foreach (var e in l.Creative) { if (e.GetType() == typeof(PremiumAd)) { PremiumAd p = db.CreativeSets.OfType <PremiumAd>().Where(c => c.CreativeSetID == e.CreativeSetID).Include(a => a.BannerAds).Include(a => a.NativAds).FirstOrDefault(); ((PremiumAd)e).BannerAds = p.BannerAds; ((PremiumAd)e).NativAds = p.NativAds; } } } if (orderModel == null) { ViewData["Message"] = "The order you are looking for does not exist. Ensure you have the correct Order ID, and try again."; return(View("Error")); } return(View(orderModel)); } catch (Exception ex) { ViewData["Message"] = "The order you are looking for does not exist. Ensure you have the correct Order ID, and try again."; return(View("Error")); } }
public ActionResult UpsertOrder(APIOrders order, string secret) { try { ValidationContext vc = new ValidationContext(order); // The simplest form of validation context. It contains only a reference to the object being validated. ICollection <ValidationResult> results = new List <ValidationResult>(); // Will contain the results of the validation bool isValid = Validator.TryValidateObject(order, vc, results, true); if (!isValid) { string message = "The following fields are either missing or not in the correct format:"; foreach (var r in results) { foreach (var m in r.MemberNames) { message += r.MemberNames + ","; } } message.TrimEnd(new char[] { ',' }); return(Json(new { succes = false, ErrorMessage = message })); } ApplicationDbContext context = new ApplicationDbContext(); var newOrder = false; if (secret == ConfigurationManager.AppSettings["APISecret"]) { var o = context.Orders.Where(c => c.BHIOrderNumber == order.workOrderNumber).FirstOrDefault(); if (o == null) { o = new Order(); context.Orders.Add(o); newOrder = true; } o.BHIOrderNumber = order.workOrderNumber; o.OrderName = order.orderName; o.ContractStartDate = order.contractStartDate; o.ContractEndDate = order.contractEndDate; foreach (var p in order.products) { var line = new LineItem(); if (!newOrder) { line = context.LineItems.Where(c => c.Order.BHIOrderNumber == o.BHIOrderNumber && c.Taxonomy == p.campaignName).FirstOrDefault(); } if (line == null) { line = new LineItem(); context.LineItems.Add(line); } line.Taxonomy = p.campaignName; line.Order = o; line.LocationSublocation = p.advertisingLocation + "/" + p.advertisingSubLocation; line.LocationSublocation += p.advertisingCommunity == null ? "" : "/" + p.advertisingCommunity; line.StartDate = p.applicableStartDate; line.EndDate = p.applicableEndDate; line.Product = p.productName; if (line.Creative == null || line.Creative.Count == 0) { CreativeSet set; if (p.productName.Contains("BDX Premium")) { set = new PremiumAd(); } else if (p.productName.Contains("BDX Native")) { set = new NativeAds(); } else { set = new NativeAds(); } set.SortOrder = 0; set.SetName = p.campaignName + "_Set_1"; set.Line = line; context.CreativeSets.Add(set); } } context.SaveChanges(); return(Json(new { success = true, AssetPortalID = o.ID })); } else { return(Json(new { succes = false, ErrorMessage = "Secret Key is invalid" })); } } catch (Exception ex) { return(Json(new { succes = false, message = ex.Message })); } }