Exemplo n.º 1
0
        public int GetRating(long userId, long linkId)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            try
            {
                return(RatingDao.FindForUserAndLink(userId, linkId).value);
            }
            catch (InstanceNotFoundException <Rating> )
            {
                return(0);
            }
            catch (DuplicateInstanceException <Rating> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
Exemplo n.º 2
0
        public FavoriteDetails GetFavorite(long userId, long linkId)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            Favorite favorite;

            try
            {
                favorite = FavoriteDao.FindForUserAndLink(userId, linkId);
            }
            catch (InstanceNotFoundException <Favorite> ex)
            {
                throw new InstanceNotFoundException <FavoriteDetails>(ex.Properties);
            }
            catch (DuplicateInstanceException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }

            return(new FavoriteDetails(favorite.favoriteId, favorite.linkId, favorite.name, favorite.description, favorite.date));
        }
Exemplo n.º 3
0
        public ListBlock <FavoriteDetails> GetFavoritesForUser(long userId, int startIndex, int count)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            ListBlock <Favorite> favorites;

            try
            {
                favorites = FavoriteDao.ListForUser(userId, startIndex, count);
            }
            catch (InstanceNotFoundException <Favorite> )
            {
                return(new ListBlock <FavoriteDetails>(startIndex, false));
            }
            catch (NoMoreItemsException <Favorite> )
            {
                return(new ListBlock <FavoriteDetails>(startIndex, false));
            }

            List <FavoriteDetails> details = new List <FavoriteDetails>();

            foreach (Favorite favorite in favorites)
            {
                details.Add(new FavoriteDetails(favorite.favoriteId, favorite.linkId, favorite.name, favorite.description, favorite.date));
            }

            return(new ListBlock <FavoriteDetails>(details, favorites.Index, favorites.HasMore));
        }
Exemplo n.º 4
0
        public long AddToFavorites(long userId, long linkId, string name, string description)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }
            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            if (FavoriteDao.ExistsForUserAndLink(userId, linkId))
            {
                throw new DuplicateInstanceException <FavoriteDetails>("userId", userId, "linkId", linkId);
            }

            Favorite favorite = Favorite.CreateFavorite(-1, userId, linkId, name, description, DateTime.Now);

            try
            {
                FavoriteDao.Create(favorite);
            }
            catch (DuplicateInstanceException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }

            return(favorite.favoriteId);
        }
Exemplo n.º 5
0
        public int CountFavoritesForUser(long userId)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            return(FavoriteDao.CountForUser(userId));
        }
Exemplo n.º 6
0
        public bool HasInFavorites(long userId, long linkId)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            return(FavoriteDao.ExistsForUserAndLink(userId, linkId));
        }
Exemplo n.º 7
0
        public void UpdateFavorite(long userId, long linkId, string name, string description)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            Favorite favorite;

            try
            {
                favorite = FavoriteDao.FindForUserAndLink(userId, linkId);
            }
            catch (InstanceNotFoundException <Favorite> ex)
            {
                throw new InstanceNotFoundException <FavoriteDetails>(ex.Properties);
            }
            catch (DuplicateInstanceException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }

            favorite.name        = name;
            favorite.description = description;

            try
            {
                FavoriteDao.Update(favorite);
            }
            catch (InstanceNotFoundException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
Exemplo n.º 8
0
        public void RemoveFromFavorites(long userId, long linkId)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            if (!LinkDao.Exists(linkId))
            {
                throw new InstanceNotFoundException <LinkDetails>("linkId", linkId);
            }

            Favorite favorite;

            try
            {
                favorite = FavoriteDao.FindForUserAndLink(userId, linkId);
            }
            catch (InstanceNotFoundException <Favorite> ex)
            {
                throw new InstanceNotFoundException <FavoriteDetails>(ex.Properties);
            }
            catch (DuplicateInstanceException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }

            try
            {
                FavoriteDao.Remove(favorite.favoriteId);
            }
            catch (InstanceNotFoundException <Favorite> ex)
            {
                throw new InternalErrorException(ex);
            }
        }
Exemplo n.º 9
0
        public long Rate(long userId, long linkId, int value)
        {
            if (!UserProfileDao.Exists(userId))
            {
                throw new InstanceNotFoundException <UserProfileDetails>("userId", userId);
            }

            Link link;

            try
            {
                link = LinkDao.Find(linkId);
            }
            catch (InstanceNotFoundException <Link> ex)
            {
                throw new InstanceNotFoundException <LinkDetails>(ex.Properties);
            }

            if (link.UserProfile.userId == userId)
            {
                throw new UserNotAuthorizedException <RatingDetails>(userId, "linkId", linkId);
            }

            if (!RatingDao.ExistsForUserAndLink(userId, linkId))
            {
                if (value == 0)
                {
                    return(-1);
                }

                Rating rating = Rating.CreateRating(-1, userId, linkId, value, DateTime.Now);
                try
                {
                    RatingDao.Create(rating);
                }
                catch (DuplicateInstanceException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                return(rating.ratingId);
            }
            else
            {
                Rating rating;
                try
                {
                    rating = RatingDao.FindForUserAndLink(userId, linkId);
                }
                catch (InstanceNotFoundException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }
                catch (DuplicateInstanceException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                if (value == 0)
                {
                    try
                    {
                        RatingDao.Remove(rating.ratingId);
                    }
                    catch (InstanceNotFoundException <Rating> ex)
                    {
                        throw new InternalErrorException(ex);
                    }
                    return(-1);
                }

                rating.value = value;
                try
                {
                    RatingDao.Update(rating);
                }
                catch (InstanceNotFoundException <Rating> ex)
                {
                    throw new InternalErrorException(ex);
                }

                return(rating.ratingId);
            }
        }