using AutoMapper; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Models.Http; using Timeline.Models.Mapper; using Timeline.Models.Validation; using Timeline.Services; using Timeline.Services.Exceptions; namespace Timeline.Controllers { /// /// Api related to highlight timeline. /// [ApiController] [ProducesErrorResponseType(typeof(CommonResponse))] public class HighlightTimelineController : Controller { private readonly IHighlightTimelineService _service; private readonly ITimelineService _timelineService; public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService) { _service = service; _timelineService = timelineService; } /// /// Get all highlight timelines. /// /// Highlight timeline list. [HttpGet("highlights")] [ProducesResponseType(200)] public async Task>> List() { var ids = await _service.GetHighlightTimelines(); var timelines = await _timelineService.GetTimelineList(ids); return timelines.MapToHttp(Url); } /// /// Add a timeline to highlight list. /// /// The timeline name. [HttpPut("highlights/{timeline}")] [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] [ProducesResponseType(401)] [ProducesResponseType(403)] public async Task Put([GeneralTimelineName] string timeline) { try { await _service.AddHighlightTimeline(timeline, this.GetUserId()); return Ok(); } catch (TimelineNotExistException) { return BadRequest(ErrorResponse.TimelineController.NotExist()); } } /// /// Remove a timeline from highlight list. /// /// Timeline name. [HttpDelete("highlights/{timeline}")] [PermissionAuthorize(UserPermission.HighlightTimelineManagement)] [ProducesResponseType(200)] [ProducesResponseType(400)] [ProducesResponseType(401)] [ProducesResponseType(403)] 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 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 { 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.")); } } } }