Ejemplo n.º 1
0
        public SaveUserFavoriteResponse SaveUserFavorite(SaveUserFavoriteRequest request)
        {
            var response = new SaveUserFavoriteResponse()
            {
                Success = false, Message = "Unable to save user favorite"
            };

            try
            {
                //Check to see if we have already created the 5 digit unique user code.
                //Not going to check to see if we get a duplicate code for this exercise
                var userCodeExists = request.WeathermanEntities.UserCodes.Any(w => w.UserId == request.UserId);

                //Will add the code if it does not exist for the logged in user.
                if (!userCodeExists)
                {
                    var userCode = new UserCode {
                        UserId = request.UserId
                    };
                    var typingMonkey = new TypingMonkey();
                    userCode.Code = typingMonkey.TypeAway(5);
                    request.WeathermanEntities.UserCodes.Add(userCode);
                }

                var savedLocation = new SavedLocation
                {
                    //Did not get a chance to implement saving the city yet.
                    City    = string.Empty,
                    ZipCode = request.PostalCode,
                    UserId  = request.UserId
                };
                request.WeathermanEntities.SavedLocations.Add(savedLocation);
                request.WeathermanEntities.SaveChanges();
            }
            catch (Exception exception)
            {
                response.Success   = false;
                response.Exception = exception;
                return(response);
            }
            response.Success = true;
            response.Message = string.Empty;
            return(response);
        }
Ejemplo n.º 2
0
        public ActionResult SaveFavorite(string zipCodeSearch)
        {
            //Get the currently logged in user.  Better error trapping could be used here.
            //Even though "Authorize" is set, still should be checking to see if the User.Identity call
            //is available.
            var userId  = new Guid(User.Identity.GetUserId());
            var request = new SaveUserFavoriteRequest
            {
                WeathermanEntities = _db,
                PostalCode         = zipCodeSearch,
                UserId             = userId
            };
            var response = FavoritesService.SaveUserFavorite(request);

            //if our service call succeeds, send the user to the Favorites view, otherwise just put them back
            //at the Index page.
            //A more detailed explanation should be added to the UI as to why their "Favorite"
            //was not saved.
            return(RedirectToAction(response.Success ? "Favorites" : "Index"));
        }