using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; using Timeline.Auth; using Timeline.Entities; using Timeline.Models.Http; using Timeline.Models.Validation; using Timeline.Services.Api; using Timeline.Services.Mapper; using Timeline.Services.Timeline; using Timeline.Services.User; namespace Timeline.Controllers { /// /// Api related to highlight timeline. /// [ApiController] [ProducesErrorResponseType(typeof(CommonResponse))] public class HighlightTimelineController : Controller { private readonly IHighlightTimelineService _service; private readonly ITimelineService _timelineService; private readonly TimelineMapper _timelineMapper; public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService, TimelineMapper timelineMapper) { _service = service; _timelineService = timelineService; _timelineMapper = timelineMapper; } private Task> Map(List timelines) { return _timelineMapper.MapToHttp(timelines, Url, this.GetOptionalUserId(), this.UserHasPermission(UserPermission.AllTimelineManagement)); } /// /// 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 await Map(timelines); } /// /// 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 { var timelineId = await _timelineService.GetTimelineIdByName(timeline); var create = await _service.AddHighlightTimeline(timelineId, this.GetUserId()); return CommonPutResponse.Create(create); } 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 { var timelineId = await _timelineService.GetTimelineIdByName(timeline); var delete = await _service.RemoveHighlightTimeline(timelineId, this.GetUserId()); return CommonDeleteResponse.Create(delete); } 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 { var timelineId = await _timelineService.GetTimelineIdByName(body.Timeline); await _service.MoveHighlightTimeline(timelineId, 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.")); } } } }