diff options
7 files changed, 36 insertions, 9 deletions
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<List<HttpTimelinePost>>(HttpMethod.Get, "v2/timelines/user/hello/posts"); - posts.Should().HaveCount(3); + var posts = await client.TestJsonSendAsync<Page<HttpTimelinePost>>(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<ActionResult<List<HttpTimelinePost>>> 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<ActionResult<Page<HttpTimelinePost>>> 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<HttpTimelinePost>(posts, Url, User); - return result; + var postPage = await _postService.GetPostsV2Async(timelineId, modifiedSince, page, numberPerPage); + var items = await _mapper.MapListAsync<HttpTimelinePost>(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<ActionResult<HttpTimelinePost>> 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<HttpTimelinePost>(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<T> Items { get; set; } = new List<T>(); + + public Page<U> WithItems<U>(List<U> items) + { + return new Page<U>(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<string, object>
+ return new EntityDeletedException(EntityTypes.TimelinePost, new Dictionary<string, object>
{
["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<CatchEntityNotExistExceptionFilter>();
setup.Filters.Add<CatchEntityAlreadyExistExceptionFilter>();
+ setup.Filters.Add<CatchEntityDeletedExceptionFilter>();
setup.Filters.Add<CatchImageExceptionFilter>();
setup.UseApiRoutePrefix("api");
})
|