From 253b06dfaa091d986a8714c081fd1e01679f538a Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 10 Feb 2021 02:03:06 +0800 Subject: ... --- BackEnd/Timeline/Services/TimelinePostService.cs | 126 ++++++++--------------- 1 file changed, 45 insertions(+), 81 deletions(-) (limited to 'BackEnd/Timeline/Services/TimelinePostService.cs') diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index 66ec8090..98841478 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -14,95 +14,69 @@ using static Timeline.Resources.Services.TimelineService; namespace Timeline.Services { - public class PostData : ICacheableData + public class TimelinePostDataDigest { -#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; } // TODO: Why nullable? - } + public TimelinePostDataDigest(string kind, string eTag, DateTime lastModified) + { + Kind = kind; + ETag = eTag; + LastModified = lastModified; + } - public abstract class TimelinePostCreateRequestContent - { - public abstract string TypeName { get; } + public string Kind { get; set; } + public string ETag { get; set; } + public DateTime LastModified { get; set; } } - public class TimelinePostCreateRequestTextContent : TimelinePostCreateRequestContent + public class TimelinePostData { - private string _text; - - public TimelinePostCreateRequestTextContent(string text) + public TimelinePostData(string kind, byte[] data, string eTag, DateTime lastModified) { - if (text is null) - throw new ArgumentNullException(nameof(text)); - - _text = text; + Kind = kind; + Data = data; + ETag = eTag; + LastModified = lastModified; } - public override string TypeName => TimelinePostContentTypes.Text; + public string Kind { get; set; } - public string Text - { - get => _text; - set - { - if (value is null) - throw new ArgumentNullException(nameof(value)); - _text = value; - } - } +#pragma warning disable CA1819 // Properties should not return arrays + public byte[] Data { get; set; } +#pragma warning restore CA1819 // Properties should not return arrays + + public string ETag { get; set; } + public DateTime LastModified { get; set; } } - public class TimelinePostCreateRequestImageContent : TimelinePostCreateRequestContent + public class TimelinePostCreateRequestData { - private byte[] _data; - - public TimelinePostCreateRequestImageContent(byte[] data) + public TimelinePostCreateRequestData(string kind, byte[] data) { - if (data is null) - throw new ArgumentNullException(nameof(data)); - - _data = data; + Kind = kind; + Data = data; } - public override string TypeName => TimelinePostContentTypes.Image; - + public string Kind { get; set; } #pragma warning disable CA1819 // Properties should not return arrays - public byte[] Data - { - get => _data; - set - { - if (value is null) - throw new ArgumentNullException(nameof(value)); - _data = value; - } - } + public byte[] Data { get; set; } #pragma warning restore CA1819 // Properties should not return arrays } public class TimelinePostCreateRequest { - public TimelinePostCreateRequest(TimelinePostCreateRequestContent content) - { - Content = content; - } - public string? Color { get; set; } /// If not set, current time is used. public DateTime? Time { get; set; } - public TimelinePostCreateRequestContent Content { get; set; } + public List Content { get; set; } = new List(); } public class TimelinePostPatchRequest { public string? Color { get; set; } public DateTime? Time { get; set; } - public TimelinePostCreateRequestContent? Content { get; set; } + public List? Content { get; set; } } public interface ITimelinePostService @@ -128,16 +102,7 @@ namespace Timeline.Services /// Thrown when post of does not exist or has been deleted. Task GetPost(long timelineId, long postId, bool includeDelete = false); - /// - /// Get the etag of data of a post. - /// - /// The id of the timeline of the post. - /// The id of the post. - /// The etag of the data. - /// Thrown when timeline does not exist. - /// Thrown when post of does not exist or has been deleted. - /// Thrown when post has no data. - Task GetPostDataETag(long timelineId, long postId); + Task GetPostDataDigest(long timelineId, long postId, long dataIndex); /// /// Get the data of a post. @@ -148,8 +113,7 @@ namespace Timeline.Services /// Thrown when timeline does not exist. /// Thrown when post of does not exist or has been deleted. /// Thrown when post has no data. - /// - Task GetPostData(long timelineId, long postId); + Task GetPostData(long timelineId, long postId, long dataIndex); /// /// Create a new post in timeline. @@ -305,7 +269,7 @@ namespace Timeline.Services if (postEntity.Content == null) throw new TimelinePostNotExistException(timelineId, postId, true); - if (postEntity.ContentType != TimelinePostContentTypes.Image) + if (postEntity.ContentType != TimelinePostDataKind.Image) throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost); var tag = postEntity.Content; @@ -313,7 +277,7 @@ namespace Timeline.Services return tag; } - public async Task GetPostData(long timelineId, long postId) + public async Task GetPostData(long timelineId, long postId) { await CheckTimelineExistence(timelineId); @@ -325,7 +289,7 @@ namespace Timeline.Services if (postEntity.Content == null) throw new TimelinePostNotExistException(timelineId, postId, true); - if (postEntity.ContentType != TimelinePostContentTypes.Image) + if (postEntity.ContentType != TimelinePostDataKind.Image) throw new TimelinePostNoDataException(ExceptionGetDataNonImagePost); var tag = postEntity.Content; @@ -349,7 +313,7 @@ namespace Timeline.Services await _database.SaveChangesAsync(); } - return new PostData + return new TimelinePostData { Data = data, Type = postEntity.ExtraContent, @@ -358,21 +322,21 @@ namespace Timeline.Services }; } - private async Task SaveContent(TimelinePostEntity entity, TimelinePostCreateRequestContent content) + private async Task SaveContent(TimelinePostEntity entity, TimelinePostCreateRequestData content) { switch (content) { - case TimelinePostCreateRequestTextContent c: - entity.ContentType = c.TypeName; - entity.Content = c.Text; + case TimelinePostCreateRequestTextData c: + entity.ContentType = c.Kind; + entity.Content = c.Data; break; - case TimelinePostCreateRequestImageContent c: + case TimelinePostCreateRequestImageData c: var imageFormat = await _imageValidator.Validate(c.Data); var imageFormatText = imageFormat.DefaultMimeType; var tag = await _dataManager.RetainEntry(c.Data); - entity.ContentType = content.TypeName; + entity.ContentType = content.Kind; entity.Content = tag; entity.ExtraContent = imageFormatText; break; @@ -383,7 +347,7 @@ namespace Timeline.Services private async Task CleanContent(TimelinePostEntity entity) { - if (entity.Content is not null && entity.ContentType == TimelinePostContentTypes.Image) + if (entity.Content is not null && entity.ContentType == TimelinePostDataKind.Image) await _dataManager.FreeEntry(entity.Content); entity.Content = null; } @@ -516,7 +480,7 @@ namespace Timeline.Services { if (post.Content != null) { - if (post.ContentType == TimelinePostContentTypes.Image) + if (post.ContentType == TimelinePostDataKind.Image) { dataTags.Add(post.Content); } -- cgit v1.2.3