drinkrate/DrinkRateAPI/Services/ApplicationUserService.cs
martinshoob 1dc37d3282 Handle not found user profile gracefully
Changes from throwing an exception when a user profile is not found to returning null.

This prevents the application from crashing when a user profile is not found, allowing for more graceful error handling.
2025-08-11 18:46:35 +02:00

21 lines
No EOL
706 B
C#

using System.Security.Claims;
using DrinkRateAPI.Contexts;
using DrinkRateAPI.DbEntities;
using DrinkRateAPI.Exceptions;
using Microsoft.EntityFrameworkCore;
namespace DrinkRateAPI.Services;
public class ApplicationUserService(ApplicationDbContext context)
{
private ApplicationDbContext _context = context;
public async Task<DbUserProfile> UserProfileByApplicationUserAsync(ClaimsPrincipal identity)
{
var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
var profile = await _context.UserProfiles
.FirstOrDefaultAsync(x => x.ApplicationUserId.ToString() == appUserId)
?? throw new NotFoundException();
return profile;
}
}