From 88232f85e69a5e4b390e73344b31372746d4adca Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 10 Mar 2020 16:19:28 +0800 Subject: ... --- Timeline/Controllers/TimelineController.cs | 48 ++++++++++++++++++++++++++++-- Timeline/Models/Http/TimelineController.cs | 1 + Timeline/Resources/Messages.Designer.cs | 45 ++++++++++++++++++++++++++++ Timeline/Resources/Messages.resx | 15 ++++++++++ 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/Timeline/Controllers/TimelineController.cs b/Timeline/Controllers/TimelineController.cs index 0e5483fa..38fe7475 100644 --- a/Timeline/Controllers/TimelineController.cs +++ b/Timeline/Controllers/TimelineController.cs @@ -123,8 +123,52 @@ namespace Timeline.Controllers return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - var res = await _service.CreatePost(name, id, body.Content, body.Time); - return res; + var content = body.Content; + + TimelinePost post; + + if (content.Type == TimelinePostContentTypes.Text) + { + var text = content.Text; + if (text == null) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_TextContentTextRequired)); + } + post = await _service.CreateTextPost(name, id, text, body.Time); + } + else if (content.Type == TimelinePostContentTypes.Image) + { + var base64Data = content.Data; + if (base64Data == null) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataRequired)); + } + byte[] data; + try + { + data = Convert.FromBase64String(base64Data); + } + catch (FormatException) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotBase64)); + } + + try + { + post = await _service.CreateImagePost(name, id, data, body.Time); + } + catch (ImageException) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotImage)); + } + } + else + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType)); + } + + var result = _mapper.Map(post); + return result; } [HttpDelete("timelines/{name}/posts/{id}")] diff --git a/Timeline/Models/Http/TimelineController.cs b/Timeline/Models/Http/TimelineController.cs index ce5f3b98..3e2e6b58 100644 --- a/Timeline/Models/Http/TimelineController.cs +++ b/Timeline/Models/Http/TimelineController.cs @@ -14,6 +14,7 @@ namespace Timeline.Models.Http public class TimelinePostCreateRequest { + [Required] public TimelinePostCreateRequestContent Content { get; set; } = default!; public DateTime? Time { get; set; } diff --git a/Timeline/Resources/Messages.Designer.cs b/Timeline/Resources/Messages.Designer.cs index 4123cb8b..fb093f07 100644 --- a/Timeline/Resources/Messages.Designer.cs +++ b/Timeline/Resources/Messages.Designer.cs @@ -177,6 +177,42 @@ namespace Timeline.Resources { } } + /// + /// Looks up a localized string similar to Unknown type of post content.. + /// + internal static string TimelineController_ContentUnknownType { + get { + return ResourceManager.GetString("TimelineController_ContentUnknownType", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Data field is not a valid base64 string in image content.. + /// + internal static string TimelineController_ImageContentDataNotBase64 { + get { + return ResourceManager.GetString("TimelineController_ImageContentDataNotBase64", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Data field is not a valid image after base64 decoding in image content.. + /// + internal static string TimelineController_ImageContentDataNotImage { + get { + return ResourceManager.GetString("TimelineController_ImageContentDataNotImage", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Data field is required for image content.. + /// + internal static string TimelineController_ImageContentDataRequired { + get { + return ResourceManager.GetString("TimelineController_ImageContentDataRequired", resourceCulture); + } + } + /// /// Looks up a localized string similar to The user specified by query param "relate" does not exist.. /// @@ -195,6 +231,15 @@ namespace Timeline.Resources { } } + /// + /// Looks up a localized string similar to Text field is required for text content.. + /// + internal static string TimelineController_TextContentTextRequired { + get { + return ResourceManager.GetString("TimelineController_TextContentTextRequired", resourceCulture); + } + } + /// /// Looks up a localized string similar to Username or password is invalid.. /// diff --git a/Timeline/Resources/Messages.resx b/Timeline/Resources/Messages.resx index 865db524..6a4cb1b6 100644 --- a/Timeline/Resources/Messages.resx +++ b/Timeline/Resources/Messages.resx @@ -156,12 +156,27 @@ The timeline with given name does not exist. + + Unknown type of post content. + + + Data field is not a valid base64 string in image content. + + + Data field is not a valid image after base64 decoding in image content. + + + Data field is required for image content. + The user specified by query param "relate" does not exist. '{0}' is an unkown visibility in the query parameter 'visibility'. + + Text field is required for text content. + Username or password is invalid. -- cgit v1.2.3