Exemple #1
0
        public async Task <ActionResult <UserWeeksDto> > GetWeeks(int ownerId)
        {
            var weeks = await _repo.QueryWeeksOf(ownerId)
                        .OrderBy(week => week.WeekNumber)
                        .Select(
                week => new UserWeekDto()
            {
                Id         = week.Id,
                OwnerId    = week.OwnerId,
                WeekNumber = week.WeekNumber,
                From       = week.From,
                To         = week.To
            })
                        .ToListAsync();

            return(Ok(new UserWeeksDto {
                Weeks = weeks
            }));
        }
Exemple #2
0
        public async Task <Result <ChartWeek> > GetLiveWeekFor(
            User user,
            DateTime currentTime,
            CancellationToken?cancellationToken = null)
        {
            var timezone = TZConvert.GetTimeZoneInfo(user.IanaTimezone ?? "");

            if (timezone == null)
            {
                throw new InvalidOperationException();
            }

            var lastUserWeek = await _repo.QueryWeeksOf(user.Id)
                               .OrderByDescending(week => week.WeekNumber)
                               .FirstOrDefaultAsync();

            var fromDate = lastUserWeek?.To.AddSeconds(1) ?? user.RegisteredAt;
            var liveWeek = _chartDateService
                           .GetChartWeeksInDateRange(lastUserWeek?.WeekNumber + 1 ?? 1,
                                                     fromDate, currentTime, timezone, true)
                           .LastOrDefault();

            var fromUnix = ToUnixTimeSeconds(liveWeek.From);
            var toUnix   = ToUnixTimeSeconds(liveWeek.To);

            var artistChart =
                _lastFm.GetWeeklyArtistChart(user.UserName, fromUnix, toUnix, cancellationToken);
            var albumChart =
                _lastFm.GetWeeklyAlbumChart(user.UserName, fromUnix, toUnix, cancellationToken);
            var trackChart =
                _lastFm.GetWeeklyTrackChart(user.UserName, fromUnix, toUnix, cancellationToken);

            // Allows for parallel running since Tasks are started at call, not at await
            await artistChart;
            await albumChart;
            await trackChart;

            var mergedResult =
                Results.Merge(artistChart.Result, albumChart.Result, trackChart.Result);

            if (mergedResult.IsFailed)
            {
                return(mergedResult);
            }

            liveWeek.Owner        = user;
            liveWeek.ChartEntries = CreateEntriesForLastFmCharts(trackChart.Result.Value,
                                                                 albumChart.Result.Value,
                                                                 artistChart.Result.Value, liveWeek);

            var existingWeeks = await _repo.QueryWeeksWithRelationsOf(user.Id)
                                .ToListAsync();

            cancellationToken?.ThrowIfCancellationRequested();

            var entryId = 1;

            Parallel.ForEach(liveWeek.ChartEntries, entry =>
            {
                var(stat, statText) = GetStatsForChartEntry(entry, existingWeeks);
                entry.Id            = entryId++;
                entry.Stat          = stat;
                entry.StatText      = statText;
            });

            return(Results.Ok(liveWeek));
        }