Add support for deleting bookmark containers

This commit is contained in:
Neil Brommer 2021-11-22 12:52:41 -08:00
parent 3eb2b2ae98
commit 25fb38baec
2 changed files with 90 additions and 15 deletions

View file

@ -28,8 +28,10 @@ else
<li class="@itemClasses"> <li class="@itemClasses">
<a @onclick="() => OnContainerSelected(container.BookmarkContainerId)"> <a @onclick="() => OnContainerSelected(container.BookmarkContainerId)">
@container.Title @container.Title
<button class="btn btn-clear"
@onclick="() => this.OnDeleteContainerClicked(container.BookmarkContainerId)">
</button>
</a> </a>
<button class="btn btn-clear"></button>
</li> </li>
} }
<li class="tab-item tab-action"> <li class="tab-item tab-action">
@ -114,6 +116,9 @@ else
{ {
try try
{ {
if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId == bookmarkContainerId) ?? false)
bookmarkContainerId = this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId;
BookmarkContainerDto? bookmarkContainer = await Http BookmarkContainerDto? bookmarkContainer = await Http
.GetFromJsonAsync<BookmarkContainerDto?>( .GetFromJsonAsync<BookmarkContainerDto?>(
$"Bookmarks/GetBookmarkContainer/{bookmarkContainerId}"); $"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() protected void OnCreateContainerClicked()
@ -149,7 +174,7 @@ else
this.bookmarkContainers.Add(newContainer); this.bookmarkContainers.Add(newContainer);
this.showCreateContainerForm = false; this.showCreateContainerForm = false;
await SetSelectedContainer(newContainer.BookmarkContainerId); await OnContainerSelected(newContainer.BookmarkContainerId);
} }
// Save the currently selected container in LocalStorage so that the same container remains // Save the currently selected container in LocalStorage so that the same container remains

View file

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Start.Server.Data.Services.Interfaces; using Start.Server.Data.Services.Interfaces;
using Start.Server.Extensions; using Start.Server.Extensions;
@ -23,47 +24,96 @@ namespace Start.Server.Controllers {
} }
[HttpGet] [HttpGet]
public IList<BookmarkContainerDto> GetAllBookmarkContainers() { [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable<BookmarkContainerDto>))]
return this.bookmarkContainerService.GetUserBookmarkContainers(this.GetAuthorizedUserId()) [ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetAllBookmarkContainers() {
List<BookmarkContainerDto>? containers = this.bookmarkContainerService
.GetUserBookmarkContainers(this.GetAuthorizedUserId())
.Select(bc => bc.MapToDto()) .Select(bc => bc.MapToDto())
.ToList(); .ToList();
if (containers == null)
return NotFound();
return Ok(containers);
} }
[HttpGet] [HttpGet]
[Route("{bookmarkContainerId}")] [Route("{bookmarkContainerId}")]
public BookmarkContainerDto? GetBookmarkContainer(int bookmarkContainerId) { [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkContainerDto))]
return this.bookmarkContainerService [ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetBookmarkContainer(int bookmarkContainerId) {
BookmarkContainerDto? container = this.bookmarkContainerService
.GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true) .GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true)
?.MapToDto(); ?.MapToDto();
if (container == null)
return NotFound();
return Ok(container);
} }
[HttpGet] [HttpGet]
[Route("{bookmarkId}")] [Route("{bookmarkId}")]
public BookmarkDto? GetBookmark(int bookmarkId) { [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))]
return this.bookmarkService [ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetBookmark(int bookmarkId) {
BookmarkDto? bookmark = this.bookmarkService
.GetBookmark(this.GetAuthorizedUserId(), bookmarkId) .GetBookmark(this.GetAuthorizedUserId(), bookmarkId)
?.MapToDto(); ?.MapToDto();
if (bookmark == null)
return NotFound();
return Ok(bookmark);
} }
[HttpPost] [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) { int bookmarkGroupId) {
return this.bookmarkService BookmarkDto? bookmark = this.bookmarkService
.CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId) .CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId)
?.MapToDto(); ?.MapToDto();
if (bookmark == null)
return BadRequest();
return Created(
Url.Action(nameof(this.GetBookmark),new { bookmarkId = bookmark.BookmarkId }),
bookmark);
} }
[HttpPost] [HttpPost]
public BookmarkContainerDto? CreateBookmarkContainer([FromBody] string title) { [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkContainerDto))]
return this.bookmarkContainerService [ProducesResponseType(StatusCodes.Status400BadRequest)]
public IActionResult CreateBookmarkContainer([FromBody] string title) {
BookmarkContainerDto? container = this.bookmarkContainerService
.CreateBookmarkContainer(this.GetAuthorizedUserId(), title) .CreateBookmarkContainer(this.GetAuthorizedUserId(), title)
?.MapToDto(); ?.MapToDto();
if (container == null)
return BadRequest();
return Created(
Url.Action(nameof(this.GetBookmarkContainer),
new { bookmarkContainerId = container.BookmarkContainerId }),
container);
} }
[HttpDelete] [HttpDelete]
public bool DeleteBookmarkContainer(int bookmarkContainerId) { [Route("{bookmarkContainerId}")]
return this.bookmarkContainerService [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult DeleteBookmarkContainer(int bookmarkContainerId) {
bool res = this.bookmarkContainerService
.DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId); .DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId);
if (!res)
return NotFound();
return Ok();
} }
} }
} }