diff options
author | crupest <crupest@outlook.com> | 2021-02-04 22:03:08 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-02-04 22:04:39 +0800 |
commit | c987364ce795c7f4fe837ad8c2a685558c25b0b5 (patch) | |
tree | e926e2bd10093eaadff8d3bb75114ab382aae0cd /BackEnd/Timeline | |
parent | 76d8616a6ba18f2bf86c46d865a9426e5accf55f (diff) | |
download | timeline-c987364ce795c7f4fe837ad8c2a685558c25b0b5.tar.gz timeline-c987364ce795c7f4fe837ad8c2a685558c25b0b5.tar.bz2 timeline-c987364ce795c7f4fe837ad8c2a685558c25b0b5.zip |
...
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r-- | BackEnd/Timeline/Controllers/TimelinePostController.cs | 39 | ||||
-rw-r--r-- | BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 4 | ||||
-rw-r--r-- | BackEnd/Timeline/Services/TimelinePostService.cs | 30 |
3 files changed, 67 insertions, 6 deletions
diff --git a/BackEnd/Timeline/Controllers/TimelinePostController.cs b/BackEnd/Timeline/Controllers/TimelinePostController.cs index 3f31decf..0148f56e 100644 --- a/BackEnd/Timeline/Controllers/TimelinePostController.cs +++ b/BackEnd/Timeline/Controllers/TimelinePostController.cs @@ -53,7 +53,7 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<ActionResult<List<HttpTimelinePost>>> PostList([FromRoute][GeneralTimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted)
+ public async Task<ActionResult<List<HttpTimelinePost>>> List([FromRoute][GeneralTimelineName] string timeline, [FromQuery] DateTime? modifiedSince, [FromQuery] bool? includeDeleted)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
@@ -69,6 +69,37 @@ namespace Timeline.Controllers }
/// <summary>
+ /// Get a post of a timeline.
+ /// </summary>
+ /// <param name="timeline">The name of the timeline.</param>
+ /// <param name="postId">The post id.</param>
+ /// <returns>The post.</returns>
+ [HttpGet("{post}")]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status403Forbidden)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
+ public async Task<ActionResult<HttpTimelinePost>> Get([FromRoute][GeneralTimelineName] string timeline, [FromRoute(Name = "post")] long postId)
+ {
+ var timelineId = await _timelineService.GetTimelineIdByName(timeline);
+
+ if (!UserHasAllTimelineManagementPermission && !await _timelineService.HasReadPermission(timelineId, this.GetOptionalUserId()))
+ {
+ return StatusCode(StatusCodes.Status403Forbidden, ErrorResponse.Common.Forbid());
+ }
+
+ try
+ {
+ var post = await _postService.GetPost(timelineId, postId);
+ var result = await _timelineMapper.MapToHttp(post, timeline, Url);
+ return result;
+ }
+ catch (TimelinePostNotExistException)
+ {
+ return NotFound(ErrorResponse.TimelineController.PostNotExist());
+ }
+ }
+
+ /// <summary>
/// Get the data of a post. Usually a image post.
/// </summary>
/// <param name="timeline">Timeline name.</param>
@@ -82,7 +113,7 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
- public async Task<IActionResult> PostDataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch)
+ public async Task<IActionResult> DataGet([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post, [FromHeader(Name = "If-None-Match")] string? ifNoneMatch)
{
_ = ifNoneMatch;
@@ -121,7 +152,7 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
- public async Task<ActionResult<HttpTimelinePost>> PostPost([FromRoute][GeneralTimelineName] string timeline, [FromBody] HttpTimelinePostCreateRequest body)
+ public async Task<ActionResult<HttpTimelinePost>> Post([FromRoute][GeneralTimelineName] string timeline, [FromBody] HttpTimelinePostCreateRequest body)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
var userId = this.GetUserId();
@@ -193,7 +224,7 @@ namespace Timeline.Controllers [ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
- public async Task<ActionResult> PostDelete([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post)
+ public async Task<ActionResult> Delete([FromRoute][GeneralTimelineName] string timeline, [FromRoute] long post)
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 94e55237..88c96d8a 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -49,7 +49,7 @@ namespace Timeline.Models.Mapper isBookmark: userId is not null && await _bookmarkTimelineService.IsBookmark(userId.Value, entity.Id, false, false),
links: new HttpTimelineLinks(
self: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = timelineName }),
- posts: urlHelper.ActionLink(nameof(TimelinePostController.PostList), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName })
+ posts: urlHelper.ActionLink(nameof(TimelinePostController.List), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName })
)
);
}
@@ -84,7 +84,7 @@ namespace Timeline.Models.Mapper (
type: TimelinePostContentTypes.Image,
text: null,
- url: urlHelper.ActionLink(nameof(TimelinePostController.PostDataGet), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName, post = entity.LocalId }),
+ url: urlHelper.ActionLink(nameof(TimelinePostController.DataGet), nameof(TimelinePostController)[0..^nameof(Controller).Length], new { timeline = timelineName, post = entity.LocalId }),
eTag: $"\"{entity.Content}\""
),
_ => throw new DatabaseCorruptedException(string.Format(CultureInfo.InvariantCulture, "Unknown timeline post type {0}.", entity.ContentType))
diff --git a/BackEnd/Timeline/Services/TimelinePostService.cs b/BackEnd/Timeline/Services/TimelinePostService.cs index c2b773ff..cf5f4e55 100644 --- a/BackEnd/Timeline/Services/TimelinePostService.cs +++ b/BackEnd/Timeline/Services/TimelinePostService.cs @@ -45,6 +45,17 @@ namespace Timeline.Services Task<List<TimelinePostEntity>> GetPosts(long timelineId, DateTime? modifiedSince = null, bool includeDeleted = false);
/// <summary>
+ /// Get a post of a timeline.
+ /// </summary>
+ /// <param name="timelineId">The id of the timeline of the post.</param>
+ /// <param name="postId">The id of the post.</param>
+ /// <param name="includeDelete">If true, return the entity even if it is deleted.</param>
+ /// <returns>The post.</returns>
+ /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
+ /// <exception cref="TimelinePostNotExistException">Thrown when post of <paramref name="postId"/> does not exist or has been deleted.</exception>
+ Task<TimelinePostEntity> GetPost(long timelineId, long postId, bool includeDelete = false);
+
+ /// <summary>
/// Get the etag of data of a post.
/// </summary>
/// <param name="timelineId">The id of the timeline of the post.</param>
@@ -189,6 +200,25 @@ namespace Timeline.Services return await query.ToListAsync();
}
+ public async Task<TimelinePostEntity> GetPost(long timelineId, long postId, bool includeDelete = false)
+ {
+ await CheckTimelineExistence(timelineId);
+
+ var post = await _database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.LocalId == postId).SingleOrDefaultAsync();
+
+ if (post is null)
+ {
+ throw new TimelinePostNotExistException(timelineId, postId, false);
+ }
+
+ if (!includeDelete && post.Content is null)
+ {
+ throw new TimelinePostNotExistException(timelineId, postId, true);
+ }
+
+ return post;
+ }
+
public async Task<string> GetPostDataETag(long timelineId, long postId)
{
await CheckTimelineExistence(timelineId);
|