Fill out basic BookmarksController
This commit is contained in:
parent
d18dea826e
commit
6ec00b7d06
6 changed files with 50 additions and 18 deletions
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Start/Server/Extensions/LinqExtensions.cs
Normal file
47
Start/Server/Extensions/LinqExtensions.cs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace Start.Server.Extensions {
|
||||
/// <summary>Extension methods for LINQ queries</summary>
|
||||
public static class LinqExtensions {
|
||||
/// <summary>
|
||||
/// If <paramref name="condition"/> is true, then returns <c>query.Where(expression)</c>.
|
||||
/// Otherwise returns the <paramref name="query"/> unchanged.
|
||||
/// </summary>
|
||||
/// <param name="query">The query to potentially apply the expression to</param>
|
||||
/// <param name="condition">
|
||||
/// Determines whether or not the expression should be applied
|
||||
/// </param>
|
||||
/// <param name="expression">A function to test each element</param>
|
||||
public static IQueryable<T> IfWhere<T>(this IQueryable<T> query, bool condition,
|
||||
Expression<Func<T, bool>> expression) {
|
||||
if (condition)
|
||||
return query.Where(expression);
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the <paramref name="condition"/> is true, apply the <paramref name="transform"/> to
|
||||
/// the query, otherwise return the query unchanged.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The <paramref name="query"/>'s type</typeparam>
|
||||
/// <param name="query">The query to potentially transform</param>
|
||||
/// <param name="condition">
|
||||
/// If true, apply the <paramref name="transform"/> to the <paramref name="query"/>
|
||||
/// </param>
|
||||
/// <param name="transform">
|
||||
/// A function to apply to the <paramref name="query"/> if the <paramref name="condition"/>
|
||||
/// is true
|
||||
/// </param>
|
||||
public static IQueryable<T> If<T>(this IQueryable<T> query, bool condition,
|
||||
Func<IQueryable<T>, IQueryable<T>> transform) {
|
||||
if (condition)
|
||||
return transform(query);
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue