From 66658abde1220a53d0e022aaac8dd49a15034a34 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 4 Feb 2021 22:03:08 +0800 Subject: ... --- .../IntegratedTests/TimelinePostTest.cs | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs index f05ed7af..b4ddcb43 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/TimelinePostTest.cs @@ -491,6 +491,8 @@ namespace Timeline.Tests.IntegratedTests Color = "#1" }); + long id; + { var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest { @@ -498,7 +500,40 @@ namespace Timeline.Tests.IntegratedTests Color = "#aabbcc" }); post.Color.Should().Be("#aabbcc"); + id = post.Id; } + + { + var post = await client.TestGetAsync($"timelines/{generator(1)}/posts/{id}"); + post.Color.Should().Be("#aabbcc"); + } + } + + [Theory] + [MemberData(nameof(TimelineNameGeneratorTestData))] + public async Task GetPost(TimelineNameGenerator generator) + { + using var client = await CreateClientAsUser(); + + HttpTimelinePostCreateRequestContent CreateRequestContent() => new() + { + Type = "text", + Text = "aaa" + }; + + await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/1"); + + var post = await client.TestPostAsync($"timelines/{generator(1)}/posts", new HttpTimelinePostCreateRequest + { + Content = CreateRequestContent(), + }); + + var post2 = await client.TestGetAsync($"timelines/{generator(1)}/posts/{post.Id}"); + post2.Should().BeEquivalentTo(post); + + await client.TestDeleteAsync($"timelines/{generator(1)}/posts/{post.Id}"); + + await client.TestGetAssertNotFoundAsync($"timelines/{generator(1)}/posts/{post.Id}"); } } } -- 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.Tests/IntegratedTests/TimelinePostTest.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