Compare commits

...

3 commits

Author SHA1 Message Date
Martin Velebil
a246764e44 Movesadmin status update endpoint
Relocates the endpoint for updating user admin status from the dedicated AdminController to the UserProfileController.

This consolidates user profile management under a single controller
and leverages existing authorization policies.
2025-08-10 18:33:36 +02:00
Martin Velebil
3230a5ed0f Merge remote-tracking branch 'origin/250809_UserProfile' into 250810_UserProfileAdmin
# Conflicts:
#	DrinkRateAPI/Services/UserProfileService.cs
2025-08-10 18:18:06 +02:00
Jiří Vrabec
dad144a80f Create user profile service base 2025-08-10 16:35:36 +02:00
5 changed files with 68 additions and 32 deletions

View file

@ -0,0 +1,6 @@
namespace DrinkRateAPI.ApiModels.UserProfile;
public class UserProfileGet
{
}

View file

@ -0,0 +1,6 @@
namespace DrinkRateAPI.ApiModels.UserProfile;
public class UserProfilePut
{
public string UserName { get; set; }
}

View file

@ -1,30 +0,0 @@
using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers;
[ApiController]
[Route("admin")]
[Authorize(Policy = "AdminOnly")]
public class AdminController : ControllerBase
{
private readonly ILogger<AdminController> _logger;
private readonly UserProfileService _userProfileService;
public AdminController(ILogger<AdminController> logger, UserProfileService userProfileService)
{
_logger = logger;
_userProfileService = userProfileService;
}
[HttpPut("users/{userId}/adminStatus")]
[Produces("application/json")]
public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] ChangeAdminStatusBody body)
{
var changedProfile = await _userProfileService.ChangeUserAdminStatusAsync(userId, body.ChangeStatusTo);
return Ok(changedProfile);
}
}

View file

@ -0,0 +1,39 @@
using System.Security.Claims;
using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Controllers;
[ApiController]
[Route("userProfile")]
public class UserProfileController : ControllerBase
{
private readonly ILogger<UserProfileController> _logger;
private readonly UserProfileService _userProfileService;
public UserProfileController(ILogger<UserProfileController> logger, UserProfileService userProfileService)
{
_logger = logger;
_userProfileService = userProfileService;
}
[HttpPut(Name = "user_profile")]
public UserProfileGet PutUserProfile(UserProfilePut userProfile)
{
throw new ApplicationException();
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
return new();
}
[HttpPut("{userId}/adminStatus")]
[Authorize(Policy = "AdminOnly")]
[Produces("application/json")]
public async Task<IActionResult> PutUserAdminStatus(string userId, [FromBody] ChangeAdminStatusBody body)
{
var changedProfile = await _userProfileService.PutUserProfileAdminStatusAsync(userId, body.ChangeStatusTo);
return Ok(changedProfile);
}
}

View file

@ -1,21 +1,27 @@
using System.Security.Claims;
using DrinkRateAPI.ApiModels.UserProfile;
using DrinkRateAPI.Contexts;
using DrinkRateAPI.DbEntities;
using DrinkRateAPI.DbEntities;
using DrinkRateAPI.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DrinkRateAPI.Services;
public class UserProfileService(ApplicationDbContext context)
public class UserProfileService(ApplicationDbContext context, ApplicationUserService applicationUserService)
{
private ApplicationDbContext _context = context;
private ApplicationUserService _applicationUserService = applicationUserService;
public bool IsUserProfileAdmin(DbUserProfile userProfile)
{
return userProfile.IsAdmin;
}
public async Task<DbUserProfile> ChangeUserAdminStatusAsync(string userId, bool changeStatusTo)
public async Task<DbUserProfile> PutUserProfileAdminStatusAsync(string userId, bool changeStatusTo)
{
var userProfile = GetUserProfileById(userId);
userProfile.IsAdmin = changeStatusTo;
@ -24,6 +30,15 @@ public class UserProfileService(ApplicationDbContext context)
return userProfile;
}
public async Task<UserProfileGet> PutUserProfileAsync(UserProfilePut userProfile, ClaimsPrincipal identity)
{
var profile = _applicationUserService.UserProfileByApplicationUserAsync(identity);
return new();
}
public DbUserProfile GetUserProfileById(string userId)
{