using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
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 timeline bookmarks.
///
[ApiController]
[ProducesErrorResponseType(typeof(CommonResponse))]
public class BookmarkTimelineController : Controller
{
private readonly IBookmarkTimelineService _service;
private readonly ITimelineService _timelineService;
private readonly TimelineMapper _timelineMapper;
public BookmarkTimelineController(IBookmarkTimelineService 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 bookmark list in order.
///
/// Bookmarks.
[HttpGet("bookmarks")]
[Authorize]
[ProducesResponseType(200)]
[ProducesResponseType(401)]
public async Task>> List()
{
var ids = await _service.GetBookmarks(this.GetUserId());
var timelines = await _timelineService.GetTimelineList(ids);
return await Map(timelines);
}
///
/// Add a bookmark.
///
/// Timeline name.
[HttpPut("bookmarks/{timeline}")]
[Authorize]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
public async Task> Put([GeneralTimelineName] string timeline)
{
try
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
var create = await _service.AddBookmark(this.GetUserId(), timelineId);
return CommonPutResponse.Create(create);
}
catch (TimelineNotExistException)
{
return BadRequest(ErrorResponse.TimelineController.NotExist());
}
}
///
/// Remove a bookmark.
///
/// Timeline name.
[HttpDelete("bookmarks/{timeline}")]
[Authorize]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
public async Task> Delete([GeneralTimelineName] string timeline)
{
try
{
var timelineId = await _timelineService.GetTimelineIdByName(timeline);
var delete = await _service.RemoveBookmark(this.GetUserId(), timelineId);
return CommonDeleteResponse.Create(delete);
}
catch (TimelineNotExistException)
{
return BadRequest(ErrorResponse.TimelineController.NotExist());
}
}
///
/// Move a bookmark to new posisition.
///
/// Request body.
[HttpPost("bookmarkop/move")]
[Authorize]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
public async Task Move([FromBody] HttpBookmarkTimelineMoveRequest request)
{
try
{
var timelineId = await _timelineService.GetTimelineIdByName(request.Timeline);
await _service.MoveBookmark(this.GetUserId(), timelineId, request.NewPosition!.Value);
return Ok();
}
catch (TimelineNotExistException)
{
return BadRequest(ErrorResponse.TimelineController.NotExist());
}
catch (InvalidBookmarkException)
{
return BadRequest(new CommonResponse(ErrorCodes.BookmarkTimelineController.NonBookmark, "You can't move a non-bookmark timeline."));
}
}
}
}