Example #1
0
        /// <summary>
        /// 获取新的完整状态树
        /// </summary>
        /// <param name="state">当前 UI 状态</param>
        /// <param name="userManager"><see cref="KeylolUserManager"/></param>
        /// <param name="dbContext"><see cref="KeylolDbContext"/></param>
        /// <param name="coupon"><see cref="CouponProvider"/></param>
        /// <param name="cachedData"><see cref="CachedDataProvider"/></param>
        /// <param name="pointIdCode">据点识别码</param>
        /// <param name="authorIdCode">作者识别码</param>
        /// <param name="userIdCode">用户识别码</param>
        /// <param name="sidForAuthor">文章在作者名下的序号</param>
        /// <param name="keyword">搜索关键字</param>
        /// <returns>完整状态树</returns>
        public static async Task <Root> Locate(string state, [Injected] KeylolUserManager userManager,
                                               [Injected] KeylolDbContext dbContext, [Injected] CouponProvider coupon,
                                               [Injected] CachedDataProvider cachedData, string pointIdCode = null, string authorIdCode = null,
                                               string userIdCode = null, int sidForAuthor = 0, string keyword = null)
        {
            var root          = new Root();
            var currentUserId = StateTreeHelper.GetCurrentUserId();
            var isOperator    = StateTreeHelper.GetCurrentUser().IsInRole(KeylolRoles.Operator);

            if (await StateTreeHelper.CanAccessAsync <Root>(nameof(CurrentUser)))
            {
                var user = await userManager.FindByIdAsync(currentUserId);

                root.CurrentUser = await CurrentUser.CreateAsync(user, userManager, dbContext, coupon, cachedData);
            }

            switch (state)
            {
            case "entrance":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Auto, dbContext, cachedData);

                break;

            case "entrance.discovery":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Discovery, dbContext, cachedData);

                break;

            case "entrance.points":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Points, dbContext, cachedData);

                break;

            case "entrance.timeline":
                root.Entrance = await EntranceLevel.CreateAsync(currentUserId,
                                                                States.Entrance.EntrancePage.Timeline, dbContext, cachedData);

                break;

            case "aggregation.point":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Auto, dbContext, cachedData)
                };
                break;

            case "aggregation.point.frontpage":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Frontpage, dbContext, cachedData)
                };
                break;

            case "aggregation.point.intel":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Intel, dbContext, cachedData)
                };
                break;

            case "aggregation.point.product":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Product, dbContext, cachedData)
                };
                break;

            case "aggregation.point.timeline":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.Timeline, dbContext, cachedData)
                };
                break;

            case "aggregation.point.edit.info":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.EditInfo, dbContext, cachedData)
                };
                break;

            case "aggregation.point.edit.style":
                root.Aggregation = new AggregationLevel
                {
                    Point = await PointLevel.CreateAsync(currentUserId, pointIdCode,
                                                         States.Aggregation.Point.EntrancePage.EditStyle, dbContext, cachedData)
                };
                break;

            case "aggregation.user":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Auto,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.dossier":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Dossier,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.people":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.People,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.timeline":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Timeline,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "aggregation.user.edit":
                root.Aggregation = new AggregationLevel
                {
                    User = await UserLevel.CreateAsync(currentUserId, userIdCode, EntrancePage.Edit,
                                                       dbContext, cachedData, userManager)
                };
                break;

            case "content.article":
                root.Content = new ContentLevel
                {
                    Article =
                        await
                        States.Content.Article.ArticlePage.CreateAsync(authorIdCode, sidForAuthor, currentUserId,
                                                                       isOperator, dbContext, cachedData, userManager)
                };
                break;

            case "content.activity":
                root.Content = new ContentLevel
                {
                    Activity = await ActivityPage.CreateAsync(authorIdCode, sidForAuthor, currentUserId,
                                                              isOperator, dbContext, cachedData, userManager)
                };
                break;

            case "post-office.unread":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        Unread = await UnreadPage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "post-office.social-activity.comment":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Comment = await CommentPage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.social-activity.like":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Like = await LikePage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.social-activity.subscriber":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        SocialActivity = new SocialActivityLevel
                        {
                            Subscriber = await SubscriberPage.CreateAsync(currentUserId, dbContext, cachedData)
                        }
                    }
                }
                ;
                break;

            case "post-office.missive":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(PostOffice)))
                {
                    root.PostOffice = new PostOfficeLevel
                    {
                        Missive = await MissivePage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "coupon.detail":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Detail = await DetailPage.CreateAsync(currentUserId, dbContext, userManager)
                    }
                }
                ;
                break;

            case "coupon.store":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Store =
                            await StorePage.CreateAsync(currentUserId, dbContext, cachedData, userManager, coupon)
                    }
                }
                ;
                break;

            case "coupon.ranking":
                if (await StateTreeHelper.CanAccessAsync <Root>(nameof(Coupon)))
                {
                    root.Coupon = new CouponLevel
                    {
                        Ranking = await RankingPage.CreateAsync(currentUserId, dbContext, cachedData)
                    }
                }
                ;
                break;

            case "search.point":
                root.Search = new SearchLevel
                {
                    Point = await PointPage.CreateAsync(currentUserId, keyword, dbContext, cachedData)
                };
                break;

            case "search.article":
                root.Search = new SearchLevel
                {
                    Article = await ArticlePage.CreateAsync(keyword, dbContext, cachedData)
                };
                break;

            case "search.user":
                root.Search = new SearchLevel
                {
                    User = await UserPage.CreateAsync(currentUserId, keyword, dbContext, cachedData)
                };
                break;

            default:
                throw new NotSupportedException("Not supported state.");
            }
            return(root);
        }