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))] 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("highlights")] [ProducesResponseType(200)] public async Task>> List() { var t = await _service.GetHighlightTimelines(); return _mapper.Map>(t); } /// /// 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.")); } } } }