diff --git a/DrinkRateAPI/Exceptions/Exceptions.cs b/DrinkRateAPI/Exceptions/Exceptions.cs new file mode 100644 index 0000000..14c8432 --- /dev/null +++ b/DrinkRateAPI/Exceptions/Exceptions.cs @@ -0,0 +1,7 @@ +namespace DrinkRateAPI.Exceptions; + +public class DrinkRateException : Exception; + +public class NotFoundException : DrinkRateException; + +public class UnauthorizedException : DrinkRateException; \ No newline at end of file diff --git a/DrinkRateAPI/Services/ApplicationUserService.cs b/DrinkRateAPI/Services/ApplicationUserService.cs new file mode 100644 index 0000000..a2f735d --- /dev/null +++ b/DrinkRateAPI/Services/ApplicationUserService.cs @@ -0,0 +1,21 @@ +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 UserProfileByApplicationUserAsync(ClaimsPrincipal identity) + { + var appUserId = identity.FindFirst(ClaimTypes.NameIdentifier)?.Value; + var profile = await _context.UserProfiles + .FirstAsync(x => x.ApplicationUserId.ToString() == appUserId) + ?? throw new NotFoundException(); + + return profile; + } +} \ No newline at end of file