Example #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="core"></param>
        /// <param name="itemType"></param>
        /// <param name="itemId"></param>
        /// <param name="rating"></param>
        /// <remarks>ItemRated should implement a transaction.</remarks>
        public static void LikeItem(Core core, ItemKey itemKey, LikeType like)
        {
            if (core == null)
            {
                throw new NullCoreException();
            }

            if (itemKey.Id < 1)
            {
                throw new InvalidItemException();
            }

            if (like < LikeType.Dislike || like > LikeType.Like)
            {
                throw new InvalidLikeException();
            }

            /* after 7 days release the IP for dynamics ip fairness */
            SelectQuery query = Like.GetSelectQueryStub(core, typeof(Like));
            query.AddCondition("like_item_id", itemKey.Id);
            query.AddCondition("like_item_type_id", itemKey.TypeId);
            QueryCondition qc1 = query.AddCondition("user_id", core.LoggedInMemberId);
            QueryCondition qc2 = qc1.AddCondition(ConditionRelations.Or, "like_ip", core.Session.IPAddress.ToString());
            qc2.AddCondition("like_time_ut", ConditionEquality.GreaterThan, UnixTime.UnixTimeStamp() - 60 * 60 * 24 * 7);

            /*DataTable ratingsTable = db.Query(string.Format("SELECT user_id FROM ratings WHERE rate_item_id = {0} AND rate_item_type = '{1}' AND (user_id = {2} OR (rate_ip = '{3}' AND rate_time_ut > UNIX_TIMESTAMP() - (60 * 60 * 24 * 7)))",
                itemId, Mysql.Escape(itemType), loggedInMember.UserId, session.IPAddress.ToString()));*/

            DataTable likesTable = core.Db.Query(query);

            ItemInfo ii = null;

            try
            {
                ii = new ItemInfo(core, itemKey);
            }
            catch (InvalidIteminfoException)
            {
                ii = ItemInfo.Create(core, itemKey);
            }

            if (likesTable.Rows.Count > 0)
            {
                Like liked = new Like(core, likesTable.Rows[0]);

                if (liked.Liking == like)
                {
                    throw new AlreadyLikedException();
                }

                switch (like)
                {
                    case LikeType.Like:
                        ii.DecrementDislikes();
                        ii.IncrementLikes();
                        break;
                    case LikeType.Dislike:
                        ii.DecrementLikes();
                        ii.IncrementDislikes();
                        break;
                }

                UpdateQuery uQuery = new UpdateQuery("likes");
                uQuery.AddField("like_time_ut", UnixTime.UnixTimeStamp());
                uQuery.AddField("like_liking", (sbyte)like);
                uQuery.AddField("like_ip", core.Session.IPAddress.ToString());
                uQuery.AddCondition("user_id", core.LoggedInMemberId);
                uQuery.AddCondition("like_item_id", itemKey.Id);
                uQuery.AddCondition("like_item_type_id", itemKey.TypeId);

                // commit the transaction
                core.Db.Query(uQuery);

            }
            else
            {
                switch (like)
                {
                    case LikeType.Like:
                        ii.IncrementLikes();
                        break;
                    case LikeType.Dislike:
                        ii.IncrementDislikes();
                        break;
                }

                InsertQuery iQuery = new InsertQuery("likes");
                iQuery.AddField("like_item_id", itemKey.Id);
                iQuery.AddField("like_item_type_id", itemKey.TypeId);
                iQuery.AddField("user_id", core.LoggedInMemberId);
                iQuery.AddField("like_time_ut", UnixTime.UnixTimeStamp());
                iQuery.AddField("like_liking", (sbyte)like);
                iQuery.AddField("like_ip", core.Session.IPAddress.ToString());

                // commit the transaction
                core.Db.Query(iQuery);
            }

            return;
        }