2021-11-28 22:32:21 -08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-11-12 19:21:59 -08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-11-22 12:52:41 -08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2021-11-12 19:21:59 -08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Start.Server.Data.Services.Interfaces;
|
2021-11-13 19:27:01 -08:00
|
|
|
|
using Start.Server.Extensions;
|
2021-11-15 21:44:16 -08:00
|
|
|
|
using Start.Shared;
|
2021-11-12 19:21:59 -08:00
|
|
|
|
|
|
|
|
|
|
namespace Start.Server.Controllers {
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
[ApiController]
|
2021-11-23 22:57:46 -08:00
|
|
|
|
[Route("[controller]")]
|
2021-11-12 19:21:59 -08:00
|
|
|
|
public class BookmarksController : ControllerBase {
|
|
|
|
|
|
private readonly IBookmarkService bookmarkService;
|
|
|
|
|
|
|
2021-12-05 15:50:48 -08:00
|
|
|
|
public BookmarksController(IBookmarkService bookmarkService) {
|
2021-11-12 19:21:59 -08:00
|
|
|
|
this.bookmarkService = bookmarkService;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-13 17:19:28 -08:00
|
|
|
|
[HttpGet]
|
2021-11-20 22:47:10 -08:00
|
|
|
|
[Route("{bookmarkId}")]
|
2021-11-22 12:52:41 -08:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))]
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2021-11-28 22:32:21 -08:00
|
|
|
|
public async Task<IActionResult> GetBookmark(int bookmarkId) {
|
|
|
|
|
|
BookmarkDto? bookmark = (await this.bookmarkService
|
|
|
|
|
|
.GetBookmark(this.GetAuthorizedUserId(), bookmarkId))
|
2021-11-15 22:27:28 -08:00
|
|
|
|
?.MapToDto();
|
2021-11-22 12:52:41 -08:00
|
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok(bookmark);
|
2021-11-13 17:19:28 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-12 19:21:59 -08:00
|
|
|
|
[HttpPost]
|
2021-11-23 22:57:46 -08:00
|
|
|
|
[Route("Create")]
|
2021-11-22 12:52:41 -08:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkDto))]
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2021-11-28 22:32:21 -08:00
|
|
|
|
public async Task<IActionResult> CreateBookmark(string title, string url, string? notes,
|
2022-04-19 13:04:38 -07:00
|
|
|
|
int sortOrder, int bookmarkGroupId) {
|
2021-11-28 22:32:21 -08:00
|
|
|
|
BookmarkDto? bookmark = (await this.bookmarkService
|
2022-04-19 13:04:38 -07:00
|
|
|
|
.CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, sortOrder,
|
|
|
|
|
|
bookmarkGroupId))
|
2021-11-15 22:27:28 -08:00
|
|
|
|
?.MapToDto();
|
2021-11-22 12:52:41 -08:00
|
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
|
return BadRequest();
|
|
|
|
|
|
|
|
|
|
|
|
return Created(
|
2022-04-28 08:57:58 -07:00
|
|
|
|
Url.Action(nameof(this.GetBookmark),new { bookmarkId = bookmark.BookmarkId })!,
|
2021-11-22 12:52:41 -08:00
|
|
|
|
bookmark);
|
2021-11-15 21:44:16 -08:00
|
|
|
|
}
|
2021-11-23 22:57:46 -08:00
|
|
|
|
|
|
|
|
|
|
[HttpDelete]
|
|
|
|
|
|
[Route("Delete/{bookmarkId}")]
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2021-11-28 22:32:21 -08:00
|
|
|
|
public async Task<IActionResult> DeleteBookmark(int bookmarkId) {
|
|
|
|
|
|
var res = await this.bookmarkService
|
|
|
|
|
|
.DeleteBookmark(this.GetAuthorizedUserId(), bookmarkId);
|
2021-11-23 22:57:46 -08:00
|
|
|
|
|
|
|
|
|
|
if (!res)
|
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
|
}
|
2021-11-12 19:21:59 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|