From c667c722ec081dfb651d3583236d7aa429c56941 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 13 May 2021 15:53:08 +0800 Subject: feat: Posts pagination. --- .../Timeline/Services/Timeline/ITimelinePostService.cs | 4 +++- .../Timeline/Services/Timeline/Resource.Designer.cs | 18 ++++++++++++++++++ BackEnd/Timeline/Services/Timeline/Resource.resx | 6 ++++++ .../Timeline/Services/Timeline/TimelinePostService.cs | 14 +++++++++++++- 4 files changed, 40 insertions(+), 2 deletions(-) (limited to 'BackEnd/Timeline/Services') diff --git a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs index 92984938..c0edf857 100644 --- a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs @@ -16,9 +16,11 @@ namespace Timeline.Services.Timeline /// The id of the timeline. /// The time that posts have been modified since. /// Whether include deleted posts. + /// The page number. Starts from 0. If null, do not do pagination. + /// Count of entities per page. If null, 20. /// A list of all posts. /// Thrown when timeline does not exist. - Task> GetPostsAsync(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false); + Task> GetPostsAsync(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false, int? page = null, int? numberPerPage = null); /// /// Get a post of a timeline. diff --git a/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs b/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs index 5ad03011..f5112cd9 100644 --- a/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs +++ b/BackEnd/Timeline/Services/Timeline/Resource.Designer.cs @@ -105,6 +105,24 @@ namespace Timeline.Services.Timeline { } } + /// + /// Looks up a localized string similar to Number per page can't be zero or negative.. + /// + internal static string ExceptionNumberPerPageZeroOrNegative { + get { + return ResourceManager.GetString("ExceptionNumberPerPageZeroOrNegative", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Page number can't be negative.. + /// + internal static string ExceptionPageNegative { + get { + return ResourceManager.GetString("ExceptionPageNegative", resourceCulture); + } + } + /// /// Looks up a localized string similar to Image validation failed.. /// diff --git a/BackEnd/Timeline/Services/Timeline/Resource.resx b/BackEnd/Timeline/Services/Timeline/Resource.resx index cc293d05..1d61a1bb 100644 --- a/BackEnd/Timeline/Services/Timeline/Resource.resx +++ b/BackEnd/Timeline/Services/Timeline/Resource.resx @@ -132,6 +132,12 @@ This timeline name is neither a valid personal timeline name nor a valid ordinary timeline name. {0} + + Number per page can't be zero or negative. + + + Page number can't be negative. + Image validation failed. diff --git a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs index a0961a8d..ee1002e0 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs @@ -64,8 +64,14 @@ namespace Timeline.Services.Timeline }); } - public async Task> GetPostsAsync(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false) + public async Task> GetPostsAsync(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false, int? page = null, int? numberPerPage = 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); + + await _basicTimelineService.ThrowIfTimelineNotExist(timelineId); modifiedSince = modifiedSince?.MyToUtc(); @@ -84,6 +90,12 @@ namespace Timeline.Services.Timeline query = query.OrderBy(p => p.Time); + if (page.HasValue) + { + var npp = numberPerPage.GetValueOrDefault(20); + query = query.Skip(npp * (page.Value - 1)).Take(npp); + } + return await query.ToListAsync(); } -- cgit v1.2.3