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); } }
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)); }
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)); }
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); }
public int CountFavoritesForUser(long userId) { if (!UserProfileDao.Exists(userId)) { throw new InstanceNotFoundException <UserProfileDetails>("userId", userId); } return(FavoriteDao.CountForUser(userId)); }
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)); }
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); } }
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); } }
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); } }