Moves the ChangeUserAdminStatusRequest to the ApiModels folder. Updates the admin controller route to "admin" and the admin status endpoint to "adminStatus". Makes the ChangeUserAdminStatus method asynchronous. Uses NotFoundException instead of KeyNotFoundException.
34 lines
No EOL
988 B
C#
34 lines
No EOL
988 B
C#
using System.Security.Claims;
|
|
using DrinkRateAPI.Contexts;
|
|
using DrinkRateAPI.DbEntities;
|
|
using DrinkRateAPI.Exceptions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace DrinkRateAPI.Services;
|
|
|
|
public class UserProfileService(ApplicationDbContext context)
|
|
{
|
|
private ApplicationDbContext _context = context;
|
|
|
|
public bool IsUserProfileAdmin(DbUserProfile userProfile)
|
|
{
|
|
return userProfile.IsAdmin;
|
|
}
|
|
|
|
public async Task<DbUserProfile> ChangeUserAdminStatusAsync(string userId, bool changeStatusTo)
|
|
{
|
|
var userProfile = GetUserProfileById(userId);
|
|
userProfile.IsAdmin = changeStatusTo;
|
|
_context.UserProfiles.Update(userProfile);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return userProfile;
|
|
}
|
|
|
|
public DbUserProfile GetUserProfileById(string userId)
|
|
{
|
|
var userProfile = _context.UserProfiles.FirstOrDefault(x => x.Id.ToString() == userId);
|
|
|
|
return userProfile ?? throw new NotFoundException();
|
|
}
|
|
} |