示例#1
0
        // Attention 35 - Address add, requires extra checking and processing
        public AddressBase AddressAdd(AddressAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                // Must validate "Home" or "Work" address type
                var isAddressTypeValid =
                    (newItem.AddressType.Trim().ToLower() == "home" || newItem.AddressType.Trim().ToLower() == "work")
                    ? true
                    : false;
                if (!isAddressTypeValid)
                {
                    return(null);
                }

                // Must validate the associated object
                var associatedItem = ds.Employees.Find(newItem.EmployeeId);
                if (associatedItem == null)
                {
                    return(null);
                }

                // Add the new object

                // Build the Address object
                Address addedItem = Mapper.Map <Address>(newItem);

                // Set its associated item identifier
                addedItem.EmployeeId = associatedItem.Id;

                // Now, look at this next task from the perspective of the employee object
                // Set the appropriate address object
                if (newItem.AddressType.Trim().ToLower() == "home")
                {
                    associatedItem.HomeAddress = addedItem;
                }
                else
                {
                    associatedItem.WorkAddress = addedItem;
                }

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

                // Return the object
                return(Mapper.Map <AddressBase>(addedItem));
            }
        }
        // POST: api/Addresses
        public IHttpActionResult Post([FromBody] AddressAdd 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.AddressAdd(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 <AddressBase>(uri, addedItem));
                }
            }
            else
            {
                // HTTP 400
                return(BadRequest(ModelState));
            }
        }
示例#3
0
        public AddressBase AddAddress(AddressAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return null;
            }
            else
            {
                // Must validate "Home" or "Work" address type
                var isAddressTypeValid =
                    (newItem.AddressType.Trim().ToLower() == "home" || newItem.AddressType.Trim().ToLower() == "work")
                    ? true
                    : false;
                if (!isAddressTypeValid) { return null; }

                // Must validate the associated object
                var associatedItem = ds.Employees.Find(newItem.EmployeeId);
                if (associatedItem == null)
                {
                    return null;
                }

                // Add the new object

                // Build the Address object
                Address addedItem = Mapper.Map<Address>(newItem);

                // Set its associated item identifier
                addedItem.EmployeeId = associatedItem.Id;

                // Now, look at this next task from the perspective of the employee object
                // Set the appropriate address object
                if (newItem.AddressType.Trim().ToLower() == "home")
                {
                    associatedItem.HomeAddress = addedItem;
                }
                else
                {
                    associatedItem.WorkAddress = addedItem;
                }

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

                // Return the object
                return Mapper.Map<AddressBase>(addedItem);
            }
        }