public IHttpActionResult Post([FromBody] OccupantAdd newItem)
        {
            if (Request.GetRouteData().Values["id"] != null)
            {
                return(BadRequest("Invalid request URI"));
            }

            if (newItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var addedItem = m.OccupantAdd(newItem);

            if (addedItem == null)
            {
                return(BadRequest("Cannot add the object"));
            }

            // HTTP 201
            var uri = Url.Link("DefaultApi", new { id = addedItem.Id });

            return(Created(uri, addedItem));
        }
示例#2
0
        public OccupantBase OccupantAdd(OccupantAdd newItem)
        {
            if (newItem == null)
            {
                return(null);
            }

            var associatedTenant = ds.Tenants.Find(newItem.TenantId);

            if (associatedTenant == null)
            {
                return(null);
            }

            var associatedApartment = ds.Apartments.Find(newItem.ApartmentNumber);

            if (associatedApartment == null)
            {
                return(null);
            }

            Occupant addedItem = Mapper.Map <Occupant>(newItem);

            addedItem.Tenant = associatedTenant;

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

            return((addedItem == null) ? null : Mapper.Map <OccupantBase>(addedItem));
        }