User profile service #2
3 changed files with 24 additions and 34 deletions
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,13 +1,24 @@
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using DrinkRateAPI.ApiModels.UserProfile;
|
using DrinkRateAPI.ApiModels.UserProfile;
|
||||||
|
using DrinkRateAPI.Services;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Controllers;
|
namespace DrinkRateAPI.Controllers;
|
||||||
|
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("user_profile")]
|
[Route("userProfile")]
|
||||||
public class UserProfileController : ControllerBase
|
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")]
|
[HttpPut(Name = "user_profile")]
|
||||||
public UserProfileGet PutUserProfile(UserProfilePut userProfile)
|
public UserProfileGet PutUserProfile(UserProfilePut userProfile)
|
||||||
{
|
{
|
||||||
|
@ -15,4 +26,14 @@ public class UserProfileController : ControllerBase
|
||||||
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
|
var x = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; //HttpContext.User.Identities.First();
|
||||||
return new();
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -11,8 +11,7 @@ using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
namespace DrinkRateAPI.Services;
|
namespace DrinkRateAPI.Services;
|
||||||
|
|
||||||
public class UserProfileService(ApplicationDbContext context,
|
public class UserProfileService(ApplicationDbContext context, ApplicationUserService applicationUserService)
|
||||||
ApplicationUserService applicationUserService)
|
|
||||||
{
|
{
|
||||||
private ApplicationDbContext _context = context;
|
private ApplicationDbContext _context = context;
|
||||||
private ApplicationUserService _applicationUserService = applicationUserService;
|
private ApplicationUserService _applicationUserService = applicationUserService;
|
||||||
|
@ -22,7 +21,7 @@ public class UserProfileService(ApplicationDbContext context,
|
||||||
return userProfile.IsAdmin;
|
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);
|
var userProfile = GetUserProfileById(userId);
|
||||||
userProfile.IsAdmin = changeStatusTo;
|
userProfile.IsAdmin = changeStatusTo;
|
||||||
|
|
Loading…
Reference in a new issue