예제 #1
0
        public async Task <IHttpActionResult> GetListOfRelatedGames(string id, PointRelationship relationship,
                                                                    bool stats = false, NormalPointIdentityType idType = NormalPointIdentityType.Id, int skip = 0, int take = 9)
        {
            if (take > 50)
            {
                take = 50;
            }

            var point = await _dbContext.NormalPoints
                        .SingleOrDefaultAsync(p => idType == NormalPointIdentityType.IdCode?p.IdCode == id : p.Id == id);

            if (point == null || (point.Type != NormalPointType.Manufacturer && point.Type != NormalPointType.Genre))
            {
                return(NotFound());
            }

            var queryBase = _dbContext.NormalPoints.Where(p => p.Id == point.Id);
            IQueryable <Models.NormalPoint> queryNext;

            switch (relationship)
            {
            case PointRelationship.Developer:
                queryNext = queryBase.SelectMany(p => p.DeveloperForPoints);
                break;

            case PointRelationship.Publisher:
                queryNext = queryBase.SelectMany(p => p.PublisherForPoints);
                break;

            case PointRelationship.Series:
                queryNext = queryBase.SelectMany(p => p.SeriesForPoints);
                break;

            case PointRelationship.Genre:
                queryNext = queryBase.SelectMany(p => p.GenreForPoints);
                break;

            case PointRelationship.Tag:
                queryNext = queryBase.SelectMany(p => p.TagForPoints);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(relationship), relationship, null);
            }
            var points = await queryNext.OrderByDescending(p => p.ReleaseDate)
                         .Skip(() => skip).Take(() => take).ToListAsync();

            var userId = User.Identity.GetUserId();

            return(Ok(points.Select(p =>
            {
                var dto = new NormalPointDto(p)
                {
                    ReleaseDate = p.ReleaseDate
                };
                if (stats)
                {
                    dto.SubscriberCount = _dbContext.NormalPoints.Where(np => np.Id == p.Id)
                                          .Select(np => np.Subscribers.Count)
                                          .Single();
                    dto.ArticleCount = _dbContext.NormalPoints.Where(np => np.Id == p.Id)
                                       .Select(np => np.Articles.Count)
                                       .Single();
                    dto.Subscribed = _dbContext.NormalPoints.Where(np => np.Id == p.Id)
                                     .SelectMany(np => np.Subscribers)
                                     .Select(u => u.Id)
                                     .Contains(userId);
                }
                return dto;
            })));
        }
예제 #2
0
        public async Task <IHttpActionResult> GetOneById(string id, bool stats = false, bool votes                     = false,
                                                         bool subscribed       = false, bool related                   = false, bool coverDescription = false,
                                                         bool more             = false, NormalPointIdentityType idType = NormalPointIdentityType.Id)
        {
            var point = await _dbContext.NormalPoints
                        .SingleOrDefaultAsync(p => idType == NormalPointIdentityType.IdCode?p.IdCode == id : p.Id == id);

            if (point == null)
            {
                return(NotFound());
            }

            var pointDto = new NormalPointDto(point);

            if (stats)
            {
                var statsResult = await _dbContext.NormalPoints
                                  .Where(p => p.Id == point.Id)
                                  .Select(p => new { articleCount = p.Articles.Count, subscriberCount = p.Subscribers.Count })
                                  .SingleAsync();

                pointDto.ArticleCount    = statsResult.articleCount;
                pointDto.SubscriberCount = statsResult.subscriberCount;
            }

            if (subscribed)
            {
                var userId = User.Identity.GetUserId();
                pointDto.Subscribed = await _dbContext.Users.Where(u => u.Id == userId)
                                      .SelectMany(u => u.SubscribedPoints)
                                      .Select(p => p.Id)
                                      .ContainsAsync(point.Id);
            }

            if (votes)
            {
                var votesResult = await _dbContext.NormalPoints
                                  .Where(p => p.Id == point.Id)
                                  .Select(
                    p => new
                {
                    level1 = p.VoteByArticles.Count(a => a.Vote == 1),
                    level2 = p.VoteByArticles.Count(a => a.Vote == 2),
                    level3 = p.VoteByArticles.Count(a => a.Vote == 3),
                    level4 = p.VoteByArticles.Count(a => a.Vote == 4),
                    level5 = p.VoteByArticles.Count(a => a.Vote == 5)
                })
                                  .SingleAsync();

                pointDto.VoteStats = new Dictionary <int, int>
                {
                    [1] = votesResult.level1,
                    [2] = votesResult.level2,
                    [3] = votesResult.level3,
                    [4] = votesResult.level4,
                    [5] = votesResult.level5
                };
            }

            if (related)
            {
                switch (point.Type)
                {
                case NormalPointType.Game:
                case NormalPointType.Hardware:
                    pointDto.SteamAppId      = point.SteamAppId;
                    pointDto.DisplayAliases  = point.DisplayAliases;
                    pointDto.ReleaseDate     = point.ReleaseDate;
                    pointDto.DeveloperPoints =
                        point.DeveloperPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.PublisherPoints =
                        point.PublisherPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.SeriesPoints        = point.SeriesPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.GenrePoints         = point.GenrePoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.TagPoints           = point.TagPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.MajorPlatformPoints =
                        point.MajorPlatformPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    pointDto.MinorPlatformPoints =
                        point.MinorPlatformPoints.Select(p => new NormalPointDto(p, true)).ToList();
                    break;

                case NormalPointType.Manufacturer:
                    pointDto.GameCountAsDeveloper = point.DeveloperForPoints.Count;
                    pointDto.GameCountAsPublisher = point.PublisherForPoints.Count;
                    break;

                case NormalPointType.Genre:
                    pointDto.GameCountAsGenre  = point.GenreForPoints.Count;
                    pointDto.GameCountAsSeries = point.SeriesForPoints.Count;
                    pointDto.GameCountAsTag    = point.TagForPoints.Count;
                    break;
                }
            }

            if (coverDescription)
            {
                if (point.Type == NormalPointType.Game || point.Type == NormalPointType.Hardware)
                {
                    pointDto.CoverImage = point.CoverImage;
                }
                pointDto.Description = point.Description;
            }

            if (more)
            {
                pointDto.ChineseAliases   = point.ChineseAliases;
                pointDto.EnglishAliases   = point.EnglishAliases;
                pointDto.NameInSteamStore = string.Join("; ", point.SteamStoreNames.Select(n => n.Name));
            }

            return(Ok(pointDto));
        }