aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Models
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-02-12 22:39:57 +0800
committerGitHub <noreply@github.com>2021-02-12 22:39:57 +0800
commitc3d0a5f88de0fbdf6bc584548832017087ab1248 (patch)
treec1c992987263897fb1c091c5129c6d1f1e64073d /BackEnd/Timeline/Models
parente232e31de839dc0c0de691c5856f29dcb92cf0fc (diff)
parent5849d34d9fcf1ccfb7fe5cc0842765129f7198b4 (diff)
downloadtimeline-c3d0a5f88de0fbdf6bc584548832017087ab1248.tar.gz
timeline-c3d0a5f88de0fbdf6bc584548832017087ab1248.tar.bz2
timeline-c3d0a5f88de0fbdf6bc584548832017087ab1248.zip
Merge pull request #267 from crupest/backend
春节大换血 Spring festival big change.
Diffstat (limited to 'BackEnd/Timeline/Models')
-rw-r--r--BackEnd/Timeline/Models/ByteData.cs8
-rw-r--r--BackEnd/Timeline/Models/Http/ErrorResponse.cs11
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePost.cs12
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs35
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs9
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs26
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs19
-rw-r--r--BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs23
-rw-r--r--BackEnd/Timeline/Models/Mapper/TimelineMapper.cs33
-rw-r--r--BackEnd/Timeline/Models/MimeTypes.cs14
-rw-r--r--BackEnd/Timeline/Models/TimelinePostContentTypes.cs14
-rw-r--r--BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs18
12 files changed, 84 insertions, 138 deletions
diff --git a/BackEnd/Timeline/Models/ByteData.cs b/BackEnd/Timeline/Models/ByteData.cs
index 7b832eb5..a1a0c238 100644
--- a/BackEnd/Timeline/Models/ByteData.cs
+++ b/BackEnd/Timeline/Models/ByteData.cs
@@ -1,4 +1,5 @@
-using NSwag.Annotations;
+using System;
+using NSwag.Annotations;
namespace Timeline.Models
{
@@ -14,6 +15,11 @@ namespace Timeline.Models
/// <param name="contentType">The content type.</param>
public ByteData(byte[] data, string contentType)
{
+ if (data is null)
+ throw new ArgumentNullException(nameof(data));
+ if (contentType is null)
+ throw new ArgumentNullException(nameof(contentType));
+
Data = data;
ContentType = contentType;
}
diff --git a/BackEnd/Timeline/Models/Http/ErrorResponse.cs b/BackEnd/Timeline/Models/Http/ErrorResponse.cs
index 1bc46680..3812471d 100644
--- a/BackEnd/Timeline/Models/Http/ErrorResponse.cs
+++ b/BackEnd/Timeline/Models/Http/ErrorResponse.cs
@@ -253,17 +253,6 @@ namespace Timeline.Models.Http
{
return new CommonResponse(ErrorCodes.TimelineController.PostNotExist, string.Format(message, formatArgs));
}
-
- public static CommonResponse PostNoData(params object?[] formatArgs)
- {
- return new CommonResponse(ErrorCodes.TimelineController.PostNoData, string.Format(TimelineController_PostNoData, formatArgs));
- }
-
- public static CommonResponse CustomMessage_PostNoData(string message, params object?[] formatArgs)
- {
- return new CommonResponse(ErrorCodes.TimelineController.PostNoData, string.Format(message, formatArgs));
- }
-
}
}
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs
index 5981d7a4..26e1a92d 100644
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePost.cs
@@ -1,7 +1,9 @@
using System;
+using System.Collections.Generic;
namespace Timeline.Models.Http
{
+
/// <summary>
/// Info of a post.
/// </summary>
@@ -9,10 +11,10 @@ namespace Timeline.Models.Http
{
public HttpTimelinePost() { }
- public HttpTimelinePost(long id, HttpTimelinePostContent? content, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated)
+ public HttpTimelinePost(long id, List<HttpTimelinePostDataDigest> dataList, bool deleted, DateTime time, HttpUser? author, string? color, DateTime lastUpdated)
{
Id = id;
- Content = content;
+ DataList = dataList;
Deleted = deleted;
Time = time;
Author = author;
@@ -25,9 +27,11 @@ namespace Timeline.Models.Http
/// </summary>
public long Id { get; set; }
/// <summary>
- /// Content of the post. May be null if post is deleted.
+ /// The data list.
/// </summary>
- public HttpTimelinePostContent? Content { get; set; }
+#pragma warning disable CA2227
+ public List<HttpTimelinePostDataDigest> DataList { get; set; } = default!;
+#pragma warning restore CA2227
/// <summary>
/// True if post is deleted.
/// </summary>
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs
deleted file mode 100644
index 55ff1ac2..00000000
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePostContent.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-namespace Timeline.Models.Http
-{
- /// <summary>
- /// Info of post content.
- /// </summary>
- public class HttpTimelinePostContent
- {
- public HttpTimelinePostContent() { }
-
- public HttpTimelinePostContent(string type, string? text, string? url, string? eTag)
- {
- Type = type;
- Text = text;
- Url = url;
- ETag = eTag;
- }
-
- /// <summary>
- /// Type of the post content.
- /// </summary>
- public string Type { get; set; } = default!;
- /// <summary>
- /// If post is of text type. This is the text.
- /// </summary>
- public string? Text { get; set; }
- /// <summary>
- /// If post is of image type. This is the image url.
- /// </summary>
- public string? Url { get; set; }
- /// <summary>
- /// If post has data (currently it means it's a image post), this is the data etag.
- /// </summary>
- public string? ETag { get; set; }
- }
-}
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs
index b25adf36..2a973c72 100644
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequest.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Timeline.Models.Validation;
@@ -7,10 +8,14 @@ namespace Timeline.Models.Http
public class HttpTimelinePostCreateRequest
{
/// <summary>
- /// Content of the new post.
+ /// Data list of the new content.
/// </summary>
[Required]
- public HttpTimelinePostCreateRequestContent Content { get; set; } = default!;
+ [MinLength(1)]
+ [MaxLength(100)]
+#pragma warning disable CA2227
+ public List<HttpTimelinePostCreateRequestData> DataList { get; set; } = default!;
+#pragma warning restore CA2227
/// <summary>
/// Time of the post. If not set, current time will be used.
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs
deleted file mode 100644
index 12ab407f..00000000
--- a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestContent.cs
+++ /dev/null
@@ -1,26 +0,0 @@
-using System.ComponentModel.DataAnnotations;
-using Timeline.Models.Validation;
-
-namespace Timeline.Models.Http
-{
- /// <summary>
- /// Content of post create request.
- /// </summary>
- public class HttpTimelinePostCreateRequestContent
- {
- /// <summary>
- /// Type of post content.
- /// </summary>
- [Required]
- [TimelinePostContentType]
- public string Type { get; set; } = default!;
- /// <summary>
- /// If post is of text type, this is the text.
- /// </summary>
- public string? Text { get; set; }
- /// <summary>
- /// If post is of image type, this is base64 of image data.
- /// </summary>
- public string? Data { get; set; }
- }
-}
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs
new file mode 100644
index 00000000..94ee5aa7
--- /dev/null
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostCreateRequestData.cs
@@ -0,0 +1,19 @@
+using System.ComponentModel.DataAnnotations;
+
+namespace Timeline.Models.Http
+{
+ public class HttpTimelinePostCreateRequestData
+ {
+ /// <summary>
+ /// Mime type of the data.
+ /// </summary>
+ [Required]
+ public string ContentType { get; set; } = default!;
+
+ /// <summary>
+ /// Base64 of data.
+ /// </summary>
+ [Required]
+ public string Data { get; set; } = default!;
+ }
+}
diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs
new file mode 100644
index 00000000..61d35e15
--- /dev/null
+++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostDataDigest.cs
@@ -0,0 +1,23 @@
+using System;
+
+namespace Timeline.Models.Http
+{
+ public class HttpTimelinePostDataDigest
+ {
+ public HttpTimelinePostDataDigest()
+ {
+
+ }
+
+ public HttpTimelinePostDataDigest(string kind, string eTag, DateTime lastUpdated)
+ {
+ Kind = kind;
+ ETag = eTag;
+ LastUpdated = lastUpdated;
+ }
+
+ public string Kind { get; set; } = default!;
+ public string ETag { get; set; } = default!;
+ public DateTime LastUpdated { get; set; }
+ }
+}
diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs
index 88c96d8a..5c46fa81 100644
--- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs
+++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs
@@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
-using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Timeline.Controllers;
@@ -67,34 +66,14 @@ namespace Timeline.Models.Mapper
public async Task<HttpTimelinePost> MapToHttp(TimelinePostEntity entity, string timelineName, IUrlHelper urlHelper)
{
- HttpTimelinePostContent? content = null;
-
- if (entity.Content != null)
- {
- content = entity.ContentType switch
- {
- TimelinePostContentTypes.Text => new HttpTimelinePostContent
- (
- type: TimelinePostContentTypes.Text,
- text: entity.Content,
- url: null,
- eTag: null
- ),
- TimelinePostContentTypes.Image => new HttpTimelinePostContent
- (
- type: TimelinePostContentTypes.Image,
- text: null,
- url: urlHelper.ActionLink(nameof(TimelinePostController.DataGet), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName, post = entity.LocalId }),
- eTag: $"\"{entity.Content}\""
- ),
- _ => throw new DatabaseCorruptedException(string.Format(CultureInfo.InvariantCulture, "Unknown timeline post type {0}.", entity.ContentType))
- };
- }
+ _ = timelineName;
+ await _database.Entry(entity).Collection(p => p.DataList).LoadAsync();
await _database.Entry(entity).Reference(e => e.Author).LoadAsync();
- HttpUser? author = null;
+ List<HttpTimelinePostDataDigest> dataDigestList = entity.DataList.OrderBy(d => d.Index).Select(d => new HttpTimelinePostDataDigest(d.Kind, $"\"{d.DataTag}\"", d.LastUpdated)).ToList();
+ HttpUser? author = null;
if (entity.Author is not null)
{
author = await _userMapper.MapToHttp(entity.Author, urlHelper);
@@ -102,11 +81,11 @@ namespace Timeline.Models.Mapper
return new HttpTimelinePost(
id: entity.LocalId,
- content: content,
- deleted: content is null,
+ dataList: dataDigestList,
time: entity.Time,
author: author,
color: entity.Color,
+ deleted: entity.Deleted,
lastUpdated: entity.LastUpdated
);
}
diff --git a/BackEnd/Timeline/Models/MimeTypes.cs b/BackEnd/Timeline/Models/MimeTypes.cs
new file mode 100644
index 00000000..37d3a893
--- /dev/null
+++ b/BackEnd/Timeline/Models/MimeTypes.cs
@@ -0,0 +1,14 @@
+namespace Timeline.Models
+{
+ public static class MimeTypes
+ {
+ public const string ImagePng = "image/png";
+ public const string ImageJpeg = "image/jpeg";
+ public const string ImageGif = "image/gif";
+ public const string ImageWebp = "image/webp";
+ public const string TextPlain = "text/plain";
+ public const string TextMarkdown = "text/markdown";
+ public const string TextJson = "text/json";
+ public const string ApplicationJson = "application/json";
+ }
+}
diff --git a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs b/BackEnd/Timeline/Models/TimelinePostContentTypes.cs
deleted file mode 100644
index ca5e79e1..00000000
--- a/BackEnd/Timeline/Models/TimelinePostContentTypes.cs
+++ /dev/null
@@ -1,14 +0,0 @@
-using System.Collections.Generic;
-
-namespace Timeline.Models
-{
- public static class TimelinePostContentTypes
- {
-#pragma warning disable CA1819 // Properties should not return arrays
- public static string[] AllTypes { get; } = new string[] { Text, Image };
-#pragma warning restore CA1819 // Properties should not return arrays
-
- public const string Text = "text";
- public const string Image = "image";
- }
-}
diff --git a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs b/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs
deleted file mode 100644
index 483cce06..00000000
--- a/BackEnd/Timeline/Models/Validation/TimelinePostContentTypeValidator.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-using System;
-
-namespace Timeline.Models.Validation
-{
- public class TimelinePostContentTypeValidator : StringSetValidator
- {
- public TimelinePostContentTypeValidator() : base(TimelinePostContentTypes.AllTypes) { }
- }
-
- [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
- public class TimelinePostContentTypeAttribute : ValidateWithAttribute
- {
- public TimelinePostContentTypeAttribute() : base(typeof(TimelinePostContentTypeValidator))
- {
-
- }
- }
-}