From 5bf2cebd80a33e3fd6d5ed59e10a76f4d2d5be91 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 6 Feb 2021 17:43:03 +0800 Subject: ... --- .../Timeline/Controllers/TimelinePostController.cs | 77 +++++++++++----------- 1 file changed, 37 insertions(+), 40 deletions(-) (limited to 'BackEnd/Timeline/Controllers/TimelinePostController.cs') diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 0148f56e..4dab2a44 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -162,54 +162,51 @@ namespace Timeline.Controllers return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); } - var content = body.Content; + var requestContent = body.Content; - TimelinePostEntity post; + TimelinePostCreateRequestContent createContent; - TimelinePostCommonProperties properties = new TimelinePostCommonProperties { Color = body.Color, Time = body.Time }; - - if (content.Type == TimelinePostContentTypes.Text) + switch (requestContent.Type) { - var text = content.Text; - if (text == null) - { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_TextContentTextRequired)); - } - post = await _postService.CreateTextPost(timelineId, userId, text, properties); + case TimelinePostContentTypes.Text: + if (requestContent.Text is null) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_TextContentTextRequired)); + } + createContent = new TimelinePostCreateRequestTextContent(requestContent.Text); + break; + case TimelinePostContentTypes.Image: + if (requestContent.Data is null) + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataRequired)); + + // decode base64 + byte[] data; + try + { + data = Convert.FromBase64String(requestContent.Data); + } + catch (FormatException) + { + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotBase64)); + } + + createContent = new TimelinePostCreateRequestImageContent(data); + break; + default: + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType)); + } - 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 _postService.CreateImagePost(timelineId, userId, data, properties); - } - catch (ImageException) - { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotImage)); - } + try + { + var post = await _postService.CreatePost(timelineId, userId, new TimelinePostCreateRequest(createContent) { Time = body.Time, Color = body.Color }); + var result = await _timelineMapper.MapToHttp(post, timeline, Url); + return result; } - else + catch (ImageException) { - return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ContentUnknownType)); + return BadRequest(ErrorResponse.Common.CustomMessage_InvalidModel(Resources.Messages.TimelineController_ImageContentDataNotImage)); } - - var result = await _timelineMapper.MapToHttp(post, timeline, Url); - return result; } /// -- cgit v1.2.3