aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-12-19 20:08:49 +0800
committercrupest <crupest@outlook.com>2020-12-19 20:08:49 +0800
commitcf8a869de33cfa5db1967698059abccaaeaba4c9 (patch)
tree5e4a504e02b9d8592be7ec5b1aba28d2dd876ddf /BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
parentab3aedad37fe4634efb0d6939d7a40642bfff032 (diff)
downloadtimeline-cf8a869de33cfa5db1967698059abccaaeaba4c9.tar.gz
timeline-cf8a869de33cfa5db1967698059abccaaeaba4c9.tar.bz2
timeline-cf8a869de33cfa5db1967698059abccaaeaba4c9.zip
feat: Bookmark timeline REST api.
Diffstat (limited to 'BackEnd/Timeline/Controllers/BookmarkTimelineController.cs')
-rw-r--r--BackEnd/Timeline/Controllers/BookmarkTimelineController.cs114
1 files changed, 114 insertions, 0 deletions
diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
new file mode 100644
index 00000000..9dff95f3
--- /dev/null
+++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
@@ -0,0 +1,114 @@
+using AutoMapper;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Timeline.Models.Http;
+using Timeline.Models.Validation;
+using Timeline.Services;
+using Timeline.Services.Exceptions;
+
+namespace Timeline.Controllers
+{
+ /// <summary>
+ /// Api related to timeline bookmarks.
+ /// </summary>
+ [ApiController]
+ [ProducesErrorResponseType(typeof(CommonResponse))]
+ public class BookmarkTimelineController : Controller
+ {
+ private readonly IBookmarkTimelineService _service;
+
+ private readonly IMapper _mapper;
+
+ public BookmarkTimelineController(IBookmarkTimelineService service, IMapper mapper)
+ {
+ _service = service;
+ _mapper = mapper;
+ }
+
+ /// <summary>
+ /// Get bookmark list in order.
+ /// </summary>
+ /// <returns>Bookmarks.</returns>
+ [HttpGet("bookmarks")]
+ [Authorize]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(401)]
+ public async Task<ActionResult<List<HttpTimeline>>> List()
+ {
+ var bookmarks = await _service.GetBookmarks(this.GetUserId());
+ return Ok(_mapper.Map<List<HttpTimeline>>(bookmarks));
+ }
+
+ /// <summary>
+ /// Add a bookmark.
+ /// </summary>
+ /// <param name="timeline">Timeline name.</param>
+ [HttpPut("bookmarks/{timeline}")]
+ [Authorize]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ [ProducesResponseType(401)]
+ public async Task<ActionResult> Put([GeneralTimelineName] string timeline)
+ {
+ try
+ {
+ await _service.AddBookmark(this.GetUserId(), timeline);
+ return Ok();
+ }
+ catch (TimelineNotExistException)
+ {
+ return BadRequest(ErrorResponse.TimelineController.NotExist());
+ }
+ }
+
+ /// <summary>
+ /// Remove a bookmark.
+ /// </summary>
+ /// <param name="timeline">Timeline name.</param>
+ [HttpDelete("bookmarks/{timeline}")]
+ [Authorize]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ [ProducesResponseType(401)]
+ public async Task<ActionResult> Delete([GeneralTimelineName] string timeline)
+ {
+ try
+ {
+ await _service.RemoveBookmark(this.GetUserId(), timeline);
+ return Ok();
+ }
+ catch (TimelineNotExistException)
+ {
+ return BadRequest(ErrorResponse.TimelineController.NotExist());
+ }
+ }
+
+ /// <summary>
+ /// Move a bookmark to new posisition.
+ /// </summary>
+ /// <param name="request">Request body.</param>
+ [HttpPost("bookmarkop/move")]
+ [Authorize]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ [ProducesResponseType(401)]
+ public async Task<ActionResult> Move([FromBody] HttpBookmarkTimelineMoveRequest request)
+ {
+ try
+ {
+ await _service.MoveBookmark(this.GetUserId(), request.Timeline, request.NewPosition!.Value);
+ return Ok();
+ }
+ catch (TimelineNotExistException)
+ {
+ return BadRequest(ErrorResponse.TimelineController.NotExist());
+ }
+ catch (InvalidBookmarkException)
+ {
+ return BadRequest(new CommonResponse(ErrorCodes.BookmarkTimelineController.NonBookmark, "You can't move a non-bookmark timeline."));
+ }
+ }
+ }
+}