aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs
blob: 64cb8afa1a454b75bd5ae30838958e3f3f218766 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
{
    /// <summary>
    /// Api related to timeline bookmarks.
    /// </summary>
    [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;
        }

        /// <summary>
        /// Get bookmark list in order.
        /// </summary>
        /// <returns>Bookmarks.</returns>
        [HttpGet("bookmarks")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(401)]
        public async Task<ActionResult<List<HttpTimeline>>> List()
        {
            var ids = await _service.GetBookmarks(this.GetUserId());
            var timelines = await _timelineService.GetTimelineList(ids);
            return await _timelineMapper.MapToHttp(timelines, Url);
        }

        /// <summary>
        /// Add a bookmark.
        /// </summary>
        /// <param name="timeline">Timeline name.</param>
        [HttpPut("bookmarks/{timeline}")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task<ActionResult> Put([GeneralTimelineName] string timeline)
        {
            try
            {
                await _service.AddBookmark(this.GetUserId(), timeline);
                return Ok();
            }
            catch (TimelineNotExistException)
            {
                return BadRequest(ErrorResponse.TimelineController.NotExist());
            }
        }

        /// <summary>
        /// Remove a bookmark.
        /// </summary>
        /// <param name="timeline">Timeline name.</param>
        [HttpDelete("bookmarks/{timeline}")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task<ActionResult> Delete([GeneralTimelineName] string timeline)
        {
            try
            {
                await _service.RemoveBookmark(this.GetUserId(), timeline);
                return Ok();
            }
            catch (TimelineNotExistException)
            {
                return BadRequest(ErrorResponse.TimelineController.NotExist());
            }
        }

        /// <summary>
        /// Move a bookmark to new posisition.
        /// </summary>
        /// <param name="request">Request body.</param>
        [HttpPost("bookmarkop/move")]
        [Authorize]
        [ProducesResponseType(200)]
        [ProducesResponseType(400)]
        [ProducesResponseType(401)]
        public async Task<ActionResult> 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."));
            }
        }
    }
}