diff options
author | crupest <crupest@outlook.com> | 2022-04-10 22:37:58 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-04-10 22:37:58 +0800 |
commit | 74bfab88dfdcdc08ee512253a803f2b3016e5f03 (patch) | |
tree | 79ef03b744245460983ed2cc3b0322ee16e50989 /BackEnd | |
parent | 9c874edbb35c87448ce91c142b127b450879e6b4 (diff) | |
download | timeline-74bfab88dfdcdc08ee512253a803f2b3016e5f03.tar.gz timeline-74bfab88dfdcdc08ee512253a803f2b3016e5f03.tar.bz2 timeline-74bfab88dfdcdc08ee512253a803f2b3016e5f03.zip |
...
Diffstat (limited to 'BackEnd')
5 files changed, 114 insertions, 9 deletions
diff --git a/BackEnd/Timeline/Controllers/TimelineBookmark1Controller.cs b/BackEnd/Timeline/Controllers/TimelineBookmark1Controller.cs index 1dfc1715..73d2078f 100644 --- a/BackEnd/Timeline/Controllers/TimelineBookmark1Controller.cs +++ b/BackEnd/Timeline/Controllers/TimelineBookmark1Controller.cs @@ -1,7 +1,12 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Timeline.Models; +using Timeline.Models.Http; +using Timeline.Services.Api; +using Timeline.Services.Timeline; +using Timeline.Services.User; namespace Timeline.Controllers { @@ -9,15 +14,63 @@ namespace Timeline.Controllers [Route("users/{username}/bookmarks")] public class TimelineBookmark1Controller : MyControllerBase { - public TimelineBookmark1Controller() + private readonly IUserService _userService; + private readonly ITimelineService _timelineService; + private readonly ITimelineBookmarkService1 _timelineBookmarkService; + + public TimelineBookmark1Controller(IUserService userService, ITimelineService timelineService, ITimelineBookmarkService1 timelineBookmarkService) + { + _userService = userService; + _timelineService = timelineService; + _timelineBookmarkService = timelineBookmarkService; + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] + [HttpGet] + public async Task<ActionResult<Page<TimelineBookmark>>> ListAsync([FromRoute] string username, [FromQuery] int? page, [FromQuery] int? pageSize) + { + var userId = await _userService.GetUserIdByUsernameAsync(username); + if (!UserHasPermission(UserPermission.UserBookmarkManagement) && !await _timelineBookmarkService.CanReadBookmarksAsync(userId, GetOptionalAuthUserId())) + { + return Forbid(); + } + return await _timelineBookmarkService.GetBookmarksAsync(userId, page ?? 1, pageSize ?? 20); + } + + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] + [HttpGet("{index}")] + public async Task<ActionResult<TimelineBookmark>> GetAsync([FromRoute] string username, [FromRoute] int index) { + var userId = await _userService.GetUserIdByUsernameAsync(username); + if (!UserHasPermission(UserPermission.UserBookmarkManagement) && !await _timelineBookmarkService.CanReadBookmarksAsync(userId, GetOptionalAuthUserId())) + { + return Forbid(); + } + return await _timelineBookmarkService.GetBookmarkAtAsync(userId, index); } - [ProducesResponseType(200)] - [ProducesResponseType(403)] - public async Task<ActionResult<TimelineBookmark>> ListAsync([FromRoute] string username) + [ProducesResponseType(StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status401Unauthorized)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)] + [Authorize] + public async Task<ActionResult<TimelineBookmark>> CreateAsync([FromRoute] string username, [FromBody] HttpTimelineBookmarkCreateRequest body) { - throw new NotImplementedException(); + var userId = await _userService.GetUserIdByUsernameAsync(username); + if (!UserHasPermission(UserPermission.UserBookmarkManagement) && GetAuthUserId() != userId) + { + return Forbid(); + } + var timelineId = await _timelineService.GetTimelineIdAsync(body.TimelineOwner, body.TimelineName); + var bookmark = await _timelineBookmarkService.AddBookmarkAsync(userId, timelineId, body.Position); + return CreatedAtAction("Get", new { username, index = bookmark.Position }, bookmark); } } } diff --git a/BackEnd/Timeline/Models/Http/HttpTimelineBookmarkCreateRequest.cs b/BackEnd/Timeline/Models/Http/HttpTimelineBookmarkCreateRequest.cs new file mode 100644 index 00000000..0a124503 --- /dev/null +++ b/BackEnd/Timeline/Models/Http/HttpTimelineBookmarkCreateRequest.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; +using Timeline.Models.Validation; + +namespace Timeline.Models.Http +{ + public class HttpTimelineBookmarkCreateRequest + { + [Required] + [Username] + public string TimelineOwner { get; set; } = default!; + + [Required] + [TimelineName] + public string TimelineName { get; set; } = default!; + + public int? Position { get; set; } + } +} + diff --git a/BackEnd/Timeline/Services/Api/ITimelineBookmarkService1.cs b/BackEnd/Timeline/Services/Api/ITimelineBookmarkService1.cs index f9ee4c15..0339e94a 100644 --- a/BackEnd/Timeline/Services/Api/ITimelineBookmarkService1.cs +++ b/BackEnd/Timeline/Services/Api/ITimelineBookmarkService1.cs @@ -10,6 +10,8 @@ namespace Timeline.Services.Api Task<TimelineBookmark> GetBookmarkAsync(long userId, long timelineId); + Task<TimelineBookmark> GetBookmarkAtAsync(long userId, int position); + Task<TimelineBookmark> AddBookmarkAsync(long userId, long timelineId, int? position = null); Task DeleteBookmarkAsync(long userId, long timelineId); diff --git a/BackEnd/Timeline/Services/Api/TimelineBookmarkService1.cs b/BackEnd/Timeline/Services/Api/TimelineBookmarkService1.cs index f047a12d..4b52d61c 100644 --- a/BackEnd/Timeline/Services/Api/TimelineBookmarkService1.cs +++ b/BackEnd/Timeline/Services/Api/TimelineBookmarkService1.cs @@ -29,6 +29,15 @@ namespace Timeline.Services.Api var user = await _userService.GetUserAsync(userId); var timeline = await _timelineService.GetTimelineAsync(timelineId); + if (await _databaseContext.BookmarkTimelines.AnyAsync(b => b.UserId == userId && b.TimelineId == timelineId)) + { + throw new EntityAlreadyExistException(EntityTypes.BookmarkTimeline, new Dictionary<string, object> + { + {"user-id", userId }, + {"timeline-id", timelineId } + }); + } + var count = await _databaseContext.BookmarkTimelines.Where(b => b.UserId == userId).CountAsync(); if (position.HasValue) @@ -92,7 +101,8 @@ namespace Timeline.Services.Api public async Task<TimelineBookmark> GetBookmarkAsync(long userId, long timelineId) { var user = await _userService.GetUserAsync(userId); - var timeline = await _timelineService.GetTimelineAsync(timelineId); var entity = await _databaseContext.BookmarkTimelines.Where(b => b.UserId == userId && b.TimelineId == timelineId).SingleOrDefaultAsync(); + var timeline = await _timelineService.GetTimelineAsync(timelineId); + var entity = await _databaseContext.BookmarkTimelines.Where(b => b.UserId == userId && b.TimelineId == timelineId).SingleOrDefaultAsync(); if (entity is null) { @@ -106,6 +116,25 @@ namespace Timeline.Services.Api return new TimelineBookmark(user.Username, timeline.Name is null ? "self" : timeline.Name, (int)entity.Rank); } + public async Task<TimelineBookmark> GetBookmarkAtAsync(long userId, int position) + { + var user = await _userService.GetUserAsync(userId); + var entity = await _databaseContext.BookmarkTimelines.Where(b => b.UserId == userId && b.Rank == position).SingleOrDefaultAsync(); + + if (entity is null) + { + throw new EntityNotExistException(EntityTypes.BookmarkTimeline, new Dictionary<string, object> + { + { "user-id", userId }, + { "position", position } + }); + } + + var timeline = await _timelineService.GetTimelineAsync(entity.TimelineId); + + return new TimelineBookmark(user.Username, timeline.Name is null ? "self" : timeline.Name, (int)entity.Rank); + } + public async Task<Page<TimelineBookmark>> GetBookmarksAsync(long userId, int page, int pageSize) { if (page <= 0) throw new ArgumentOutOfRangeException(nameof(page)); diff --git a/BackEnd/Timeline/Services/User/UserPermission.cs b/BackEnd/Timeline/Services/User/UserPermission.cs index 1404cdcd..097a7bcf 100644 --- a/BackEnd/Timeline/Services/User/UserPermission.cs +++ b/BackEnd/Timeline/Services/User/UserPermission.cs @@ -13,6 +13,7 @@ /// <summary>
/// This permission allow to add or remove highlight timelines.
/// </summary>
- HighlightTimelineManagement
+ HighlightTimelineManagement,
+ UserBookmarkManagement,
}
}
|