From f15f883198226a146113f82b3d7ca4864f3aa58b Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 30 Apr 2022 23:34:29 +0800 Subject: ... --- .../Controllers/V2/TimelinePostV2Controller.cs | 2 +- .../Services/Timeline/ITimelinePostService.cs | 2 +- .../Services/Timeline/TimelinePostService.cs | 23 +++++++--------------- 3 files changed, 9 insertions(+), 18 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs index 8b7101e4..a6d12ae4 100644 --- a/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs +++ b/BackEnd/Timeline/Controllers/V2/TimelinePostV2Controller.cs @@ -47,7 +47,7 @@ namespace Timeline.Controllers.V2 { return Forbid(); } - var postPage = await _postService.GetPostsV2Async(timelineId, modifiedSince, page, pageSize); + var postPage = await _postService.GetPostsV2Async(timelineId, page ?? 1, pageSize ?? 20, modifiedSince); var items = await MapListAsync(postPage.Items); return postPage.WithItems(items); } diff --git a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs index af36933c..105ceb54 100644 --- a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs @@ -65,7 +65,7 @@ namespace Timeline.Services.Timeline /// Number per page. /// A task containing a page of post entity. /// Thrown when timeline does not exist. - Task> GetPostsV2Async(long timelineId, DateTime? modifiedSince = null, int? page = null, int? numberPerPage = null); + Task> GetPostsV2Async(long timelineId, int page, int numberPerPage, DateTime? modifiedSince = null); /// /// Get a post of a timeline. diff --git a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs index e0212cb7..8fee0467 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs @@ -382,13 +382,12 @@ namespace Timeline.Services.Timeline return timelineEntity.OwnerId == modifierId || postEntity.AuthorId == modifierId; } - public async Task> GetPostsV2Async(long timelineId, DateTime? modifiedSince = null, int? page = null, int? numberPerPage = null) + public async Task> GetPostsV2Async(long timelineId, int page, int pageSize, DateTime? modifiedSince = null) { - if (page.HasValue && page < 0) - throw new ArgumentOutOfRangeException(nameof(page), Resource.ExceptionPageNegative); - if (numberPerPage.HasValue && numberPerPage <= 0) - throw new ArgumentOutOfRangeException(nameof(numberPerPage), Resource.ExceptionNumberPerPageZeroOrNegative); - + if (page <= 0) + throw new ArgumentOutOfRangeException(nameof(page)); + if (pageSize <= 0) + throw new ArgumentOutOfRangeException(nameof(pageSize)); var timeline = await _timelineService.GetTimelineAsync(timelineId); @@ -401,19 +400,11 @@ namespace Timeline.Services.Timeline query = query.Where(p => p.LastUpdated >= modifiedSince || (p.Author != null && p.Author.UsernameChangeTime >= modifiedSince)); } - query = query.OrderBy(p => p.Time); + query = query.OrderBy(p => p.Time).Skip(pageSize * (page - 1)).Take(pageSize); - var pageNumber = page.GetValueOrDefault(1); - var pageSize = numberPerPage.GetValueOrDefault(20); - - if (pageNumber > 1) - { - query = query.Skip(pageSize * (pageNumber - 1)).Take(pageSize); - } - var items = await query.ToListAsync(); - return new Page(pageNumber, pageSize, timeline.CurrentPostLocalId, items); + return new Page(page, pageSize, timeline.CurrentPostLocalId, items); } public async Task GetPostV2Async(long timelineId, long postId) -- cgit v1.2.3