Ejemplo n.º 1
0
        //// Edit user
        //public UserBase UserEdit(UserEdit editedItem)
        //{
        //    if (editedItem == null) { return null; }

        //    // Fetch the object
        //    //var storedItem = ds.Items.Find(editedItem.ItemId);
        //    var storedItem = ds.Users.First(i => i.UserId == editedItem.UserId);

        //    if (storedItem == null) { return null; }

        //    // Edit object
        //    ds.Entry(storedItem).CurrentValues.SetValues(editedItem);

        //    ds.SaveChanges();

        //    return Mapper.Map<UserBase>(storedItem);
        //}


        // Edit user location
        public UserBase UserEditLocation(UserEditLocation editedItem)
        {
            //var u = HttpContext.Current.User as ClaimsPrincipal;
            //if (!HttpContext.Current.User.Identity.IsAuthenticated)
            //    throw new HttpResponseException(System.Net.HttpStatusCode.Unauthorized);

            //// Fetch the object
            //var storedItem = ds.Users.SingleOrDefault(i => i.Email == u.Identity.Name);

            var storedItem = ds.Users.SingleOrDefault(i => i.UserId == editedItem.UserId);

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

            // Edit object
            if (editedItem.Location.LocationId != 0)
            {
                var loc = ds.Locations.SingleOrDefault(e => e.LocationId == editedItem.Location.LocationId);
                ds.Entry(loc).CurrentValues.SetValues(editedItem.Location);
            }
            else
            {
                var location = Mapper.Map <Location>(editedItem.Location);
                storedItem.PreferableLocations.Add(location);
            }

            ds.SaveChanges();

            return(Mapper.Map <UserBase>(storedItem));
        }
Ejemplo n.º 2
0
        public IHttpActionResult Put(int?id, [FromBody] UserEditLocation editedItem)
        {
            // Ensure that an "editedItem" is in the entity body
            if (editedItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            // Ensure that the id value in the URI matches the id value in the entity body
            if (id.GetValueOrDefault() != editedItem.UserId)
            {
                return(BadRequest("Invalid data in the entity body"));
            }

            // Ensure that we can use the incoming data
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            else
            {
                // Attempt to update the item
                var changedItem = m.UserEditLocation(editedItem);

                // Notice the ApiController convenience methods
                if (changedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot edit the object"));
                }
                else
                {
                    // HTTP 200 with the changed item in the entity body
                    return(Ok(changedItem));
                }
            }
        }