From d9ed0c1b0fb04d161d27b556e33f0a03738e717d Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 18 Jun 2020 19:41:51 +0800 Subject: feat(back): Timeline service add post modified since. --- Timeline/Services/TimelineService.cs | 77 ++++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 21 deletions(-) (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 6c1e91c6..e0b1b51d 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -105,6 +105,20 @@ namespace Timeline.Services /// Task> GetPosts(string timelineName); + /// + /// Get the posts that have been modified since a given time in the timeline. + /// + /// The name of the timeline. + /// The time that posts have been modified since. + /// A list of all posts. + /// Thrown when is null. + /// Throw when is of bad format. + /// + /// Thrown when timeline with name does not exist. + /// If it is a personal timeline, then inner exception is . + /// + Task> GetPosts(string timelineName, DateTime modifiedSince); + /// /// Get the etag of data of a post. /// @@ -383,6 +397,34 @@ namespace Timeline.Services }; } + private async Task MapTimelinePostFromEntity(TimelinePostEntity entity, string timelineName) + { + if (entity.Content == null) + { + throw new ArgumentException(ExceptionPostDeleted, nameof(entity)); + } + + var author = await _userService.GetUserById(entity.AuthorId); + + var type = entity.ContentType; + + ITimelinePostContent content = type switch + { + TimelinePostContentTypes.Text => new TextTimelinePostContent(entity.Content), + TimelinePostContentTypes.Image => new ImageTimelinePostContent(entity.Content), + _ => throw new DatabaseCorruptedException(string.Format(CultureInfo.InvariantCulture, ExceptionDatabaseUnknownContentType, type)) + }; + + return new TimelinePost( + id: entity.LocalId, + author: author, + content: content, + time: entity.Time, + lastUpdated: entity.LastUpdated, + timelineName: timelineName + ); + } + private TimelineEntity CreateNewTimelineEntity(string? name, long ownerId) { var currentTime = _clock.GetCurrentTime(); @@ -488,30 +530,23 @@ namespace Timeline.Services var posts = new List(); foreach (var entity in postEntities) { - if (entity.Content == null) - { - throw new Exception(); - } - - var author = await _userService.GetUserById(entity.AuthorId); + posts.Add(await MapTimelinePostFromEntity(entity, timelineName)); + } + return posts; + } - var type = entity.ContentType; + public async Task> GetPosts(string timelineName, DateTime modifiedSince) + { + if (timelineName == null) + throw new ArgumentNullException(nameof(timelineName)); - ITimelinePostContent content = type switch - { - TimelinePostContentTypes.Text => new TextTimelinePostContent(entity.Content), - TimelinePostContentTypes.Image => new ImageTimelinePostContent(entity.Content), - _ => throw new DatabaseCorruptedException(string.Format(CultureInfo.InvariantCulture, ExceptionDatabaseUnknownContentType, type)) - }; + var timelineId = await FindTimelineId(timelineName); + var postEntities = await _database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId && p.Content != null && p.LastUpdated > modifiedSince).ToListAsync(); - posts.Add(new TimelinePost( - id: entity.LocalId, - author: author, - content: content, - time: entity.Time, - lastUpdated: entity.LastUpdated, - timelineName: timelineName - )); + var posts = new List(); + foreach (var entity in postEntities) + { + posts.Add(await MapTimelinePostFromEntity(entity, timelineName)); } return posts; } -- cgit v1.2.3