Ejemplo n.º 1
0
        public ApartmentWithUnit ApartmentAdd(ApartmentAdd newItem)
        {
            if (newItem == null)
            {
                return(null);
            }

            else
            {
                var apartment = ds.Apartments.Find(newItem.ApartmentNumber);
                if (apartment != null)
                {
                    return(null);
                }

                var associatedUnit = ds.Units.Find(newItem.UnitId);
                if (associatedUnit == null)
                {
                    return(null);
                }

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

                addedItem.Unit = associatedUnit;

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

                return(Mapper.Map <ApartmentWithUnit>(addedItem));
            }
        }
        public IHttpActionResult Post([FromBody] ApartmentAdd 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 leaseInfo = m.LeaseGetByAptNumber(newItem.ApartmentNumber);

            if (leaseInfo != null)
            {
                return(Content(HttpStatusCode.Conflict, "Apartment Already Exists"));
            }

            var addedItem = m.ApartmentAdd(newItem);

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

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

            return(Created(uri, addedItem));
        }