From 25fb38baec9c23a210b2bb4949ce6a47b2f64c87 Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Mon, 22 Nov 2021 12:52:41 -0800 Subject: [PATCH] Add support for deleting bookmark containers --- Start/Client/Pages/StartPage.razor | 31 +++++++- .../Server/Controllers/BookmarksController.cs | 74 ++++++++++++++++--- 2 files changed, 90 insertions(+), 15 deletions(-) diff --git a/Start/Client/Pages/StartPage.razor b/Start/Client/Pages/StartPage.razor index 0c2f4b8..158c998 100644 --- a/Start/Client/Pages/StartPage.razor +++ b/Start/Client/Pages/StartPage.razor @@ -28,8 +28,10 @@ else
  • @container.Title + -
  • }
  • @@ -114,6 +116,9 @@ else { try { + if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId == bookmarkContainerId) ?? false) + bookmarkContainerId = this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId; + BookmarkContainerDto? bookmarkContainer = await Http .GetFromJsonAsync( $"Bookmarks/GetBookmarkContainer/{bookmarkContainerId}"); @@ -127,9 +132,29 @@ else } } - protected async Task OnDeleteContainerClicked() + protected async Task OnDeleteContainerClicked(int bookmarkContainerId) { + try + { + HttpResponseMessage result = await Http + .DeleteAsync($"Bookmarks/DeleteBookmarkContainer/{bookmarkContainerId}"); + if (result.StatusCode == System.Net.HttpStatusCode.OK) + { + if (await this.GetSelectedContainerId() == bookmarkContainerId) + await this.OnContainerSelected( + this.bookmarkContainers?.First().BookmarkContainerId + ?? bookmarkContainerId); + + this.bookmarkContainers = this.bookmarkContainers + ?.Where(bc => bc.BookmarkContainerId != bookmarkContainerId) + .ToList(); + } + } + catch (AccessTokenNotAvailableException e) + { + e.Redirect(); + } } protected void OnCreateContainerClicked() @@ -149,7 +174,7 @@ else this.bookmarkContainers.Add(newContainer); this.showCreateContainerForm = false; - await SetSelectedContainer(newContainer.BookmarkContainerId); + await OnContainerSelected(newContainer.BookmarkContainerId); } // Save the currently selected container in LocalStorage so that the same container remains diff --git a/Start/Server/Controllers/BookmarksController.cs b/Start/Server/Controllers/BookmarksController.cs index bc6aff5..4b10373 100644 --- a/Start/Server/Controllers/BookmarksController.cs +++ b/Start/Server/Controllers/BookmarksController.cs @@ -1,6 +1,7 @@ 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; @@ -23,47 +24,96 @@ namespace Start.Server.Controllers { } [HttpGet] - public IList GetAllBookmarkContainers() { - return this.bookmarkContainerService.GetUserBookmarkContainers(this.GetAuthorizedUserId()) + [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}")] - public BookmarkContainerDto? GetBookmarkContainer(int bookmarkContainerId) { - return this.bookmarkContainerService + [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}")] - public BookmarkDto? GetBookmark(int bookmarkId) { - return this.bookmarkService + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public IActionResult GetBookmark(int bookmarkId) { + BookmarkDto? bookmark = this.bookmarkService .GetBookmark(this.GetAuthorizedUserId(), bookmarkId) ?.MapToDto(); + + if (bookmark == null) + return NotFound(); + + return Ok(bookmark); } [HttpPost] - public BookmarkDto? CreateBookmark(string title, string url, string? notes, + [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkDto))] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public IActionResult CreateBookmark(string title, string url, string? notes, int bookmarkGroupId) { - return this.bookmarkService + BookmarkDto? bookmark = this.bookmarkService .CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId) ?.MapToDto(); + + if (bookmark == null) + return BadRequest(); + + return Created( + Url.Action(nameof(this.GetBookmark),new { bookmarkId = bookmark.BookmarkId }), + bookmark); } [HttpPost] - public BookmarkContainerDto? CreateBookmarkContainer([FromBody] string title) { - return this.bookmarkContainerService + [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] - public bool DeleteBookmarkContainer(int bookmarkContainerId) { - return this.bookmarkContainerService + [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(); } } }