Пример #1
0
        /// <summary>
        /// Process returns a JSON response list of folders
        /// </summary>
        public void Process(UriWrapper uri, IHttpProcessor processor, User user)
        {
            // Shortcut to favorites repository
            IFavoriteRepository favoriteRepository = Injection.Kernel.Get <IFavoriteRepository>();

            // Lists of favorites and items associated
            IList <IItem>    items     = new List <IItem>();
            IList <Favorite> favorites = new List <Favorite>();

            // If no action specified, read favorites
            if (uri.Action == null || uri.Action == "read")
            {
                // Get this users's favorites
                favorites = favoriteRepository.FavoritesForUserId((int)user.UserId);

                // Get the items associated with their favorites
                items = favoriteRepository.ItemsForFavorites(favorites);

                // Send response
                processor.WriteJson(new FavoritesResponse(null, items, favorites));
                return;
            }

            // Verify ID present for remaining actions
            if (uri.Id == null)
            {
                processor.WriteJson(new FavoritesResponse("ID required for modifying favorites", null, null));
                return;
            }

            // create - add favorites
            if (uri.Action == "create")
            {
                favoriteRepository.AddFavorite((int)user.UserId, (int)uri.Id, null);
                processor.WriteJson(new FavoritesResponse(null, items, favorites));
                return;
            }

            // delete - remove favorites
            if (uri.Action == "delete")
            {
                // Grab favorite to delete, verify its ownership
                Favorite fav = favoriteRepository.FavoriteForId((int)uri.Id);
                if (fav.FavoriteUserId != user.UserId)
                {
                    processor.WriteJson(new FavoritesResponse("Cannot delete another user's favorite", null, null));
                    return;
                }

                // Remove favorite
                favoriteRepository.DeleteFavorite((int)uri.Id);
                processor.WriteJson(new FavoritesResponse(null, items, favorites));
                return;
            }

            // Invalid action
            processor.WriteJson(new FavoritesResponse("Invalid action specified: " + uri.Action, null, null));
            return;
        }
Пример #2
0
 public async Task <ActionResult <Favorite> > AddFavorite([FromBody] Favorite favorite)
 {
     try
     {
         return(Ok(await _repository.AddFavorite(favorite)));
     }
     catch (Exception exc)
     {
         _logger.LogError($"Error: {exc}");
         // transaction.Rollback();
         return(NotFound());
     }
 }
Пример #3
0
        public void SetParameterFavorite(IParameter parameter, bool isFavorite)
        {
            var parameterPath = _entityPathResolver.PathFor(parameter);

            if (isFavorite)
            {
                _favoriteRepository.AddFavorite(parameterPath);
                _eventPublisher.PublishEvent(new AddParameterToFavoritesEvent(parameterPath));
            }
            else
            {
                _favoriteRepository.RemoveFavorite(parameterPath);
                _eventPublisher.PublishEvent(new RemoveParameterFromFavoritesEvent(parameterPath));
            }
        }
Пример #4
0
 public void AddFavorite(Favorite favorite)
 {
     _favoriteRepository.AddFavorite(favorite);
 }