Fill out basic BookmarksController
This commit is contained in:
parent
d18dea826e
commit
6ec00b7d06
|
@ -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<BookmarkContainer> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
28
Start/Server/Extensions/ControllerExtensions.cs
Normal file
28
Start/Server/Extensions/ControllerExtensions.cs
Normal file
|
@ -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 {
|
||||
/// <summary>
|
||||
/// Get the current user's ID (<see cref="ClaimTypes.NameIdentifier"/>) from claims. The
|
||||
/// caller is assumed to have checked that the user is logged in (and thus they have a user
|
||||
/// ID set).
|
||||
/// <para>If there is no user ID, an exception will be thrown.</para>
|
||||
/// </summary>
|
||||
/// <param name="controller"></param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore;
|
|||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Start.Server {
|
||||
namespace Start.Server.Extensions {
|
||||
/// <summary>Extension methods for LINQ queries</summary>
|
||||
public static class LinqExtensions {
|
||||
/// <summary>
|
|
@ -16,6 +16,7 @@
|
|||
<ItemGroup>
|
||||
<None Remove="Data\Services\" />
|
||||
<None Remove="Data\Services\Interfaces\" />
|
||||
<None Remove="Extensions\" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
|
||||
|
@ -31,5 +32,6 @@
|
|||
<ItemGroup>
|
||||
<Folder Include="Data\Services\" />
|
||||
<Folder Include="Data\Services\Interfaces\" />
|
||||
<Folder Include="Extensions\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue