User profile service #2

Merged
Jiri merged 16 commits from 250809_UserProfile into main 2025-08-11 20:10:50 +00:00
4 changed files with 9 additions and 8 deletions
Showing only changes of commit b2b8d1e076 - Show all commits

View file

@ -1,4 +1,4 @@
namespace DrinkRateAPI.Requests;
namespace DrinkRateAPI.ApiModels.UserProfile;
public class ChangeUserAdminStatusRequest
{

View file

@ -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);
}

View file

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="ApiModels\" />
<Folder Include="Migrations\" />
</ItemGroup>

View file

@ -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();
}
}