2021-11-14 03:27:01 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-11-16 05:44:16 +00:00
|
|
|
|
using System.Linq;
|
2021-11-13 03:21:59 +00:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-11-22 20:52:41 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-11-13 03:21:59 +00:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Start.Server.Data.Services.Interfaces;
|
2021-11-14 03:27:01 +00:00
|
|
|
|
using Start.Server.Extensions;
|
2021-11-16 05:44:16 +00:00
|
|
|
|
using Start.Shared;
|
2021-11-13 03:21:59 +00:00
|
|
|
|
|
|
|
|
|
namespace Start.Server.Controllers {
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
2021-11-21 06:47:10 +00:00
|
|
|
|
[Route("[controller]/[action]")]
|
2021-11-13 03:21:59 +00:00
|
|
|
|
public class BookmarksController : ControllerBase {
|
2021-11-14 03:27:01 +00:00
|
|
|
|
private readonly IBookmarkContainerService bookmarkContainerService;
|
|
|
|
|
private readonly IBookmarkGroupService bookmarkGroupService;
|
2021-11-13 03:21:59 +00:00
|
|
|
|
private readonly IBookmarkService bookmarkService;
|
|
|
|
|
|
2021-11-14 03:27:01 +00:00
|
|
|
|
public BookmarksController(IBookmarkContainerService bookmarkContainerService,
|
|
|
|
|
IBookmarkGroupService bookmarkGroupService, IBookmarkService bookmarkService) {
|
|
|
|
|
this.bookmarkContainerService = bookmarkContainerService;
|
|
|
|
|
this.bookmarkGroupService = bookmarkGroupService;
|
2021-11-13 03:21:59 +00:00
|
|
|
|
this.bookmarkService = bookmarkService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<BookmarkContainerDto>))]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult GetAllBookmarkContainers() {
|
|
|
|
|
List<BookmarkContainerDto>? containers = this.bookmarkContainerService
|
|
|
|
|
.GetUserBookmarkContainers(this.GetAuthorizedUserId())
|
2021-11-16 05:44:16 +00:00
|
|
|
|
.Select(bc => bc.MapToDto())
|
|
|
|
|
.ToList();
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (containers == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(containers);
|
2021-11-13 03:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-11-21 06:47:10 +00:00
|
|
|
|
[Route("{bookmarkContainerId}")]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkContainerDto))]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult GetBookmarkContainer(int bookmarkContainerId) {
|
|
|
|
|
BookmarkContainerDto? container = this.bookmarkContainerService
|
2021-11-21 06:47:10 +00:00
|
|
|
|
.GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
?.MapToDto();
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (container == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(container);
|
2021-11-13 03:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
[HttpGet]
|
2021-11-21 06:47:10 +00:00
|
|
|
|
[Route("{bookmarkId}")]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult GetBookmark(int bookmarkId) {
|
|
|
|
|
BookmarkDto? bookmark = this.bookmarkService
|
2021-11-21 06:47:10 +00:00
|
|
|
|
.GetBookmark(this.GetAuthorizedUserId(), bookmarkId)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
?.MapToDto();
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(bookmark);
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-13 03:21:59 +00:00
|
|
|
|
[HttpPost]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkDto))]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public IActionResult CreateBookmark(string title, string url, string? notes,
|
2021-11-13 03:21:59 +00:00
|
|
|
|
int bookmarkGroupId) {
|
2021-11-22 20:52:41 +00:00
|
|
|
|
BookmarkDto? bookmark = this.bookmarkService
|
2021-11-21 06:47:10 +00:00
|
|
|
|
.CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
?.MapToDto();
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
return Created(
|
|
|
|
|
Url.Action(nameof(this.GetBookmark),new { bookmarkId = bookmark.BookmarkId }),
|
|
|
|
|
bookmark);
|
2021-11-16 05:44:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkContainerDto))]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
|
|
public IActionResult CreateBookmarkContainer([FromBody] string title) {
|
|
|
|
|
BookmarkContainerDto? container = this.bookmarkContainerService
|
2021-11-21 06:47:10 +00:00
|
|
|
|
.CreateBookmarkContainer(this.GetAuthorizedUserId(), title)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
?.MapToDto();
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (container == null)
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
return Created(
|
|
|
|
|
Url.Action(nameof(this.GetBookmarkContainer),
|
|
|
|
|
new { bookmarkContainerId = container.BookmarkContainerId }),
|
|
|
|
|
container);
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
2021-11-21 06:47:10 +00:00
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
2021-11-22 20:52:41 +00:00
|
|
|
|
[Route("{bookmarkContainerId}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult DeleteBookmarkContainer(int bookmarkContainerId) {
|
|
|
|
|
bool res = this.bookmarkContainerService
|
2021-11-21 06:47:10 +00:00
|
|
|
|
.DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId);
|
2021-11-22 20:52:41 +00:00
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok();
|
2021-11-21 06:47:10 +00:00
|
|
|
|
}
|
2021-11-13 03:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|