using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
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 timeline bookmarks.
///
[ApiController]
[ProducesErrorResponseType(typeof(CommonResponse))]
public class BookmarkTimelineController : Controller
{
private readonly IBookmarkTimelineService _service;
private readonly ITimelineService _timelineService;
public BookmarkTimelineController(IBookmarkTimelineService service, ITimelineService timelineService)
{
_service = service;
_timelineService = timelineService;
}
///
/// 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 Ok(timelines.MapToHttp(Url));
}
///
/// Add a bookmark.
///
/// Timeline name.
[HttpPut("bookmarks/{timeline}")]
[Authorize]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(401)]
public async Task Put([GeneralTimelineName] string timeline)
{
try
{
await _service.AddBookmark(this.GetUserId(), timeline);
return Ok();
}
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
{
await _service.RemoveBookmark(this.GetUserId(), timeline);
return Ok();
}
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
{
await _service.MoveBookmark(this.GetUserId(), request.Timeline, 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."));
}
}
}
}