Split Common from Messages

This commit is contained in:
Jonathan Bourdon
2024-07-01 23:38:06 -04:00
parent adb1cebc73
commit 82699dae41
4 changed files with 3 additions and 4 deletions

View File

@@ -5,9 +5,6 @@ using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Messages.Handlers;
public record GetMessagesByUserRequest(
[FromRoute] Guid UserId);
public class GetMessagesByUser(
MessagingDbContext context)
: EndpointWithoutRequest<List<Message>>

View File

@@ -1,4 +1,5 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Messages.Data;
namespace Hutopy.Web.Messages.Handlers;

View File

@@ -1,4 +1,5 @@
using FastEndpoints;
using Hutopy.Web.Common;
using Hutopy.Web.Messages.Data;
namespace Hutopy.Web.Messages.Handlers;

View File

@@ -1,42 +0,0 @@
using System.Security.Claims;
namespace Hutopy.Web.Messages;
public class Shared(string claimName) : Exception;
public static class ClaimsPrincipalExtensions
{
public static Guid GetUserId(this ClaimsPrincipal claims)
{
return (Guid)claims.GetFirstValue<Guid>(ClaimTypes.NameIdentifier);
}
public static string GetFirstName(this ClaimsPrincipal claims)
{
return (string)claims.GetFirstValue<string>(ClaimTypes.GivenName);
}
public static string GetLastName(this ClaimsPrincipal claims)
{
return (string)claims.GetFirstValue<string>(ClaimTypes.Surname);
}
public static string GetEmail(this ClaimsPrincipal claims)
{
return (string)claims.GetFirstValue<string>(ClaimTypes.Email);
}
public static object GetFirstValue<TValue>(this ClaimsPrincipal claims, string key)
{
var claim = claims.FindFirst(key);
if (claim is null) throw new Shared(key);
if (typeof(TValue) == typeof(Guid))
{
return Guid.Parse(claim.Value);
}
return Convert.ChangeType(claim.Value, typeof(TValue));
}
}