protected override void ExecuteWorkImplementation() { var favoriteLabel = m_favoritesRepository.FindById <FavoriteLabel>(m_favoriteLabelId); OwnershipHelper.CheckItemOwnership(favoriteLabel.User.Id, m_userId); if (favoriteLabel.IsDefault) { throw new MainServiceException(MainServiceErrorCode.CannotRemoveDefaultFavoriteLabel, "Can't remove default favorite label"); } m_favoritesRepository.Delete(favoriteLabel); }
public void UpdateFavoriteLabel(long labelId, string name, string color) { var now = DateTime.UtcNow; var user = TryGetUser(); var favoriteLabel = m_favoritesRepository.FindById <FavoriteLabel>(labelId); CheckItemOwnership(favoriteLabel.User.Id, user); if (favoriteLabel.IsDefault) { throw new ArgumentException("User can't modify default favorite label"); } favoriteLabel.Name = name; favoriteLabel.Color = color; favoriteLabel.LastUseTime = now; m_favoritesRepository.Update(favoriteLabel); }
protected FavoriteLabel GetFavoriteLabelAndCheckAuthorization(long?labelId, int userId) { if (labelId == null) { var defaultLabel = m_favoritesRepository.GetDefaultFavoriteLabel(userId); return(defaultLabel); } var label = m_favoritesRepository.FindById <FavoriteLabel>(labelId.Value); if (label == null) { throw new MainServiceException(MainServiceErrorCode.FavoriteLabelNotFound, "FavoriteLabel not found"); } if (label.User.Id != userId) { throw new MainServiceException(MainServiceErrorCode.UserDoesNotOwnLabel, "Current user doesn't own this FavoriteLabel"); } return(label); }
protected override long ExecuteWorkImplementation() { var now = DateTime.UtcNow; var user = m_favoritesRepository.Load <User>(m_userId); FavoriteLabel favoriteLabel; if (m_favoriteLabelId == null) { favoriteLabel = new FavoriteLabel { IsDefault = m_createDefault, User = user, // Other properties are set outside IF block }; } else { favoriteLabel = m_favoritesRepository.FindById <FavoriteLabel>(m_favoriteLabelId.Value); OwnershipHelper.CheckItemOwnership(favoriteLabel.User.Id, m_userId); if (favoriteLabel.IsDefault) { throw new MainServiceException(MainServiceErrorCode.CannotModifyDefaultFavoriteLabel, "User can't modify default favorite label"); } } favoriteLabel.Name = m_data.Name; favoriteLabel.Color = m_data.Color; favoriteLabel.LastUseTime = now; m_favoritesRepository.Save(favoriteLabel); return(favoriteLabel.Id); }