diff options
Diffstat (limited to 'BackEnd/Timeline/Services/TimelinePostService.cs')
-rw-r--r-- | BackEnd/Timeline/Services/TimelinePostService.cs | 126 |
1 files changed, 45 insertions, 81 deletions
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; }
/// <summary>If not set, current time is used.</summary>
public DateTime? Time { get; set; }
- public TimelinePostCreateRequestContent Content { get; set; }
+ public List<TimelinePostCreateRequestData> Content { get; set; } = new List<TimelinePostCreateRequestData>();
}
public class TimelinePostPatchRequest
{
public string? Color { get; set; }
public DateTime? Time { get; set; }
- public TimelinePostCreateRequestContent? Content { get; set; }
+ public List<TimelinePostCreateRequestData?>? Content { get; set; }
}
public interface ITimelinePostService
@@ -128,16 +102,7 @@ namespace Timeline.Services /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
Task<TimelinePostEntity> GetPost(long timelineId, long postId, bool includeDelete = false);
- /// <summary>
- /// Get the etag of data of a post.
- /// </summary>
- /// <param name="timelineId">The id of the timeline of the post.</param>
- /// <param name="postId">The id of the post.</param>
- /// <returns>The etag of the data.</returns>
- /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
- /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
- /// <exception cref="TimelinePostNoDataException">Thrown when post has no data.</exception>
- Task<string> GetPostDataETag(long timelineId, long postId);
+ Task<TimelinePostDataDigest> GetPostDataDigest(long timelineId, long postId, long dataIndex);
/// <summary>
/// Get the data of a post.
@@ -148,8 +113,7 @@ namespace Timeline.Services /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
/// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
/// <exception cref="TimelinePostNoDataException">Thrown when post has no data.</exception>
- /// <seealso cref="GetPostDataETag(long, long)"/>
- Task<PostData> GetPostData(long timelineId, long postId);
+ Task<TimelinePostData> GetPostData(long timelineId, long postId, long dataIndex);
/// <summary>
/// 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<PostData> GetPostData(long timelineId, long postId)
+ public async Task<TimelinePostData> 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);
}
|