From cf8a869de33cfa5db1967698059abccaaeaba4c9 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 +++++++++++++++++++++ .../Controllers/HighlightTimelineController.cs | 8 +- BackEnd/Timeline/Models/Http/BookmarkTimeline.cs | 23 +++++ BackEnd/Timeline/Models/Http/HighlightTimeline.cs | 6 ++ BackEnd/Timeline/Startup.cs | 1 + 5 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 BackEnd/Timeline/Controllers/BookmarkTimelineController.cs create mode 100644 BackEnd/Timeline/Models/Http/BookmarkTimeline.cs (limited to 'BackEnd/Timeline') 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.")); + } + } + } +} diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index 0b6e1665..519d6161 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -46,6 +46,8 @@ namespace Timeline.Controllers [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] public async Task Put([GeneralTimelineName] string timeline) { try @@ -67,6 +69,8 @@ namespace Timeline.Controllers [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] public async Task Delete([GeneralTimelineName] string timeline) { try @@ -81,12 +85,14 @@ namespace Timeline.Controllers } /// - /// Move a highlight position. + /// Move a highlight to new position. /// [HttpPost("highlightop/move")] [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] + [ProducesResponseType(401)] + [ProducesResponseType(403)] public async Task Move([FromBody] HttpHighlightTimelineMoveRequest body) { try diff --git a/BackEnd/Timeline/Models/Http/BookmarkTimeline.cs b/BackEnd/Timeline/Models/Http/BookmarkTimeline.cs new file mode 100644 index 00000000..14be1112 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/BookmarkTimeline.cs @@ -0,0 +1,23 @@ +using System.ComponentModel.DataAnnotations; +using Timeline.Models.Validation; + +namespace Timeline.Models.Http +{ + /// + /// Move bookmark timeline request body model. + /// + public class HttpBookmarkTimelineMoveRequest + { + /// + /// Timeline name. + /// + [GeneralTimelineName] + public string Timeline { get; set; } = default!; + + /// + /// New position, starting at 1. + /// + [Required] + public long? NewPosition { get; set; } + } +} diff --git a/BackEnd/Timeline/Models/Http/HighlightTimeline.cs b/BackEnd/Timeline/Models/Http/HighlightTimeline.cs index e5aed068..5af0e528 100644 --- a/BackEnd/Timeline/Models/Http/HighlightTimeline.cs +++ b/BackEnd/Timeline/Models/Http/HighlightTimeline.cs @@ -8,9 +8,15 @@ namespace Timeline.Models.Http /// public class HttpHighlightTimelineMoveRequest { + /// + /// Timeline name. + /// [GeneralTimelineName] public string Timeline { get; set; } = default!; + /// + /// New position, starting at 1. + /// [Required] public long? NewPosition { get; set; } } diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index d20fc54b..66c708ac 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -102,6 +102,7 @@ namespace Timeline services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddDbContext((services, options) => { -- cgit v1.2.3