Example #1
0
        public IHttpActionResult PostProduct(Product product)
        {
            if (product != null)
            {
                if (string.IsNullOrWhiteSpace(product.Code))
                {
                    ModelState.AddModelError(key: "ProductCode", errorMessage: "Product must have a code.");
                }

                if (ModelState.IsValid)
                {
                    db.Products.Add(product);
                    db.Commit();

                    return Created("products/code/" + product.Code, new { code = product.Code });
                }

                return BadRequest(ModelState);
            }
            return Conflict();
        }
Example #2
0
        public IHttpActionResult Rebrand(string code, [FromBody]dynamic model)
        {
            if (model != null)
            {
                Product original = db.Products.GetAll()
                    .Where(prod => !prod.Discontinued)
                    .FirstOrDefault(prod => prod.Code == code);

                if (original != null)
                {
                    Product rebranded = new Product
                    {
                        DisplayName = model.Name,
                        ReorderLevel = original.ReorderLevel,
                        CategoryId = original.CategoryId,
                        SupplierId = original.SupplierId
                    };
                    db.Products.Add(rebranded);
                    db.Products.Delete(original);

                    db.Commit();

                    return Created(location: "products/code/" + code, content: new { Result = rebranded });
                }

                ModelState.AddModelError(key: "ProductCode", errorMessage: code + " not found or has been discontinued.");
                return BadRequest(ModelState);
            }
            return Conflict();
        }