Exemplo n.º 1
0
        /// <summary>
        /// 找到该用户发现house被赞的数目
        /// </summary>
        /// <param name="ownerId"></param>
        /// <returns></returns>
        public int FindHouseLikedCountByUser(Guid ownerId)
        {
            var query = this.getNewQueryObject();
            var dict  = new Dictionary <string, object>
            {
                { "OwnerId", ownerId }
            };

            query.AppendQuery(dict, QueryLogic.And);
            var houses           = EntityRepository.FindAsQueryable(query);
            int housesLikedCount = 0;

            foreach (var house in houses)
            {
                housesLikedCount += LrfService.GetLRFCount(house.Id, LRFType.Like);
            }
            return(housesLikedCount);
        }
        //rate
        /// <summary>
        /// rate the entity, make sure you have the RateTimes and Rating field in you entity, it will update the ratings in the entity
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entityService"></param>
        /// <param name="targetId"></param>
        /// <param name="userId"></param>
        /// <param name="lrfService"></param>
        /// <param name="rating"></param>
        /// <returns></returns>
        public static float Rate <T>(this EntityService <T> entityService, Guid targetId, Guid userId,
                                     LikeRateFavService lrfService, float rating) where T : Entity
        {
            var doIRate = lrfService.DoILikeRateFav(userId, targetId, LRFType.Rate);
            //calc new rating
            Dictionary <string, object> queryDict = new Dictionary <string, object>();

            queryDict.Add("_id", targetId);
            QueryObject <T> query = new QueryObject <T>(entityService.EntityRepository);

            query.AppendQuery(queryDict, QueryLogic.And);

            var entity       = entityService.FindOneById(targetId);
            var oldRating    = (float)entity.GetType().GetProperty("Rating").GetValue(entity, null);
            var oldRateTimes = lrfService.GetLRFCount(targetId, LRFType.Rate);

            if (!doIRate)
            {
                //my first time rating
                var newRating = (oldRating * oldRateTimes + rating) / (oldRateTimes + 1);
                lrfService.Rate(userId, targetId, rating);
                ((MongoDBRepository <T>)entityService.EntityRepository).Update(query,
                                                                               Update.Set("Rating", newRating));
                return(newRating);
            }
            else
            {
                //get my old rating
                var myOldRating = lrfService.GetMyRatingFor(userId, targetId);
                //calc the new one
                var newRating = (oldRating * oldRateTimes - myOldRating + rating) / (oldRateTimes);
                lrfService.Rate(userId, targetId, rating);
                ((MongoDBRepository <T>)entityService.EntityRepository).Update(query,
                                                                               Update.Set("Rating", newRating));
                lrfService.Rate(userId, targetId, rating);
                return(newRating);
            }
        }