public static bool ToggleLike <T>(this EntityService <T> entityService, Guid targetId, Guid userId,
                                          LikeRateFavService lrfService) where T : Entity
        {
            var result = lrfService.ToggleLike(userId, targetId);

            if (result)
            {
                //fave times+1, make sure your entity has this field
                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);
                ((MongoDBRepository <T>)entityService.EntityRepository).Update(query,
                                                                               Update.Inc("LikeTimes", 1));
            }
            else
            {
                //fave times+1, make sure your entity has this field
                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);
                ((MongoDBRepository <T>)entityService.EntityRepository).Update(query,
                                                                               Update.Inc("LikeTimes", -1));
            }
            return(result);
        }
Ejemplo n.º 2
0
 public HouseService(IRepository <House> repository, BlogPostService checkinService, EntityService <OldHouseUserProfile> profileSerice,
                     LikeRateFavService lrfService, UserManager <OldHouseUser> userManger, MessageService feedService) : base(repository)
 {
     CheckInService = checkinService;
     CheckInService.RegisterField <CheckIn>(new List <string>());
     ProfileService = profileSerice;
     //remember to register user private filds here
     MyUserManager = userManger;
     LrfService    = lrfService;
     //use the profile repository
     FollowService = new FollowService <OldHouseUserProfile>(ProfileService.EntityRepository);
     FeedService   = feedService;
     registerHouse();
     registerUserProfile();
     instence = this;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// config the business logic
        /// </summary>
        public static void ConfigBusiness()
        {
            //todo ioc is really needed here
            string database = "OldHouseDb";//"OldHouseDbProduction"
            MongoDBRepository <House>               houseDb   = new MongoDBRepository <House>(@"mongodb://127.0.0.1:27017", database, "House");
            MongoDBRepository <BlogPostEntity>      CheckInDb = new MongoDBRepository <BlogPostEntity>(@"mongodb://127.0.0.1:27017", database, "CheckIn");
            MongoDBRepository <LikeRateFavEntity>   LrfDb     = new MongoDBRepository <LikeRateFavEntity>(@"mongodb://127.0.0.1:27017", database, "LikeRateFavorite");
            MongoDBRepository <OldHouseUser>        UserDb    = new MongoDBRepository <OldHouseUser>(@"mongodb://127.0.0.1:27017", database, "OldHouseUser");
            MongoDBRepository <OldHouseUserProfile> ProfileDb = new MongoDBRepository <OldHouseUserProfile>(@"mongodb://127.0.0.1:27017", database, "OldHouseUserProfile");
            MongoDBRepository <Message>             feedDb    = new MongoDBRepository <Message>(@"mongodb://127.0.0.1:27017", database, "Feed");

            MongoDBRepository <FeedBackEntity> FeedbackDb = new MongoDBRepository <FeedBackEntity>(@"mongodb://127.0.0.1:27017", database, "Feedback");
            MongoDBRepository <Article>        ArticleDb  = new MongoDBRepository <Article>(@"mongodb://127.0.0.1:27017", database, "Article");

            MongoDBRepository <Event>      eventDb      = new MongoDBRepository <Event>(@"mongodb://127.0.0.1:27017", database, "Event");
            MongoDBRepository <Subscriber> subscriberDb = new MongoDBRepository <Subscriber>(@"mongodb://127.0.0.1:27017", database, "Subscriber");

            //string handlerRelativePath = System.AppDomain.CurrentDomain.BaseDirectory.Substring(0, System.AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\').LastIndexOf(@"\")) + @"\OldHouse.EventHandlers\bin\Debug";
            //EventService.InitService(eventDb, subscriberDb, 100, 1000, handlerRelativePath);
            EventService.InitService(eventDb, subscriberDb, 100, 1000, HttpRuntime.BinDirectory + @"\..\OldHouseEventHandler");

            //a like rate fav service shared by all entity service
            var shareedLrfService = new LikeRateFavService(LrfDb);

            MyFeedbackService = new FeedbackService(FeedbackDb);
            MyArticleService  = new ArticleService(ArticleDb);
            MyHouseService    = new HouseService(
                houseDb,
                new BlogPostService(CheckInDb, shareedLrfService),
                new EntityService <OldHouseUserProfile>(ProfileDb),
                shareedLrfService,
                new UserManager <OldHouseUser>(UserDb),
                new MessageService(feedDb)
                );
            ConfigDtoMapping();

            hotFix();
        }
        //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);
            }
        }