From 0aa43ef4c6724f672453676a0c688cd80097dad7 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 9 Apr 2022 18:53:52 +0800 Subject: ... --- .../IntegratedTests2/TimelinePostTest1.cs | 5 +++-- .../IntegratedTests2/TimelinePostTest2.cs | 1 + .../Timeline/Controllers/TimelinePostV2Controller.cs | 11 ++++++----- .../Filters/CatchEntityDeletedExceptionFilter.cs | 18 ++++++++++++++++++ BackEnd/Timeline/Models/Page.cs | 5 +++++ .../Timeline/Services/Timeline/TimelinePostService.cs | 4 ++-- BackEnd/Timeline/Startup.cs | 1 + 7 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest1.cs b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest1.cs index 4ecb805c..d06da9d9 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest1.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest1.cs @@ -72,8 +72,9 @@ namespace Timeline.Tests.IntegratedTests2 public async Task ListTest() { using var client = CreateClientAsUser(); - var posts = await client.TestJsonSendAsync>(HttpMethod.Get, "v2/timelines/user/hello/posts"); - posts.Should().HaveCount(3); + var posts = await client.TestJsonSendAsync>(HttpMethod.Get, "v2/timelines/user/hello/posts"); + posts.TotalCount.Should().Be(3); + posts.Items.Should().HaveCount(3); } [Fact] diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest2.cs b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest2.cs index 9aaae21a..993e9353 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest2.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest2.cs @@ -124,6 +124,7 @@ namespace Timeline.Tests.IntegratedTests2 { using var client = CreateClientAsUser(); await client.TestSendAsync(HttpMethod.Delete, "v2/timelines/user/hello/posts/1", expectedStatusCode: HttpStatusCode.NoContent); + await client.TestSendAsync(HttpMethod.Get, "v2/timelines/user/hello/posts/1", expectedStatusCode: HttpStatusCode.Gone); } [Fact] diff --git a/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs b/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs index af1b3182..3a694e31 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs @@ -44,22 +44,23 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] - public async Task>> ListAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted, [FromQuery][Range(0, int.MaxValue)] int? page, [FromQuery][Range(1, int.MaxValue)] int? numberPerPage) + public async Task>> ListAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery][Range(0, int.MaxValue)] int? page, [FromQuery][Range(1, int.MaxValue)] int? numberPerPage) { var timelineId = await _timelineService.GetTimelineIdAsync(owner, timeline); if (!UserHasPermission(UserPermission.AllTimelineManagement) && !await _timelineService.HasReadPermissionAsync(timelineId, GetOptionalAuthUserId())) { return Forbid(); } - var posts = await _postService.GetPostsAsync(timelineId, modifiedSince, includeDeleted ?? false, page, numberPerPage); - var result = await _mapper.MapListAsync(posts, Url, User); - return result; + var postPage = await _postService.GetPostsV2Async(timelineId, modifiedSince, page, numberPerPage); + var items = await _mapper.MapListAsync(postPage.Items, Url, User); + return postPage.WithItems(items); } [HttpGet("{post}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status410Gone)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] public async Task> GetAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline, [FromRoute(Name = "post")] long postId) { @@ -68,7 +69,7 @@ namespace Timeline.Controllers { return Forbid(); } - var post = await _postService.GetPostAsync(timelineId, postId); + var post = await _postService.GetPostV2Async(timelineId, postId); var result = await _mapper.MapAsync(post, Url, User); return result; } diff --git a/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs b/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs new file mode 100644 index 00000000..41f4894d --- /dev/null +++ b/BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using Timeline.Services; + +namespace Timeline.Filters +{ + public class CatchEntityDeletedExceptionFilter : IExceptionFilter + { + public void OnException(ExceptionContext context) + { + if (context.Exception is EntityDeletedException e) + { + context.Result = new StatusCodeResult(StatusCodes.Status410Gone); + } + } + } +} diff --git a/BackEnd/Timeline/Models/Page.cs b/BackEnd/Timeline/Models/Page.cs index 807702c1..c0d8d472 100644 --- a/BackEnd/Timeline/Models/Page.cs +++ b/BackEnd/Timeline/Models/Page.cs @@ -23,6 +23,11 @@ namespace Timeline.Models public long TotalPageCount { get; set; } public long TotalCount { get; set; } public List Items { get; set; } = new List(); + + public Page WithItems(List items) + { + return new Page(PageNumber, PageSize, TotalCount, items); + } } } diff --git a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs index 40f226ce..644e989f 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs @@ -55,9 +55,9 @@ namespace Timeline.Services.Timeline }); } - private static EntityNotExistException CreatePostDeletedException(long timelineId, long postId) + private static EntityDeletedException CreatePostDeletedException(long timelineId, long postId) { - return new EntityNotExistException(EntityTypes.TimelinePost, new Dictionary + return new EntityDeletedException(EntityTypes.TimelinePost, new Dictionary { ["timeline-id"] = timelineId, ["post-id"] = postId, diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 295cad20..665705a7 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -72,6 +72,7 @@ namespace Timeline setup.Filters.Add(new ProducesAttribute(MimeTypes.ApplicationJson, MimeTypes.TextJson)); setup.Filters.Add(); setup.Filters.Add(); + setup.Filters.Add(); setup.Filters.Add(); setup.UseApiRoutePrefix("api"); }) -- cgit v1.2.3