From 6ec00b7d06bb669d8b09e0127d177446fcb34ebe Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Sat, 13 Nov 2021 19:27:01 -0800 Subject: [PATCH] Fill out basic BookmarksController --- .../Server/Controllers/BookmarksController.cs | 34 +++++++++---------- .../Data/Services/BookmarkContainerService.cs | 1 + .../Data/Services/BookmarkGroupService.cs | 1 + .../Server/Extensions/ControllerExtensions.cs | 28 +++++++++++++++ .../Server/{ => Extensions}/LinqExtensions.cs | 2 +- Start/Server/Start.Server.csproj | 2 ++ 6 files changed, 50 insertions(+), 18 deletions(-) create mode 100644 Start/Server/Extensions/ControllerExtensions.cs rename Start/Server/{ => Extensions}/LinqExtensions.cs (97%) diff --git a/Start/Server/Controllers/BookmarksController.cs b/Start/Server/Controllers/BookmarksController.cs index c9f4cd0..80398fa 100644 --- a/Start/Server/Controllers/BookmarksController.cs +++ b/Start/Server/Controllers/BookmarksController.cs @@ -1,10 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Security.Claims; +using System.Collections.Generic; using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Start.Server.Data.Services.Interfaces; +using Start.Server.Extensions; using Start.Server.Models; namespace Start.Server.Controllers { @@ -12,38 +10,40 @@ namespace Start.Server.Controllers { [ApiController] [Route("[controller]")] public class BookmarksController : ControllerBase { + private readonly IBookmarkContainerService bookmarkContainerService; + private readonly IBookmarkGroupService bookmarkGroupService; private readonly IBookmarkService bookmarkService; - public BookmarksController(IBookmarkService bookmarkService) { + private readonly string userId; + + public BookmarksController(IBookmarkContainerService bookmarkContainerService, + IBookmarkGroupService bookmarkGroupService, IBookmarkService bookmarkService) { + this.bookmarkContainerService = bookmarkContainerService; + this.bookmarkGroupService = bookmarkGroupService; this.bookmarkService = bookmarkService; + + this.userId = this.GetAuthorizedUserId(); } - /* [HttpGet] public IList GetAllBookmarkContainers() { - + return this.bookmarkContainerService.GetUserBookmarkContainers(this.userId); } [HttpGet] - public BookmarkContainer GetBookmarkContainer(int bookmarkContainerId) { - + public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(int bookmarkContainerId) { + return this.bookmarkContainerService.GetBookmarkContainer(this.userId, bookmarkContainerId, true, true); } - */ [HttpGet] public (BookmarkStatus, Bookmark?) GetBookmark(int bookmarkId) { - return this.bookmarkService.GetBookmark(this.GetUserId(), bookmarkId); + return this.bookmarkService.GetBookmark(this.userId, bookmarkId); } [HttpPost] public (BookmarkStatus, Bookmark?) CreateBookmark(string title, string url, string? notes, int bookmarkGroupId) { - return this.bookmarkService.CreateBookmark(this.GetUserId(), title, url, notes, - bookmarkGroupId); - } - - private string GetUserId() { - return this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value; + return this.bookmarkService.CreateBookmark(this.userId, title, url, notes, bookmarkGroupId); } } } diff --git a/Start/Server/Data/Services/BookmarkContainerService.cs b/Start/Server/Data/Services/BookmarkContainerService.cs index 42aa78b..95c993d 100644 --- a/Start/Server/Data/Services/BookmarkContainerService.cs +++ b/Start/Server/Data/Services/BookmarkContainerService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Start.Server.Data.Services.Interfaces; +using Start.Server.Extensions; using Start.Server.Models; namespace Start.Server.Data.Services { diff --git a/Start/Server/Data/Services/BookmarkGroupService.cs b/Start/Server/Data/Services/BookmarkGroupService.cs index 0929e10..4951ff2 100644 --- a/Start/Server/Data/Services/BookmarkGroupService.cs +++ b/Start/Server/Data/Services/BookmarkGroupService.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; using Start.Server.Data.Services.Interfaces; +using Start.Server.Extensions; using Start.Server.Models; namespace Start.Server.Data.Services { diff --git a/Start/Server/Extensions/ControllerExtensions.cs b/Start/Server/Extensions/ControllerExtensions.cs new file mode 100644 index 0000000..6cb6bf9 --- /dev/null +++ b/Start/Server/Extensions/ControllerExtensions.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Security.Claims; +using Microsoft.AspNetCore.Mvc; + +namespace Start.Server.Extensions { + public static class ControllerExtensions { + /// + /// Get the current user's ID () from claims. The + /// caller is assumed to have checked that the user is logged in (and thus they have a user + /// ID set). + /// If there is no user ID, an exception will be thrown. + /// + /// + public static string GetAuthorizedUserId(this ControllerBase controller) { + string? res = controller.GetUserId(); + + if (res == null) + throw new KeyNotFoundException("The user ID could not be retrieved from claims"); + + return res; + } + + public static string? GetUserId(this ControllerBase controller) { + return controller.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; + } + } +} diff --git a/Start/Server/LinqExtensions.cs b/Start/Server/Extensions/LinqExtensions.cs similarity index 97% rename from Start/Server/LinqExtensions.cs rename to Start/Server/Extensions/LinqExtensions.cs index cc46a51..eb06e70 100644 --- a/Start/Server/LinqExtensions.cs +++ b/Start/Server/Extensions/LinqExtensions.cs @@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore; using System.Linq; using System.Linq.Expressions; -namespace Start.Server { +namespace Start.Server.Extensions { /// Extension methods for LINQ queries public static class LinqExtensions { /// diff --git a/Start/Server/Start.Server.csproj b/Start/Server/Start.Server.csproj index dcc51c2..2b772f5 100644 --- a/Start/Server/Start.Server.csproj +++ b/Start/Server/Start.Server.csproj @@ -16,6 +16,7 @@ + @@ -31,5 +32,6 @@ +