From d42d826ae863b20913321d229a168b05d723db73 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 6 Feb 2021 16:41:19 +0800 Subject: ... --- .../Models/Http/HttpTimelinePostPatchRequest.cs | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs (limited to 'BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs') diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs new file mode 100644 index 00000000..3dface29 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs @@ -0,0 +1,39 @@ +using System; +using Timeline.Models.Validation; + +namespace Timeline.Models.Http +{ + /// + /// The model of changing post content. + /// + public class HttpTimelinePostPatchRequestContent + { + /// + /// The new type of the post. If null, old type is used. This field can't be used alone. Use it with corresponding fields to change post content. + /// + [TimelinePostContentType] + public string? Type { get; set; } + /// + /// The new text. Null for not change. + /// + public string? Text { get; set; } + /// + /// The new data. Null for not change. + /// + public string? Data { get; set; } + } + + public class HttpTimelinePostPatchRequest + { + /// + /// Change the time. Null for not change. + /// + public DateTime? Time { get; set; } + + /// + /// Change the color. Null for not change. + /// + [Color] + public string? Color { get; set; } + } +} -- cgit v1.2.3 From 45b683d582d4a7760ffd69c4cd47841ce0545119 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 8 Feb 2021 21:24:42 +0800 Subject: ... --- .../IntegratedTests/TimelinePostTest.cs | 30 +++++++++++++++++++ .../Timeline/Controllers/TimelinePostController.cs | 35 +++++++++++++++++++++- .../Models/Http/HttpTimelinePostPatchRequest.cs | 20 ------------- 3 files changed, 64 insertions(+), 21 deletions(-) (limited to 'BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index b4ddcb43..17c85f22 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -535,5 +535,35 @@ namespace Timeline.Tests.IntegratedTests await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/{post.Id}"); } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task PatchPost(TimelineNameGenerator generator) + { + using var client = await CreateClientAsUser(); + + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest + { + Content = new() + { + Type = "text", + Text = "aaa" + } + }); + + var date = new DateTime(2000, 10, 1); + + var post2 = await client.TestPatchAsync($"timelines/{generator(1)}/posts/{post.Id}", new HttpTimelinePostPatchRequest + { + Time = date, + Color = "#aabbcc" + }); + post2.Time.Should().Be(date); + post2.Color.Should().Be("#aabbcc"); + + var post3 = await client.TestGetAsync($"timelines/{generator(1)}/posts/{post.Id}"); + post3.Time.Should().Be(date); + post3.Color.Should().Be("#aabbcc"); + } } } diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 4dab2a44..44498c58 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -4,7 +4,6 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; -using Timeline.Entities; using Timeline.Filters; using Timeline.Helpers; using Timeline.Models; @@ -209,6 +208,40 @@ namespace Timeline.Controllers } } + /// + /// Update a post except content. + /// + /// Timeline name. + /// Post id. + /// Request body. + /// New info of post. + [HttpPatch("{post}")] + [Authorize] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + public async Task> Patch([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromBody] HttpTimelinePostPatchRequest body) + { + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + + try + { + if (!UserHasAllTimelineManagementPermission && !await _postService.HasPostModifyPermission(timelineId, post, this.GetUserId(), true)) + { + return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid()); + } + + var entity = await _postService.PatchPost(timelineId, post, new TimelinePostPatchRequest { Time = body.Time, Color = body.Color }); + var result = await _timelineMapper.MapToHttp(entity, timeline, Url); + return Ok(result); + } + catch (TimelinePostNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.PostNotExist()); + } + } + /// /// Delete a post. /// diff --git a/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs index 3dface29..2c6edf66 100644 --- a/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs +++ b/BackEnd/Timeline/Models/Http/HttpTimelinePostPatchRequest.cs @@ -3,26 +3,6 @@ using Timeline.Models.Validation; namespace Timeline.Models.Http { - /// - /// The model of changing post content. - /// - public class HttpTimelinePostPatchRequestContent - { - /// - /// The new type of the post. If null, old type is used. This field can't be used alone. Use it with corresponding fields to change post content. - /// - [TimelinePostContentType] - public string? Type { get; set; } - /// - /// The new text. Null for not change. - /// - public string? Text { get; set; } - /// - /// The new data. Null for not change. - /// - public string? Data { get; set; } - } - public class HttpTimelinePostPatchRequest { /// -- cgit v1.2.3