AGSSbackend/AGSS/Services/UserService.cs

72 lines
2.1 KiB
C#
Raw Normal View History

using AGSS.Models;
using AGSS.Models.Entities;
using Microsoft.AspNetCore.Identity;
namespace AGSS.Services;
public class UserService
{
private readonly UserManager<UserModel> _userManager;
public UserService(UserManager<UserModel> userManager)
{
_userManager = userManager;
}
public async Task<UserProfile> GetUserProfileAsync(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
throw new ArgumentException("User not found");
}
return new UserProfile
{
Id = user.Id,
UserName = user.UserName,
Email = user.Email,
Sex = user.Sex,
Description = user.Description,
Config = user.Config,
JobCode = user.JobCode,
JobName = user.JobName,
Birthday = user.Birthday,
MenuCode = user.MenuCode,
MenuName = user.MenuName
};
}
public async Task<List<UserProfile>> GetUsersProfileInRoleAsync(string roleName)
{
var usersInRole = await _userManager.GetUsersInRoleAsync(roleName);
if (usersInRole == null || !usersInRole.Any())
{
throw new ArgumentException("No users found in the specified role");
}
var userProfiles = new List<UserProfile>();
foreach (var user in usersInRole)
{
userProfiles.Add(new UserProfile
{
Id = user.Id,
UserName = user.UserName,
Email = user.Email,
Sex = user.Sex,
Description = user.Description,
Config = user.Config,
JobCode = user.JobCode,
JobName = user.JobName,
Birthday = user.Birthday,
MenuCode = user.MenuCode,
MenuName = user.MenuName
});
}
// Assuming you want to return a single UserProfile, you might need to adjust this logic
// For now, returning the first user's profile
return userProfiles;
}
}