User profile service #2
4 changed files with 9 additions and 8 deletions
|
@ -1,4 +1,4 @@
|
|||
namespace DrinkRateAPI.Requests;
|
||||
namespace DrinkRateAPI.ApiModels.UserProfile;
|
||||
|
||||
public class ChangeUserAdminStatusRequest
|
||||
{
|
|
@ -1,11 +1,11 @@
|
|||
using DrinkRateAPI.Requests;
|
||||
using DrinkRateAPI.ApiModels.UserProfile;
|
||||
using DrinkRateAPI.Services;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DrinkRateAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[Route("admin")]
|
||||
public class AdminController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<AdminController> _logger;
|
||||
|
@ -21,7 +21,7 @@ public class AdminController : ControllerBase
|
|||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("[action]")]
|
||||
[Route("adminStatus")]
|
||||
[Produces("application/json")]
|
||||
public async Task<IActionResult> PutUserAdminStatus([FromBody] ChangeUserAdminStatusRequest request)
|
||||
{
|
||||
|
@ -31,7 +31,7 @@ public class AdminController : ControllerBase
|
|||
return Unauthorized();
|
||||
}
|
||||
|
||||
var changedProfile = _userProfileService.ChangeUserAdminStatus(request.UserId, request.ChangeStatusTo);
|
||||
var changedProfile = await _userProfileService.ChangeUserAdminStatusAsync(request.UserId, request.ChangeStatusTo);
|
||||
|
||||
return Ok(changedProfile);
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="ApiModels\" />
|
||||
<Folder Include="Migrations\" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
@ -15,12 +15,12 @@ public class UserProfileService(ApplicationDbContext context)
|
|||
return userProfile.IsAdmin;
|
||||
}
|
||||
|
||||
public DbUserProfile ChangeUserAdminStatus(string userId, bool changeStatusTo)
|
||||
public async Task<DbUserProfile> ChangeUserAdminStatusAsync(string userId, bool changeStatusTo)
|
||||
{
|
||||
var userProfile = GetUserProfileById(userId);
|
||||
userProfile.IsAdmin = changeStatusTo;
|
||||
_context.UserProfiles.Update(userProfile);
|
||||
_context.SaveChanges();
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return userProfile;
|
||||
}
|
||||
|
@ -29,6 +29,6 @@ public class UserProfileService(ApplicationDbContext context)
|
|||
{
|
||||
var userProfile = _context.UserProfiles.FirstOrDefault(x => x.Id.ToString() == userId);
|
||||
|
||||
return userProfile ?? throw new KeyNotFoundException($"User with ID {userId} not found");
|
||||
return userProfile ?? throw new NotFoundException();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue