Ejemplo n.º 1
0
        public JObject deleteProductCategory(JObject request)
        {
            //Get arguments
            request.TryGetValue("categoryID", out JToken idValue);
            if (idValue == null || idValue.Type != JTokenType.String)
            {
                return(Templates.MissingArguments("productID"));
            }

            // Prepare values
            string categoryID = idValue.ToString();

            //Check if product exists
            ProductCategory category = GetObject <ProductCategory>(categoryID);

            if (category == null)
            {
                return(Templates.NoSuchProductCategory(categoryID));
            }

            // Delete category and relate
            category.Delete(Connection);
            category.GetName(Connection).Delete(Connection);

            //Create base response
            return(new JObject()
            {
                { "reason", null },
                { "success", true }
            });
        }
Ejemplo n.º 2
0
        public virtual void TestDeleteListElements2()
        {
            string baseName = GetBaseName();

            NeoDatis.Odb.ODB odb     = Open(baseName);
            Catalog          catalog = new Catalog
                                           ("Fnac");
            ProductCategory books = new ProductCategory
                                        ("Books");

            books.GetProducts().Add(new Product
                                        ("Book1", new System.Decimal(10.1)));
            books.GetProducts().Add(new Product
                                        ("Book2", new System.Decimal(10.2)));
            books.GetProducts().Add(new Product
                                        ("Book3", new System.Decimal(10.3)));
            ProductCategory computers = new ProductCategory
                                            ("Computers");

            computers.GetProducts().Add(new Product
                                            ("MacBook", new System.Decimal(1300.1)));
            computers.GetProducts().Add(new Product
                                            ("BookBookPro", new System.Decimal(2000.2)));
            computers.GetProducts().Add(new Product
                                            ("MacMini", new System.Decimal(1000.3)));
            catalog.GetCategories().Add(books);
            catalog.GetCategories().Add(computers);
            odb.Store(catalog);
            odb.Close();
            odb = Open(baseName);
            NeoDatis.Odb.Objects <Catalog> objects = odb.GetObjects <Catalog>();
            Println(objects.Count + " catalog(s)");
            while (objects.HasNext())
            {
                Catalog c = (Catalog
                             )objects.Next();
                System.Collections.Generic.IList <NeoDatis.Odb.Test.VO.Arraycollectionmap.Catalog.ProductCategory> pCategories = c.GetCategories();
                Println(c.GetCategories().Count + " product categories");
                for (int j = 0; j < pCategories.Count; j++)
                {
                    ProductCategory pc = (ProductCategory
                                          )pCategories[j];
                    Println("\tProduct Category : " + pc.GetName() + " : " + pc.GetProducts().Count +
                            " products");
                    for (int k = 0; k < pc.GetProducts().Count; k++)
                    {
                        Product p = pc.GetProducts()[k];
                        Println("\t\tProduct " + p.GetName());
                        odb.Delete(p);
                    }
                    odb.Delete(pc);
                }
                odb.Delete(c);
            }
            odb.Close();
            odb = Open(baseName);
            NeoDatis.Odb.Objects <Catalog>         catalogs          = odb.GetObjects <Catalog>();
            NeoDatis.Odb.Objects <ProductCategory> productCategories = odb.GetObjects <ProductCategory>();
            NeoDatis.Odb.Objects <Product>         products          = odb.GetObjects <Product>();
            AssertTrue(catalogs.Count == 0);
            AssertTrue(productCategories.Count == 0);
            AssertTrue(products.Count == 0);
            DeleteBase(baseName);
        }
        public JObject updateProductCategory(JObject request)
        {
            //Validate arguments
            string  categoryID;
            string  newCategoryID = null;
            JObject names         = null;

            request.TryGetValue("categoryID", out JToken categoryIDValue);
            request.TryGetValue("newCategoryID", out JToken newCategoryIDValue);
            request.TryGetValue("name", out JToken nameValue);
            if (categoryIDValue == null || categoryIDValue.Type != JTokenType.String)
            {
                return(Templates.MissingArguments("categoryID"));
            }
            else
            {
                categoryID = categoryIDValue.ToObject <string>();
                if (categoryID == "default" || categoryID == "uncategorized")
                {
                    return(Templates.InvalidArgument("categoryID"));
                }
            }
            if (newCategoryIDValue != null && newCategoryIDValue.Type == JTokenType.String)
            {
                newCategoryID = newCategoryIDValue.ToObject <string>();
            }
            if (nameValue != null && nameValue.Type == JTokenType.Object)
            {
                names = nameValue.ToObject <JObject>();
            }

            //Get product, if it exists
            ProductCategory category = GetObject <ProductCategory>(categoryID);

            if (category == null)
            {
                return(Templates.NoSuchProductCategory(categoryID));
            }

            ///////////////LanguageItem
            //Edit the LanguageItem if needed;
            LanguageItem item = category.GetName(Connection);

            if (names != null)
            {
                if (names.TryGetValue("en", out JToken enValue))
                {
                    if (enValue.Type == JTokenType.String)
                    {
                        item.en = enValue.ToObject <string>();
                    }
                }
                if (names.TryGetValue("nl", out JToken nlValue))
                {
                    if (nlValue.Type == JTokenType.String)
                    {
                        item.nl = nlValue.ToObject <string>();
                    }
                }
                if (names.TryGetValue("ar", out JToken arValue))
                {
                    if (arValue.Type == JTokenType.String)
                    {
                        item.ar = arValue.ToObject <string>();
                    }
                }
                item.Update(Connection);
            }

            //If a new product ID was specified, check if it already exists. If it doesn't, change the product ID.
            if (newCategoryID != null)
            {
                ProductCategory newProduct = GetObject <ProductCategory>(newCategoryID);
                if (newProduct != null)
                {
                    return(Templates.AlreadyExists(categoryID));
                }
                else
                {
                    item.Id = newCategoryID + "_name";
                    item.Update(Connection);
                    category.Name = item.Id;
                    category.UpdateTrace();
                    category.Id = newCategoryID;
                }
            }

            category.Update(Connection);

            //Create response
            return(new JObject()
            {
                { "reason", null },
                { "success", true }
            });
        }