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 : MyControllerBase
{
private readonly IHighlightTimelineService _service;
private readonly ITimelineService _timelineService;
private readonly IGenericMapper _mapper;
public HighlightTimelineController(IHighlightTimelineService service, ITimelineService timelineService, IGenericMapper mapper)
{
_service = service;
_timelineService = timelineService;
_mapper = mapper;
}
private Task> Map(List timelines)
{
return _mapper.MapListAsync(timelines, Url, User);
}
///
/// Get all highlight timelines.
///
/// Highlight timeline list.
[HttpGet("highlights")]
[ProducesResponseType(200)]
public async Task>> List()
{
var ids = await _service.GetHighlightTimelinesAsync();
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)
{
var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline);
var create = await _service.AddHighlightTimelineAsync(timelineId, GetUserId());
return CommonPutResponse.Create(create);
}
///
/// 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)
{
var timelineId = await _timelineService.GetTimelineIdByNameAsync(timeline);
var delete = await _service.RemoveHighlightTimelineAsync(timelineId, GetUserId());
return CommonDeleteResponse.Create(delete);
}
///
/// 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.GetTimelineIdByNameAsync(body.Timeline);
await _service.MoveHighlightTimelineAsync(timelineId, body.NewPosition!.Value);
return Ok();
}
catch (InvalidHighlightTimelineException)
{
return BadRequest(new CommonResponse(ErrorCodes.HighlightTimelineController.NonHighlight, "Can't move a non-highlight timeline."));
}
}
}
}