Esempio n. 1
0
        private async Task <IEnumerable <ResourceDto> > HydrateHistoryItems(IEnumerable <LibraryHistoryEntity> historyItems, int userId)
        {
            var resourcesXml = new XElement("Resources",
                                            historyItems
                                            .Select(i => i.ResourceId)
                                            .Distinct()
                                            .Select(i => new XElement("R", new XAttribute("Id", i)))
                                            )
                               .ToString(SaveOptions.DisableFormatting);

            var sql       = @"
select Id, Format, NaturalKey, Segment, Title, CategoryIds, Tags, Source, 
    HasExplanation, HasExample, HasExercise, HasText, HasPicture, HasAudio, HasVideo,
    IsPersonal, Comment, Viewed
from dbo.libGetResources(@UserId, @Resources);
";
            var resources = await DapperHelper.QueryResilientlyAsync <ResourceDto>(sql, new { UserId = userId, Resources = resourcesXml });

            // Correlate the resources we got from the database to the items we got from the history table.
            var dict = resources.ToDictionary(i => i.Id);

            return(historyItems
                   .Select(i =>
            {
                ResourceDto resource;
                if (dict.TryGetValue(i.ResourceId, out resource))
                {
                    resource.LocalTime = KeyUtils.InvertedKeyToLocalTime(i.RowKey);
                }
                else
                {
                    resource = new ResourceDto();
                }
                return resource;
            })
                   .Where(i => i.Id != 0)
                   );
        }
Esempio n. 2
0
        // GET: /history
        public async Task <ActionResult> History()
        {
            // Send all the days there are records for. We will enable/disable days in the calendar on the page accordingly. RowKeys in the table are "inverted" local time.
            var days = new List <string>();

            if (this.IsAuthenticated())
            {
                var userId          = this.GetUserId();
                var partitionKey    = KeyUtils.IntToKey(userId);
                var filterPartition = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey);
                var query           = new TableQuery <TableEntity>().Where(filterPartition);
                var table           = AzureStorageUtils.GetCloudTable(AzureStorageUtils.TableNames.LibraryHistory);

                TableQuerySegment <TableEntity> currentSegment = null;
                while (currentSegment == null || currentSegment.ContinuationToken != null)
                {
                    currentSegment = await table.ExecuteQuerySegmentedAsync <TableEntity>(
                        query,
                        currentSegment != null?currentSegment.ContinuationToken : null
                        );

                    // Format 2014-01-21 as "140121"
                    var localDays = currentSegment.Results
                                    .GroupBy(i => i.RowKey.Substring(0, 6))
                                    .Select(i => KeyUtils.InvertedKeyToLocalTime(i.Key, 3, "", "d2").Substring(2))
                    ;

                    days.AddRange(localDays);
                }
            }

            //var daysParam = days.Distinct();
            //ViewBag.DaysParamJson = JsonUtils.SerializeAsJson(daysParam);
            ViewBag.DaysParam = days.Distinct();

            return(View());
        }