BlazorStart/Start/Server/Controllers/BookmarksController.cs

69 lines
2 KiB
C#
Raw Normal View History

using System.Threading.Tasks;
2021-11-13 03:21:59 +00:00
using Microsoft.AspNetCore.Authorization;
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]
[Route("[controller]")]
2021-11-13 03:21:59 +00:00
public class BookmarksController : ControllerBase {
private readonly IBookmarkService bookmarkService;
2021-12-05 23:50:48 +00:00
public BookmarksController(IBookmarkService bookmarkService) {
2021-11-13 03:21:59 +00:00
this.bookmarkService = bookmarkService;
}
2021-11-14 01:19:28 +00:00
[HttpGet]
[Route("{bookmarkId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetBookmark(int bookmarkId) {
BookmarkDto? bookmark = (await this.bookmarkService
.GetBookmark(this.GetAuthorizedUserId(), bookmarkId))
2021-11-16 06:27:28 +00:00
?.MapToDto();
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]
[Route("Create")]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateBookmark(string title, string url, string? notes,
2022-04-19 20:04:38 +00:00
int sortOrder, int bookmarkGroupId) {
BookmarkDto? bookmark = (await this.bookmarkService
2022-04-19 20:04:38 +00:00
.CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, sortOrder,
bookmarkGroupId))
2021-11-16 06:27:28 +00:00
?.MapToDto();
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
}
[HttpDelete]
[Route("Delete/{bookmarkId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeleteBookmark(int bookmarkId) {
var res = await this.bookmarkService
.DeleteBookmark(this.GetAuthorizedUserId(), bookmarkId);
if (!res)
return NotFound();
return Ok();
}
2021-11-13 03:21:59 +00:00
}
}