From fb7e9fdc042fa14656a6cdd21f4b002d92badb29 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 17 Dec 2020 20:24:22 +0800 Subject: ... --- .../Controllers/HighlightTimelineController.cs | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 BackEnd/Timeline/Controllers/HighlightTimelineController.cs (limited to 'BackEnd/Timeline/Controllers/HighlightTimelineController.cs') diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs new file mode 100644 index 00000000..3819bfc4 --- /dev/null +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -0,0 +1,63 @@ +using AutoMapper; +using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; +using System.Threading.Tasks; +using Timeline.Auth; +using Timeline.Models.Http; +using Timeline.Models.Validation; +using Timeline.Services; +using Timeline.Services.Exceptions; + +namespace Timeline.Controllers +{ + /// + /// Api related to highlight timeline. + /// + [ApiController] + [ProducesErrorResponseType(typeof(CommonResponse))] + [Route("highlights")] + public class HighlightTimelineController : Controller + { + private readonly IHighlightTimelineService _service; + private readonly IMapper _mapper; + + public HighlightTimelineController(IHighlightTimelineService service, IMapper mapper) + { + _service = service; + _mapper = mapper; + } + + /// + /// Get all highlight timelines. + /// + /// Highlight timeline list. + [HttpGet] + [ProducesResponseType(200)] + public async Task>> List() + { + var t = await _service.GetHighlightTimelines(); + return _mapper.Map>(t); + } + + /// + /// Add a timeline to highlight list. + /// + /// + [HttpPut("{timeline}")] + [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + public async Task Put([GeneralTimelineName] string timeline) + { + try + { + await _service.AddHighlightTimeline(timeline, this.GetUserId()); + return Ok(); + } + catch (TimelineNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.NotExist()); + } + } + } +} -- cgit v1.2.3 From a0f8b2ba0d41678b09bff2adaac94e5bac717b9b Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 17 Dec 2020 23:09:24 +0800 Subject: ... --- BackEnd/Timeline.ErrorCodes/ErrorCodes.cs | 5 +++ .../Controllers/HighlightTimelineController.cs | 52 ++++++++++++++++++++-- BackEnd/Timeline/Models/Http/HighlightTimeline.cs | 17 +++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 BackEnd/Timeline/Models/Http/HighlightTimeline.cs (limited to 'BackEnd/Timeline/Controllers/HighlightTimelineController.cs') diff --git a/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs b/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs index 90c4ed99..a8519216 100644 --- a/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs +++ b/BackEnd/Timeline.ErrorCodes/ErrorCodes.cs @@ -63,6 +63,11 @@ public const int PostNotExist = 1_104_05_01; public const int PostNoData = 1_104_05_02; } + + public static class HighlightTimelineController + { + public const int NonHighlight = 1_105_01_01; + } } } diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index 3819bfc4..0b6e1665 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -15,7 +15,6 @@ namespace Timeline.Controllers /// [ApiController] [ProducesErrorResponseType(typeof(CommonResponse))] - [Route("highlights")] public class HighlightTimelineController : Controller { private readonly IHighlightTimelineService _service; @@ -31,7 +30,7 @@ namespace Timeline.Controllers /// Get all highlight timelines. /// /// Highlight timeline list. - [HttpGet] + [HttpGet("highlights")] [ProducesResponseType(200)] public async Task>> List() { @@ -42,8 +41,8 @@ namespace Timeline.Controllers /// /// Add a timeline to highlight list. /// - /// - [HttpPut("{timeline}")] + /// The timeline name. + [HttpPut("highlights/{timeline}")] [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] @@ -59,5 +58,50 @@ namespace Timeline.Controllers return BadRequest(ErrorResponse.TimelineController.NotExist()); } } + + /// + /// Remove a timeline from highlight list. + /// + /// Timeline name. + [HttpDelete("highlights/{timeline}")] + [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + public async Task Delete([GeneralTimelineName] string timeline) + { + try + { + await _service.RemoveHighlightTimeline(timeline, this.GetUserId()); + return Ok(); + } + catch (TimelineNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.NotExist()); + } + } + + /// + /// Move a highlight position. + /// + [HttpPost("highlightop/move")] + [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] + [ProducesResponseType(200)] + [ProducesResponseType(400)] + public async Task Move([FromBody] HttpHighlightTimelineMoveRequest body) + { + try + { + await _service.MoveHighlightTimeline(body.Timeline, body.NewPosition!.Value); + return Ok(); + } + catch (TimelineNotExistException) + { + return BadRequest(ErrorResponse.TimelineController.NotExist()); + } + catch (InvalidHighlightTimelineException) + { + return BadRequest(new CommonResponse(ErrorCodes.HighlightTimelineController.NonHighlight, "Can't move a non-highlight timeline.")); + } + } } } diff --git a/BackEnd/Timeline/Models/Http/HighlightTimeline.cs b/BackEnd/Timeline/Models/Http/HighlightTimeline.cs new file mode 100644 index 00000000..e5aed068 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/HighlightTimeline.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; +using Timeline.Models.Validation; + +namespace Timeline.Models.Http +{ + /// + /// Move highlight timeline request body model. + /// + public class HttpHighlightTimelineMoveRequest + { + [GeneralTimelineName] + public string Timeline { get; set; } = default!; + + [Required] + public long? NewPosition { get; set; } + } +} -- cgit v1.2.3