Compare commits
3 commits
b59fef222f
...
a246764e44
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a246764e44 | ||
![]() |
3230a5ed0f | ||
![]() |
dad144a80f |
5 changed files with 68 additions and 32 deletions
6
DrinkRateAPI/ApiModels/UserProfile/UserProfileGet.cs
Normal file
6
DrinkRateAPI/ApiModels/UserProfile/UserProfileGet.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace DrinkRateAPI.ApiModels.UserProfile;
|
||||
|
||||
public class UserProfileGet
|
||||
{
|
||||
|
||||
}
|
6
DrinkRateAPI/ApiModels/UserProfile/UserProfilePut.cs
Normal file
6
DrinkRateAPI/ApiModels/UserProfile/UserProfilePut.cs
Normal file
|
@ -0,0 +1,6 @@
|
|||
namespace DrinkRateAPI.ApiModels.UserProfile;
|
||||
|
||||
public class UserProfilePut
|
||||
{
|
||||
public string UserName { get; set; }
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
39
DrinkRateAPI/Controllers/UserProfileController.cs
Normal file
39
DrinkRateAPI/Controllers/UserProfileController.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue