Commits

Anonymous committed b0d0ba0

Backed out changeset: 14853bbe1832

Comments (0)

Files changed (1)

Controllers/ProductAdminController.cs

 using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Web.Mvc;
 using Nwazet.Commerce.Models;
+using Nwazet.Commerce.Services;
 using Orchard;
 using Orchard.ContentManagement;
 using Orchard.Core.Common.Models;
     public class ProductAdminController : Controller {
         private readonly IContentManager _contentManager;
         private readonly ISiteService _siteService;
+        private readonly IWorkContextAccessor _wca;
 
         public ProductAdminController(
             IOrchardServices services,
             IContentManager contentManager,
             ISiteService siteService,
+            IWorkContextAccessor wca,
             IShapeFactory shapeFactory) {
             Services = services;
             _contentManager = contentManager;
             _siteService = siteService;
+            _wca = wca;
             T = NullLocalizer.Instance;
             Shape = shapeFactory;
         }
 
         public ActionResult List(ListContentsViewModel model, PagerParameters pagerParameters) {
             var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters);
-            var query = _contentManager.Query<ProductPart>(VersionOptions.Latest);
+            var query = _contentManager.Query<ProductPart, ProductPartRecord>(VersionOptions.Latest);
 
             switch (model.Options.OrderBy) {
                 case ContentsOrder.Modified:
-                    //query = query.OrderByDescending<ContentPartRecord, int>(ci => ci.ContentItemRecord.Versions.Single(civr => civr.Latest).Id);
-                    query = query.OrderByDescending<CommonPartRecord>(cr => cr.ModifiedUtc);
+                    query.OrderByDescending<CommonPartRecord, DateTime?>(cr => cr.ModifiedUtc);
                     break;
                 case ContentsOrder.Published:
-                    query = query.OrderByDescending<CommonPartRecord>(cr => cr.PublishedUtc);
+                    query.OrderByDescending<CommonPartRecord, DateTime?>(cr => cr.PublishedUtc);
                     break;
                 case ContentsOrder.Created:
-                    //query = query.OrderByDescending<ContentPartRecord, int>(ci => ci.Id);
-                    query = query.OrderByDescending<CommonPartRecord>(cr => cr.CreatedUtc);
+                    query.OrderByDescending<CommonPartRecord, DateTime?>(cr => cr.CreatedUtc);
                     break;
             }
 
             return View((object)viewModel);
         }
 
-
-
+        [HttpPost]
+        public ActionResult RemoveOne(int id) {
+            var product = _contentManager.Get<ProductPart>(id);
+            product.Inventory--;
+            Dictionary<string, int> newInventory;
+            IBundleService bundleService;
+            if (_wca.GetContext().TryResolve(out bundleService)) {
+                var affectedBundles = _contentManager.Query<BundlePart, BundlePartRecord>()
+                    .Where(b => b.Products.Any(p => p.ContentItemRecord.Id == product.Id))
+                    .WithQueryHints(new QueryHints().ExpandParts<ProductPart>())
+                    .List();
+                newInventory = affectedBundles.ToDictionary(
+                    b => b.As<ProductPart>().Sku,
+                    b => bundleService.GetProductQuantitiesFor(b).Min(p => p.Product.Inventory / p.Quantity));
+            } else {
+                newInventory = new Dictionary<string, int>(1);
+            }
+            newInventory.Add(product.Sku, product.Inventory);
+            return new JsonResult {
+                Data = newInventory
+            };
+        }
     }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.