From 4623d8838d5978a33a71fa94c0ad337ad764ce2c Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Tue, 23 Nov 2021 21:55:04 -0800 Subject: [PATCH] Move bookmark container API endpoints to their own controller --- Start/Client/Components/CreateContainer.razor | 2 +- Start/Client/Components/DeleteContainer.razor | 2 +- Start/Client/Pages/StartPage.razor | 12 +-- .../BookmarkContainersController.cs | 84 +++++++++++++++++++ .../Server/Controllers/BookmarksController.cs | 67 +-------------- 5 files changed, 95 insertions(+), 72 deletions(-) create mode 100644 Start/Server/Controllers/BookmarkContainersController.cs diff --git a/Start/Client/Components/CreateContainer.razor b/Start/Client/Components/CreateContainer.razor index 27eb26e..1d8bc50 100644 --- a/Start/Client/Components/CreateContainer.razor +++ b/Start/Client/Components/CreateContainer.razor @@ -51,7 +51,7 @@ protected async void OnSubmit() { HttpResponseMessage response = await Http - .PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", model.Title); + .PostAsJsonAsync("BookmarkContainers/Create", model.Title); Stream stream = response.RequestMessage!.Content!.ReadAsStream(); StreamReader reader = new StreamReader(stream); diff --git a/Start/Client/Components/DeleteContainer.razor b/Start/Client/Components/DeleteContainer.razor index 65deace..9807f2d 100644 --- a/Start/Client/Components/DeleteContainer.razor +++ b/Start/Client/Components/DeleteContainer.razor @@ -42,7 +42,7 @@ try { HttpResponseMessage result = await Http - .DeleteAsync($"Bookmarks/DeleteBookmarkContainer/{this.BookmarkContainerId}"); + .DeleteAsync($"BookmarkContainers/Delete/{this.BookmarkContainerId}"); if (result.StatusCode == System.Net.HttpStatusCode.OK) { diff --git a/Start/Client/Pages/StartPage.razor b/Start/Client/Pages/StartPage.razor index fc6fa1f..7a2196b 100644 --- a/Start/Client/Pages/StartPage.razor +++ b/Start/Client/Pages/StartPage.razor @@ -99,10 +99,11 @@ else protected async Task LoadContainers() { - try { + try + { this.bookmarkContainers = await Http .GetFromJsonAsync>( - "Bookmarks/GetAllBookmarkContainers"); + "BookmarkContainers"); if (this.bookmarkContainers == null || !this.bookmarkContainers.Any()) { @@ -111,7 +112,8 @@ else await this.OnContainerSelected(await this.GetSelectedContainerId()); } - catch (AccessTokenNotAvailableException e) { + catch (AccessTokenNotAvailableException e) + { e.Redirect(); } } @@ -119,7 +121,7 @@ else protected async Task CreateDefaultContainer() { HttpResponseMessage response = await Http - .PostAsJsonAsync("Bookmarks/CreateBookmarkContainer", "Default"); + .PostAsJsonAsync("BookmarkContainers/Create", "Default"); BookmarkContainerDto? container = await response .RequestMessage @@ -139,7 +141,7 @@ else BookmarkContainerDto? bookmarkContainer = await Http .GetFromJsonAsync( - $"Bookmarks/GetBookmarkContainer/{bookmarkContainerId}"); + $"BookmarkContainers/{bookmarkContainerId}"); await this.SetSelectedContainer(bookmarkContainerId); this.selectedBookmarkContainer = bookmarkContainer; diff --git a/Start/Server/Controllers/BookmarkContainersController.cs b/Start/Server/Controllers/BookmarkContainersController.cs new file mode 100644 index 0000000..ae108a6 --- /dev/null +++ b/Start/Server/Controllers/BookmarkContainersController.cs @@ -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))] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public IActionResult GetAllBookmarkContainers() { + List? 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(); + } + } +} diff --git a/Start/Server/Controllers/BookmarksController.cs b/Start/Server/Controllers/BookmarksController.cs index 4b10373..a11b2dc 100644 --- a/Start/Server/Controllers/BookmarksController.cs +++ b/Start/Server/Controllers/BookmarksController.cs @@ -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))] - [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult GetAllBookmarkContainers() { - List? 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(); - } } }