From 6ac5ba10f9c417f63a4713612a0abf5b20deb099 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 19 Dec 2020 20:08:49 +0800 Subject: feat: Bookmark timeline REST api. --- .../Controllers/BookmarkTimelineController.cs | 114 +++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 BackEnd/Timeline/Controllers/BookmarkTimelineController.cs (limited to 'BackEnd/Timeline/Controllers/BookmarkTimelineController.cs') 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 +{ + /// + /// Api related to timeline bookmarks. + /// + [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; + } + + /// + /// Get bookmark list in order. + /// + /// Bookmarks. + [HttpGet("bookmarks")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(401)] + public async Task>> List() + { + var bookmarks = await _service.GetBookmarks(this.GetUserId()); + return Ok(_mapper.Map>(bookmarks)); + } + + /// + /// Add a bookmark. + /// + /// Timeline name. + [HttpPut("bookmarks/{timeline}")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + [ProducesResponseType(401)] + public async Task Put([GeneralTimelineName] string timeline) + { + try + { + await _service.AddBookmark(this.GetUserId(), timeline); + return Ok(); + } + catch (TimelineNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.NotExist()); + } + } + + /// + /// Remove a bookmark. + /// + /// Timeline name. + [HttpDelete("bookmarks/{timeline}")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + [ProducesResponseType(401)] + public async Task Delete([GeneralTimelineName] string timeline) + { + try + { + await _service.RemoveBookmark(this.GetUserId(), timeline); + return Ok(); + } + catch (TimelineNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.NotExist()); + } + } + + /// + /// Move a bookmark to new posisition. + /// + /// Request body. + [HttpPost("bookmarkop/move")] + [Authorize] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + [ProducesResponseType(401)] + public async Task 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.")); + } + } + } +} -- cgit v1.2.3