From 636cf3839d92e884987e4e3aec7f23953d02fe37 Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 13 Mar 2020 17:58:32 +0800 Subject: Add cache for timeline post data. --- Timeline/Services/TimelineService.cs | 49 ++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'Timeline/Services/TimelineService.cs') diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 301a1d97..b26016e5 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; +using Timeline.Helpers; using Timeline.Models; using Timeline.Models.Validation; using static Timeline.Resources.Services.TimelineService; @@ -32,14 +33,14 @@ namespace Timeline.Services public long UserId { get; set; } } - public class PostData + public class PostData : ICacheableData { #pragma warning disable CA1819 // Properties should not return arrays public byte[] Data { get; set; } = default!; #pragma warning restore CA1819 // Properties should not return arrays public string Type { get; set; } = default!; public string ETag { get; set; } = default!; - public DateTime LastModified { get; set; } = default!; + public DateTime? LastModified { get; set; } } /// @@ -91,6 +92,20 @@ namespace Timeline.Services /// See remarks of . Task> GetPosts(string name); + /// + /// Get the etag of data of a post. + /// + /// See remarks of . + /// The id of the post. + /// The etag of the data. + /// Thrown when is null. + /// See remarks of . + /// See remarks of . + /// Thrown when post of does not exist or has been deleted. + /// Thrown when post has no data. See remarks. + /// + Task GetPostDataETag(string name, long postId); + /// /// Get the data of a post. /// @@ -105,6 +120,7 @@ namespace Timeline.Services /// /// Use this method to retrieve the image of image post. /// + /// Task GetPostData(string name, long postId); /// @@ -402,6 +418,29 @@ namespace Timeline.Services } return posts; } + + public async Task GetPostDataETag(string name, long postId) + { + if (name == null) + throw new ArgumentNullException(nameof(name)); + + var timelineId = await FindTimelineId(name); + var postEntity = await Database.TimelinePosts.Where(p => p.LocalId == postId).SingleOrDefaultAsync(); + + if (postEntity == null) + throw new TimelinePostNotExistException(name, postId); + + if (postEntity.Content == null) + throw new TimelinePostNotExistException(name, postId, true); + + if (postEntity.ContentType != TimelinePostContentTypes.Image) + throw new InvalidOperationException(ExceptionGetDataNonImagePost); + + var tag = postEntity.Content; + + return tag; + } + public async Task GetPostData(string name, long postId) { if (name == null) @@ -1014,6 +1053,12 @@ namespace Timeline.Services return s.GetPosts(realName); } + public Task GetPostDataETag(string name, long postId) + { + var s = BranchName(name, out var realName); + return s.GetPostDataETag(realName, postId); + } + public Task GetPostData(string name, long postId) { var s = BranchName(name, out var realName); -- cgit v1.2.3