aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-04-09 18:53:52 +0800
committercrupest <crupest@outlook.com>2022-04-09 18:53:52 +0800
commit0aa43ef4c6724f672453676a0c688cd80097dad7 (patch)
tree5bbb0d736b385b9d0c6254f7e006161e0080de8f /BackEnd/Timeline
parent4db131899145b7aca0ea5fd36984cf1542c9619b (diff)
downloadtimeline-0aa43ef4c6724f672453676a0c688cd80097dad7.tar.gz
timeline-0aa43ef4c6724f672453676a0c688cd80097dad7.tar.bz2
timeline-0aa43ef4c6724f672453676a0c688cd80097dad7.zip
...
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r--BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs11
-rw-r--r--BackEnd/Timeline/Filters/CatchEntityDeletedExceptionFilter.cs18
-rw-r--r--BackEnd/Timeline/Models/Page.cs5
-rw-r--r--BackEnd/Timeline/Services/Timeline/TimelinePostService.cs4
-rw-r--r--BackEnd/Timeline/Startup.cs1
5 files changed, 32 insertions, 7 deletions
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");
})