private async System.Threading.Tasks.Task GetPageAsyncTestAsync() { var ops = new DbContextOptionsBuilder().UseInMemoryDatabase("test").Options; var dateTime = new Mock <IDateTime>(); dateTime.Setup(s => s.Now).Returns(DateTime.Now); var session = new Mock <IAppSession>(); session.Setup(s => s.CancellationToken).Returns(default(CancellationToken)); var ctx = new AppDbContext(ops, dateTime.Object); ctx.Demo.AddRange(new[] { new Demo("a"), new Demo("b"), new Demo("c"), new Demo("d"), }); ctx.SaveChanges(); var rsy = new DemoRepository(ctx, session.Object); var dto = new GetDemoPageDto { Index = 1, Size = 2, }; var rsp = await rsy.GetPageAsync(dto); Assert.Equal(2, rsp.List.Count()); Assert.Equal("a", rsp.List.First().Name); Assert.Equal("b", rsp.List.Last().Name); Assert.Equal(4, rsp.Total); }
public async Task <PageDto <DemoDto> > GetPageAsync(GetDemoPageDto dto) { var data = _appDbContext.Demo.AsNoTracking(); if (dto.StartDate.HasValue) { var startDate = dto.StartDate?.Date; data = data.Where(w => w.CreateDate > startDate); } if (dto.StartDate.HasValue) { var endDate = dto.EndDate?.NextDayDate(); data = data.Where(w => w.CreateDate > endDate); } var count = await data.CountAsync(_appSession.CancellationToken); var list = await data.GetPage(dto.Page()).Select(s => new DemoDto { CreateDate = s.CreateDate, Id = s.Id, ModifiedDate = s.ModifiedDate, Name = s.Name, Items = s.DemoItems.Select(ss => new DemoDto.Item { Name = ss.Name, Sort = ss.Sort }) }).ToArrayAsync(_appSession.CancellationToken); return(new PageDto <DemoDto>(count, list)); }
public async Task <PageDto <DemoDto> > GetPageAsync([FromQuery] GetDemoPageDto dto) { return(await _demoRepository.GetPageAsync(dto)); }