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.Collections.Generic;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Identity;
|
|
||||||
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.Models;
|
using Start.Server.Models;
|
||||||
|
|
||||||
namespace Start.Server.Controllers {
|
namespace Start.Server.Controllers {
|
||||||
|
@ -12,38 +10,40 @@ namespace Start.Server.Controllers {
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class BookmarksController : ControllerBase {
|
public class BookmarksController : ControllerBase {
|
||||||
|
private readonly IBookmarkContainerService bookmarkContainerService;
|
||||||
|
private readonly IBookmarkGroupService bookmarkGroupService;
|
||||||
private readonly IBookmarkService bookmarkService;
|
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.bookmarkService = bookmarkService;
|
||||||
|
|
||||||
|
this.userId = this.GetAuthorizedUserId();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public IList<BookmarkContainer> GetAllBookmarkContainers() {
|
public IList<BookmarkContainer> GetAllBookmarkContainers() {
|
||||||
|
return this.bookmarkContainerService.GetUserBookmarkContainers(this.userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public BookmarkContainer GetBookmarkContainer(int bookmarkContainerId) {
|
public (BookmarkStatus, BookmarkContainer?) GetBookmarkContainer(int bookmarkContainerId) {
|
||||||
|
return this.bookmarkContainerService.GetBookmarkContainer(this.userId, bookmarkContainerId, true, true);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public (BookmarkStatus, Bookmark?) GetBookmark(int bookmarkId) {
|
public (BookmarkStatus, Bookmark?) GetBookmark(int bookmarkId) {
|
||||||
return this.bookmarkService.GetBookmark(this.GetUserId(), bookmarkId);
|
return this.bookmarkService.GetBookmark(this.userId, bookmarkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public (BookmarkStatus, Bookmark?) CreateBookmark(string title, string url, string? notes,
|
public (BookmarkStatus, Bookmark?) CreateBookmark(string title, string url, string? notes,
|
||||||
int bookmarkGroupId) {
|
int bookmarkGroupId) {
|
||||||
return this.bookmarkService.CreateBookmark(this.GetUserId(), title, url, notes,
|
return this.bookmarkService.CreateBookmark(this.userId, title, url, notes, bookmarkGroupId);
|
||||||
bookmarkGroupId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetUserId() {
|
|
||||||
return this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Start.Server.Data.Services.Interfaces;
|
using Start.Server.Data.Services.Interfaces;
|
||||||
|
using Start.Server.Extensions;
|
||||||
using Start.Server.Models;
|
using Start.Server.Models;
|
||||||
|
|
||||||
namespace Start.Server.Data.Services {
|
namespace Start.Server.Data.Services {
|
||||||
|
|
|
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Start.Server.Data.Services.Interfaces;
|
using Start.Server.Data.Services.Interfaces;
|
||||||
|
using Start.Server.Extensions;
|
||||||
using Start.Server.Models;
|
using Start.Server.Models;
|
||||||
|
|
||||||
namespace Start.Server.Data.Services {
|
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;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
|
|
||||||
namespace Start.Server {
|
namespace Start.Server.Extensions {
|
||||||
/// <summary>Extension methods for LINQ queries</summary>
|
/// <summary>Extension methods for LINQ queries</summary>
|
||||||
public static class LinqExtensions {
|
public static class LinqExtensions {
|
||||||
/// <summary>
|
/// <summary>
|
|
@ -16,6 +16,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Remove="Data\Services\" />
|
<None Remove="Data\Services\" />
|
||||||
<None Remove="Data\Services\Interfaces\" />
|
<None Remove="Data\Services\Interfaces\" />
|
||||||
|
<None Remove="Extensions\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
|
<None Update="app.db" CopyToOutputDirectory="PreserveNewest" ExcludeFromSingleFile="true" />
|
||||||
|
@ -31,5 +32,6 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Data\Services\" />
|
<Folder Include="Data\Services\" />
|
||||||
<Folder Include="Data\Services\Interfaces\" />
|
<Folder Include="Data\Services\Interfaces\" />
|
||||||
|
<Folder Include="Extensions\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|
Loading…
Reference in a new issue