Esempio n. 1
0
        // Add new vehicle
        public VehicleBase AddVehicle(VehicleAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                // Must validate the Manufacturer
                var associatedItem = ds.Manufacturers.Find(newItem.ManufacturerId);
                if (associatedItem == null)
                {
                    return(null);
                }

                // Add the new object
                Vehicle addedItem = Mapper.Map <Vehicle>(newItem);
                addedItem.Manufacturer = associatedItem;

                ds.Vehicles.Add(addedItem);
                ds.SaveChanges();

                // Return the object
                return(Mapper.Map <VehicleBase>(addedItem));
            }
        }
Esempio n. 2
0
        // Add new vehicle
        public VehicleBase AddVehicle(VehicleAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return null;
            }
            else
            {
                // Must validate the Manufacturer
                var associatedItem = ds.Manufacturers.Find(newItem.ManufacturerId);
                if (associatedItem == null)
                {
                    return null;
                }

                // Add the new object
                Vehicle addedItem = Mapper.Map<Vehicle>(newItem);
                addedItem.Manufacturer = associatedItem;

                ds.Vehicles.Add(addedItem);
                ds.SaveChanges();

                // Return the object
                return Mapper.Map<VehicleBase>(addedItem);
            }
        }
Esempio n. 3
0
        // POST: api/Vehicles
        public IHttpActionResult Post([FromBody] VehicleAdd newItem)
        {
            // Ensure that the URI is clean (and does not have an id parameter)
            if (Request.GetRouteData().Values["id"] != null)
            {
                return(BadRequest("Invalid request URI"));
            }

            // Ensure that a "newItem" is in the entity body
            if (newItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            // Ensure that we can use the incoming data
            if (ModelState.IsValid)
            {
                // Attempt to add the new object
                var addedItem = m.AddVehicle(newItem);

                // Notice the ApiController convenience methods
                if (addedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot add the object"));
                }
                else
                {
                    // HTTP 201 with the new object in the entity body
                    // Notice how to create the URI for the Location header

                    var uri = Url.Link("DefaultApi", new { id = addedItem.Id });
                    return(Created <VehicleBase>(uri, addedItem));
                }
            }
            else
            {
                // HTTP 400
                return(BadRequest(ModelState));
            }
        }