示例#1
0
        private async Task <GetOrganizationResponse> GetOrganization(Organization organization)
        {
            PostServicesBase.AssertCanViewOrganization(Db, organization, SessionAs <AuthUserSession>(), out _);

            var members = await Db.SelectAsync <OrganizationMember>(x => x.OrganizationId == organization.Id && (x.IsOwner || x.IsModerator));

            return(new GetOrganizationResponse
            {
                Cache = Stopwatch.GetTimestamp(),
                Id = organization.Id,
                Slug = organization.Slug,
                Organization = organization,
                Labels = await Db.SelectAsync <OrganizationLabel>(x => x.OrganizationId == organization.Id),
                Categories = (await Db.SelectAsync <Category>(x => x.OrganizationId == organization.Id && x.Deleted == null)).OrderBy(x => x.Name).ToList(),
                Owners = members.Where(x => x.IsOwner).ToList(),
                Moderators = members.Where(x => x.IsModerator).ToList(),
                MembersCount = PostServicesBase.GetOrganizationMembersCount(organization.Id), //display only, can be stale
            });
        }
示例#2
0
        public object Any(Overview request)
        {
            if (request.Reload)
            {
                Cache.FlushAll();
            }

            var topTechByCategory = GetTopTechByCategory();

            var map = new Dictionary <string, List <TechnologyInfo> >();

            foreach (var tech in topTechByCategory)
            {
                List <TechnologyInfo> techs = null;
                var key = Enum.GetName(typeof(TechnologyTier), tech.Tier);
                if (key != null && !map.TryGetValue(key, out techs))
                {
                    map[key] = techs = new List <TechnologyInfo>();
                }

                techs?.Add(tech);
            }

            foreach (var tier in map.Keys)
            {
                var list = map[tier];
                list.Sort((x, y) => y.StacksCount - x.StacksCount);
                if (list.Count > 5)
                {
                    list.RemoveRange(5, list.Count - 5);
                }
            }

            var allOrgs = Db.Select <OrganizationInfo>(Db.From <Organization>()
                                                       .Where(g => g.Deleted == null));

            var allOrgsMap = allOrgs.ToDictionary(x => x.Id);

            var orgLabels = Db.Select <OrganizationLabel>();

            foreach (var orgLabel in orgLabels)
            {
                if (allOrgsMap.TryGetValue(orgLabel.OrganizationId, out var org))
                {
                    (org.Labels ?? (org.Labels = new List <LabelInfo>()))
                    .Add(orgLabel.ConvertTo <LabelInfo>());
                }
            }

            var orgCategories = Db.Select <Category>(x => x.Deleted == null);

            foreach (var category in orgCategories)
            {
                if (allOrgsMap.TryGetValue(category.OrganizationId, out var org))
                {
                    (org.Categories ?? (org.Categories = new List <CategoryInfo>()))
                    .Add(category.ConvertTo <CategoryInfo>());
                }
            }

            foreach (var entry in allOrgsMap)
            {
                var org = entry.Value;
                org.MembersCount = PostServicesBase.GetOrganizationMembersCount(org.Id);
                org.Categories?.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
            }

            var response = new OverviewResponse
            {
                Created = DateTime.UtcNow,

                LatestTechStacks = Db.GetTechstackDetails(Db.From <TechnologyStack>().OrderByDescending(x => x.LastModified).Limit(20)),

                TopUsers = Db.Select <UserInfo>(
                    @"select u.user_name as UserName, u.default_profile_url as AvatarUrl, COUNT(*) as StacksCount
                        from technology_stack ts
                             left join
	                           user_favorite_technology_stack uf on (ts.id = uf.technology_stack_id)
	                           left join
	                           custom_user_auth u on (u.id = ts.owner_id::integer)
                       group by u.user_name, u.default_profile_url
                      having count(*) > 0
                       order by StacksCount desc
                       limit 20"),

                TopTechnologies = topTechByCategory
                                  .OrderByDescending(x => x.StacksCount)
                                  .Take(50)
                                  .ToList(),

                PopularTechStacks = Db.Select(
                    Db.From <TechnologyStack>()
                    .Join <PageStats>((s, p) => s.Id == p.RefId && p.RefType == "stack")
                    .OrderByDescending <PageStats>(p => p.ViewCount)
                    .Limit(12)),

                AllOrganizations = allOrgs,

                TopTechnologiesByTier = map,
            };

            //Lighten payload
            response.LatestTechStacks.Each(x => {
                x.Details = x.DetailsHtml = null;
                x.TechnologyChoices.Each(y => {
                    y.Description = null;
                });
            });

            //Put TechStacks entry first to provide a first good experience
            var techStacksApp = response.LatestTechStacks.FirstOrDefault(x => x.Id == TechStacksAppId);

            if (techStacksApp != null)
            {
                response.LatestTechStacks.RemoveAll(x => x.Id == TechStacksAppId);
                response.LatestTechStacks.Insert(0, techStacksApp);
            }

            return(response);
        }