Example #1
0
        public ServiceRequestWithService ServiceRequestAdd(ServiceRequestAdd newItem)
        {
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                var associatedItem = ds.Services.Find(newItem.ServiceId);
                if (associatedItem == null)
                {
                    return(null);
                }

                var today = DateTime.Now;

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

                addedItem.Service = associatedItem;

                if (newItem.RequestDate == null)
                {
                    addedItem.RequestDate = today;
                }


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

                return(Mapper.Map <ServiceRequestWithService>(addedItem));
            }
        }
Example #2
0
        public IHttpActionResult Post([FromBody] ServiceRequestAdd 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.ServiceRequestAdd(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));
        }