예제 #1
0
        public async Task GetItemsCursorPaging(PagingCursorArgs args)
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            var query = _demoDbContext.Items;

            var time = args.PageCursor != null ? new DateTimeOffset(args.PageCursor.Value, TimeSpan.Zero) : new DateTimeOffset(DateTime.MinValue, TimeSpan.Zero);

            _itemsCursorPagingLoading.OnNext(true);

            // get page size + 1 item (for next cursor data)
            var items = await query
                        .OrderBy(x => x.CreateDate)
                        .Where(x => x.CreateDate >= time)
                        .Take(args.PageSize + 1).ToListAsync();

            // get previous cursor
            var firstItem = items.FirstOrDefault()?.CreateDate;
            var previous  = await query
                            .OrderByDescending(x => x.CreateDate)
                            .Where(x => x.CreateDate < firstItem)
                            .Take(args.PageSize).LastOrDefaultAsync();

            _itemsCursorPaging.OnNext(new PagingCursorData
            {
                Items        = items.Take(args.PageSize).ToList(),
                Total        = await query.CountAsync(),
                NextPage     = items.LastOrDefault()?.CreateDate.Ticks,
                PreviousPage = previous?.CreateDate.Ticks
            });

            watch.Stop();

            _execTime.OnNext($"Execution Time: {watch.ElapsedMilliseconds} ms");
            _itemsCursorPagingLoading.OnNext(false);
        }
예제 #2
0
        public async Task Delete(PagingCursorArgs args)
        {
            if (args.PageCursor == null)
            {
                return;
            }

            var query = _demoDbContext.Items;
            var time  = args.PageCursor != null ? new DateTimeOffset(args.PageCursor.Value, TimeSpan.Zero) : new DateTimeOffset(DateTime.MinValue, TimeSpan.Zero);

            _itemsCursorPagingLoading.OnNext(true);

            var previous = await query
                           .OrderByDescending(x => x.CreateDate)
                           .Where(x => x.CreateDate < time)
                           .Take(args.PageSize).ToListAsync();

            if (previous.Count == 0)
            {
                return;
            }

            int index      = new Random().Next(previous.Count);
            var itemDelete = previous[index];

            _demoDbContext.Items.Remove(itemDelete);
            await _demoDbContext.SaveChangesAsync();

            _itemsCursorPagingLoading.OnNext(false);

            return;
        }