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">
<a @onclick="() => OnContainerSelected(container.BookmarkContainerId)">
@container.Title
<button class="btn btn-clear"
@onclick="() => this.OnDeleteContainerClicked(container.BookmarkContainerId)">
</button>
</a>
<button class="btn btn-clear"></button>
</li>
}
<li class="tab-item tab-action">
@ -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<BookmarkContainerDto?>(
$"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

View file

@ -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<BookmarkContainerDto> GetAllBookmarkContainers() {
return this.bookmarkContainerService.GetUserBookmarkContainers(this.GetAuthorizedUserId())
[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}")]
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();
}
}
}