From 86160e4a5697615452a7e06dc78f8be4c8b2515f Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 9 Apr 2022 19:30:39 +0800 Subject: ... --- .../IntegratedTests2/TimelinePostTest3.cs | 9 +++++ .../Controllers/TimelinePostV2Controller.cs | 6 ++-- .../Services/Timeline/ITimelinePostService.cs | 26 ++++++++++++++ .../Services/Timeline/TimelinePostService.cs | 42 ++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) (limited to 'BackEnd') diff --git a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest3.cs b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest3.cs index 5313a7c8..f760bd03 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest3.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests2/TimelinePostTest3.cs @@ -69,6 +69,15 @@ namespace Timeline.Tests.IntegratedTests2 await client.TestSendAsync(HttpMethod.Get, "v2/timelines/user/hello/posts/1/data/1"); } + [Fact] + public async Task PostDataGetDeleted() + { + 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/data", expectedStatusCode: HttpStatusCode.Gone); + await client.TestSendAsync(HttpMethod.Get, "v2/timelines/user/hello/posts/1/data/1", expectedStatusCode: HttpStatusCode.Gone); + } + [Fact] public async Task PostDataGetNotExist() { diff --git a/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs b/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs index 3a694e31..6e4b265f 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostV2Controller.cs @@ -80,6 +80,7 @@ namespace Timeline.Controllers [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status410Gone)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] public async Task> DataIndexGetAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline, [FromRoute] long post) { @@ -92,6 +93,7 @@ namespace Timeline.Controllers [ProducesResponseType(typeof(void), StatusCodes.Status304NotModified)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status410Gone)] [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] public async Task DataGetAsync([FromRoute][Username] string owner, [FromRoute][TimelineName] string timeline, [FromRoute] long post, [FromRoute(Name = "data_index")][Range(0, 100)] long dataIndex) { @@ -103,10 +105,10 @@ namespace Timeline.Controllers } return await DataCacheHelper.GenerateActionResult(this, - () => _postService.GetPostDataDigestAsync(timelineId, post, dataIndex), + () => _postService.GetPostDataDigestV2Async(timelineId, post, dataIndex), async () => { - var data = await _postService.GetPostDataAsync(timelineId, post, dataIndex); + var data = await _postService.GetPostDataV2Async(timelineId, post, dataIndex); if (data.ContentType == MimeTypes.TextMarkdown) { return new ByteData(_markdownProcessor.Process(data.Data, Url, timeline, post), data.ContentType); diff --git a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs index f595af87..af36933c 100644 --- a/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/ITimelinePostService.cs @@ -78,6 +78,32 @@ namespace Timeline.Services.Timeline /// Thrown when post is deleted. Task GetPostV2Async(long timelineId, long postId); + /// + /// Get a data digest of a post. + /// + /// The timeline id. + /// The post id. + /// The index of the data. + /// The data digest. + /// Thrown when timeline does not exist. + /// Thrown when post of does not exist. + /// Thrown when post is deleted. + /// Thrown when data of that index does not exist. + Task GetPostDataDigestV2Async(long timelineId, long postId, long dataIndex); + + /// + /// Get a data of a post. + /// + /// The timeline id. + /// The post id. + /// The index of the data. + /// The data. + /// Thrown when timeline does not exist. + /// Thrown when post of does not exist. + /// Thrown when post is deleted. + /// Thrown when data of that index does not exist. + Task GetPostDataV2Async(long timelineId, long postId, long dataIndex); + /// /// Create a new post in timeline. /// diff --git a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs index 644e989f..e0212cb7 100644 --- a/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/Timeline/TimelinePostService.cs @@ -434,5 +434,47 @@ namespace Timeline.Services.Timeline return post; } + + public async Task GetPostDataDigestV2Async(long timelineId, long postId, long dataIndex) + { + await _timelineService.ThrowIfTimelineNotExist(timelineId); + + var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).Select(p => new { p.Id, p.Deleted }).SingleOrDefaultAsync(); + + if (postEntity is null) + throw CreatePostNotExistException(timelineId, postId, false); + + if (postEntity.Deleted) + throw CreatePostDeletedException(timelineId, postId); + + var dataEntity = await _database.TimelinePostData.Where(d => d.PostId == postEntity.Id && d.Index == dataIndex).SingleOrDefaultAsync(); + + if (dataEntity is null) + throw CreatePostDataNotExistException(timelineId, postId, dataIndex); + + return new CacheableDataDigest(dataEntity.DataTag, dataEntity.LastUpdated); + } + + public async Task GetPostDataV2Async(long timelineId, long postId, long dataIndex) + { + await _timelineService.ThrowIfTimelineNotExist(timelineId); + + var postEntity = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).Select(p => new { p.Id, p.Deleted }).SingleOrDefaultAsync(); + + if (postEntity is null) + throw CreatePostNotExistException(timelineId, postId, false); + + if (postEntity.Deleted) + throw CreatePostDeletedException(timelineId, postId); + + var dataEntity = await _database.TimelinePostData.Where(d => d.PostId == postEntity.Id && d.Index == dataIndex).SingleOrDefaultAsync(); + + if (dataEntity is null) + throw CreatePostDataNotExistException(timelineId, postId, dataIndex); + + var data = await _dataManager.GetEntryAndCheck(dataEntity.DataTag, $"Timeline {timelineId}, post {postId}, data {dataIndex} requires this data."); + + return new ByteData(data, dataEntity.Kind); + } } } -- cgit v1.2.3