public static WishlistCollection LoadForUser(Int32 userId, int maximumRows, int startRowIndex, string sortExpression) { //CREATE THE DYNAMIC SQL TO LOAD OBJECT StringBuilder selectQuery = new StringBuilder(); selectQuery.Append("SELECT"); if (maximumRows > 0) { selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString()); } selectQuery.Append(" " + Wishlist.GetColumnNames(string.Empty)); selectQuery.Append(" FROM ac_Wishlists"); selectQuery.Append(" WHERE UserId = @userId"); if (!string.IsNullOrEmpty(sortExpression)) { selectQuery.Append(" ORDER BY " + sortExpression); } Database database = Token.Instance.Database; DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); database.AddInParameter(selectCommand, "@userId", System.Data.DbType.Int32, userId); //EXECUTE THE COMMAND WishlistCollection results = new WishlistCollection(); int thisIndex = 0; int rowCount = 0; using (IDataReader dr = database.ExecuteReader(selectCommand)) { while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows))) { if (thisIndex >= startRowIndex) { Wishlist wishlist = new Wishlist(); Wishlist.LoadDataReader(wishlist, dr); results.Add(wishlist); rowCount++; } thisIndex++; } dr.Close(); } return(results); }
/// <summary> /// Moves the source wishlist to the target wishlist. /// </summary> /// <param name="sourceId">The ID of the user with the source wishlist.</param> /// <param name="targetId">The ID of the user to transfer the wishlist to.</param> /// <param name="transferEmptyWishlist">If false, the wishlist is not transferred when the source wishlist is empty. If true, the wishlist is always transferred.</param> /// <remarks>Any existing contents of the target wishlist are removed prior to transfer.</remarks> public static void Transfer(int sourceId, int targetId, bool transferEmptyWishlist) { if (sourceId != targetId) { //GET THE DEFAULT BASKET FOR THE SOURCE USER WishlistCollection sourceWishlists = WishlistDataSource.LoadForUser(sourceId); if (sourceWishlists.Count == 0) { return; } Wishlist sourceWishlist = sourceWishlists[0]; if (!transferEmptyWishlist) { //WE SHOULD NOT TRANSFER EMPTY BASKETS, COUNT THE SOURCE ITEMS int sourceCount = WishlistItemDataSource.CountForWishlist(sourceWishlist.WishlistId); if (sourceCount == 0) { return; } } //DETERMINE WHETHER USER HAS A WISHLIST ALREADY Database database = Token.Instance.Database; DbCommand command = database.GetSqlStringCommand("SELECT WishlistId FROM ac_Wishlists WHERE UserId = @targetId"); database.AddInParameter(command, "@targetId", System.Data.DbType.Int32, targetId); int targetWishlistId = AlwaysConvert.ToInt(database.ExecuteScalar(command)); if (targetWishlistId == 0) { //USER HAS NO WISHLIST, SO MOVE THE SOURCE WISHLIST TO NEW USER sourceWishlist.UserId = targetId; sourceWishlist.Save(); } else { //USER HAS A WISHLIST, JUST MOVE ITEMS FROM SOURCE TO TARGET command = database.GetSqlStringCommand("UPDATE ac_WishlistItems SET WishlistId = @targetWishlistId WHERE WishlistId = @sourceWishlistId"); database.AddInParameter(command, "@targetWishlistId", System.Data.DbType.Int32, targetWishlistId); database.AddInParameter(command, "@sourceWishlistId", System.Data.DbType.Int32, sourceWishlist.WishlistId); database.ExecuteNonQuery(command); } } }