aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Services/TimelineService.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-06-18 19:41:51 +0800
committercrupest <crupest@outlook.com>2020-06-18 19:41:51 +0800
commitd9ed0c1b0fb04d161d27b556e33f0a03738e717d (patch)
tree36029dc650c6fd7e166ca1db36cb761ba77dd402 /Timeline/Services/TimelineService.cs
parent135f47e7477bb2a72c423145dcd286ae494fd3ed (diff)
downloadtimeline-d9ed0c1b0fb04d161d27b556e33f0a03738e717d.tar.gz
timeline-d9ed0c1b0fb04d161d27b556e33f0a03738e717d.tar.bz2
timeline-d9ed0c1b0fb04d161d27b556e33f0a03738e717d.zip
feat(back): Timeline service add post modified since.
Diffstat (limited to 'Timeline/Services/TimelineService.cs')
-rw-r--r--Timeline/Services/TimelineService.cs77
1 files changed, 56 insertions, 21 deletions
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
@@ -106,6 +106,20 @@ namespace Timeline.Services
Task<List<TimelinePost>> GetPosts(string timelineName);
/// <summary>
+ /// Get the posts that have been modified since a given time in the timeline.
+ /// </summary>
+ /// <param name="timelineName">The name of the timeline.</param>
+ /// <param name="modifiedSince">The time that posts have been modified since.</param>
+ /// <returns>A list of all posts.</returns>
+ /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
+ /// <exception cref="ArgumentException">Throw when <paramref name="timelineName"/> is of bad format.</exception>
+ /// <exception cref="TimelineNotExistException">
+ /// Thrown when timeline with name <paramref name="timelineName"/> does not exist.
+ /// If it is a personal timeline, then inner exception is <see cref="UserNotExistException"/>.
+ /// </exception>
+ Task<List<TimelinePost>> GetPosts(string timelineName, DateTime modifiedSince);
+
+ /// <summary>
/// Get the etag of data of a post.
/// </summary>
/// <param name="timelineName">The name of the timeline of the post.</param>
@@ -383,6 +397,34 @@ namespace Timeline.Services
};
}
+ private async Task<TimelinePost> 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<TimelinePost>();
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<List<TimelinePost>> 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<TimelinePost>();
+ foreach (var entity in postEntities)
+ {
+ posts.Add(await MapTimelinePostFromEntity(entity, timelineName));
}
return posts;
}