Move bookmark container API endpoints to their own controller

This commit is contained in:
Neil Brommer 2021-11-23 21:55:04 -08:00
parent b8b23abffc
commit 4623d8838d
5 changed files with 95 additions and 72 deletions

View file

@ -0,0 +1,84 @@
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Start.Server.Data.Services.Interfaces;
using Start.Server.Extensions;
using Start.Shared;
namespace Start.Server.Controllers {
[Authorize]
[ApiController]
[Route("[controller]")]
public class BookmarkContainersController : ControllerBase {
private readonly IBookmarkContainerService bookmarkContainerService;
public BookmarkContainersController(IBookmarkContainerService bookmarkContainerService) {
this.bookmarkContainerService = bookmarkContainerService;
}
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK,
Type = typeof(IEnumerable<BookmarkContainerDto>))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetAllBookmarkContainers() {
List<BookmarkContainerDto>? containers = this.bookmarkContainerService
.GetUserBookmarkContainers(this.GetAuthorizedUserId())
.Select(bc => bc.MapToDto())
.ToList();
if (containers == null)
return NotFound();
return Ok(containers);
}
[HttpGet]
[Route("{bookmarkContainerId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkContainerDto))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetBookmarkContainer(int bookmarkContainerId) {
BookmarkContainerDto? container = this.bookmarkContainerService
.GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true)
?.MapToDto();
if (container == null)
return NotFound();
return Ok(container);
}
[HttpPost]
[Route("Create")]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkContainerDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult CreateBookmarkContainer([FromBody] string title) {
BookmarkContainerDto? container = this.bookmarkContainerService
.CreateBookmarkContainer(this.GetAuthorizedUserId(), title)
?.MapToDto();
if (container == null)
return BadRequest();
return Created(
Url.Action(nameof(this.GetBookmarkContainer),
new { bookmarkContainerId = container.BookmarkContainerId }),
container);
}
[HttpDelete]
[Route("Delete/{bookmarkContainerId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult DeleteBookmarkContainer(int bookmarkContainerId) {
bool res = this.bookmarkContainerService
.DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId);
if (!res)
return NotFound();
return Ok();
}
}
}

View file

@ -12,47 +12,15 @@ namespace Start.Server.Controllers {
[ApiController]
[Route("[controller]/[action]")]
public class BookmarksController : ControllerBase {
private readonly IBookmarkContainerService bookmarkContainerService;
private readonly IBookmarkGroupService bookmarkGroupService;
private readonly IBookmarkService bookmarkService;
public BookmarksController(IBookmarkContainerService bookmarkContainerService,
IBookmarkGroupService bookmarkGroupService, IBookmarkService bookmarkService) {
this.bookmarkContainerService = bookmarkContainerService;
public BookmarksController(IBookmarkGroupService bookmarkGroupService,
IBookmarkService bookmarkService) {
this.bookmarkGroupService = bookmarkGroupService;
this.bookmarkService = bookmarkService;
}
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<BookmarkContainerDto>))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetAllBookmarkContainers() {
List<BookmarkContainerDto>? containers = this.bookmarkContainerService
.GetUserBookmarkContainers(this.GetAuthorizedUserId())
.Select(bc => bc.MapToDto())
.ToList();
if (containers == null)
return NotFound();
return Ok(containers);
}
[HttpGet]
[Route("{bookmarkContainerId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkContainerDto))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetBookmarkContainer(int bookmarkContainerId) {
BookmarkContainerDto? container = this.bookmarkContainerService
.GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true)
?.MapToDto();
if (container == null)
return NotFound();
return Ok(container);
}
[HttpGet]
[Route("{bookmarkId}")]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))]
@ -84,36 +52,5 @@ namespace Start.Server.Controllers {
Url.Action(nameof(this.GetBookmark),new { bookmarkId = bookmark.BookmarkId }),
bookmark);
}
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkContainerDto))]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult CreateBookmarkContainer([FromBody] string title) {
BookmarkContainerDto? container = this.bookmarkContainerService
.CreateBookmarkContainer(this.GetAuthorizedUserId(), title)
?.MapToDto();
if (container == null)
return BadRequest();
return Created(
Url.Action(nameof(this.GetBookmarkContainer),
new { bookmarkContainerId = container.BookmarkContainerId }),
container);
}
[HttpDelete]
[Route("{bookmarkContainerId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult DeleteBookmarkContainer(int bookmarkContainerId) {
bool res = this.bookmarkContainerService
.DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId);
if (!res)
return NotFound();
return Ok();
}
}
}