From 4ea535d93753826ec900879560d876cec4d58c38 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 ++++++++--------------- BackEnd/Timeline/Services/UserAvatarService.cs | 37 ++----- 2 files changed, 54 insertions(+), 109 deletions(-) (limited to 'BackEnd/Timeline/Services') 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); } diff --git a/BackEnd/Timeline/Services/UserAvatarService.cs b/BackEnd/Timeline/Services/UserAvatarService.cs index b41c45fd..afd6cf0a 100644 --- a/BackEnd/Timeline/Services/UserAvatarService.cs +++ b/BackEnd/Timeline/Services/UserAvatarService.cs @@ -8,28 +8,12 @@ using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Helpers; +using Timeline.Helpers.Cache; +using Timeline.Models; using Timeline.Services.Exceptions; namespace Timeline.Services { - public class Avatar - { - public string Type { get; set; } = default!; - [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = "DTO Object")] - public byte[] Data { get; set; } = default!; - } - - public class AvatarInfo - { - public Avatar Avatar { get; set; } = default!; - public DateTime LastModified { get; set; } - - public CacheableData ToCacheableData() - { - return new CacheableData(Avatar.Type, Avatar.Data, LastModified); - } - } - /// /// Provider for default user avatar. /// @@ -42,29 +26,24 @@ namespace Timeline.Services /// Get the etag of default avatar. /// /// - Task GetDefaultAvatarETag(); + Task GetDefaultAvatarETag(); /// /// Get the default avatar. /// - Task GetDefaultAvatar(); + Task GetDefaultAvatar(); } public interface IUserAvatarService { - /// - /// Get the etag of a user's avatar. Warning: This method does not check the user existence. - /// - /// The id of the user to get avatar etag of. - /// The etag. - Task GetAvatarETag(long id); + Task GetAvatarDigest(long id); /// /// Get avatar of a user. If the user has no avatar set, a default one is returned. Warning: This method does not check the user existence. /// /// The id of the user to get avatar of. /// The avatar info. - Task GetAvatar(long id); + Task GetAvatar(long id); /// /// Set avatar for a user. Warning: This method does not check the user existence. @@ -74,7 +53,9 @@ namespace Timeline.Services /// The etag of the avatar. /// Thrown if any field in is null when is not null. /// Thrown if avatar is of bad format. - Task SetAvatar(long id, Avatar? avatar); + Task SetAvatar(long id, ByteData avatar); + + Task DeleteAvatar(long id); } // TODO! : Make this configurable. -- cgit v1.2.3