Exemplo n.º 1
0
        public IHttpActionResult BulkInsert(ODataActionParameters parameter)
        {
            var suppliersParam = parameter.Find <IEnumerable <string> >("suppliers");

            if (suppliersParam == null)
            {
                return(BadRequest(SuppliersParameterNotPassed));
            }

            IEnumerable <Supplier> suppliers = suppliersParam.Select(s => new Supplier {
                Name = s
            });

            _context.Suppliers.AddRange(suppliers);
            _context.SaveChanges();

            var newSuppliers = new List <Supplier>();

            foreach (var supplier in _context.Suppliers)
            {
                if (suppliers.SingleOrDefault(s => s.Name == supplier.Name) != null)
                {
                    newSuppliers.Add(supplier);
                }
            }

            return(ResponseMessage(Request.CreateResponse(System.Net.HttpStatusCode.Created, newSuppliers)));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> ApplyVAT([FromODataUri] int key, ODataActionParameters parameters)
        {
            if (parameters == null)
            {
                return(BadRequest("Undefined parameter request body"));
            }

            Product product = await Task.Run(() => _database.Products.SingleOrDefault(p => p.Id == key));

            if (product == null)
            {
                return(BadRequest("Product does not exist!!!"));
            }

            int vatPercent = parameters.Find <int>("vat");

            if (vatPercent == 0)
            {
                return(Ok(product.Price));                 //No change in price if VAT not defined
            }
            //No VAT in the manufacturing city
            City manufacturingCity = parameters.Find <City>("manufacturedIn");

            if (manufacturingCity == null)
            {
                return(BadRequest("Incorrect data format or missing manufacturing city"));
            }

            IEnumerable <City> availableCities = parameters.Find <IEnumerable <City> >("availableCities");

            if (availableCities == null)
            {
                return(BadRequest("Incorrect data format or missing available cities"));
            }
            if (availableCities.SingleOrDefault(city => city.Id == manufacturingCity.Id) != null)
            {
                return(Ok(product.Price));
            }

            //Update VATted price for the Product
            product.Price = product.Price + (product.Price * vatPercent) / 100;
            return(Ok(product.Price));
        }