using AGSS.Models; using AGSS.Models.Entities; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; namespace AGSS.Services; public class UserService { private readonly UserManager _userManager; public UserService(UserManager userManager) { _userManager = userManager; } public async Task 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> 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(); 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; } public async Task> GetUsersProfileByUserNameAsync(string userName) { var users = await _userManager.Users .Where(u => u.UserName.Contains(userName)) .Select(u => new UserProfile { Id = u.Id, UserName = u.UserName, Email = u.Email, Sex = u.Sex, Description = u.Description, Config = u.Config, JobCode = u.JobCode, JobName = u.JobName, Birthday = u.Birthday, MenuCode = u.MenuCode, MenuName = u.MenuName }) .ToListAsync(); if (users == null || !users.Any()) { throw new ArgumentException("No users found with the specified username"); } return users; } public async Task> GetUsersProfileAllAsync() { var users = await _userManager.Users .Select(u => new UserProfile { Id = u.Id, UserName = u.UserName, Email = u.Email, Sex = u.Sex, Description = u.Description, Config = u.Config, JobCode = u.JobCode, JobName = u.JobName, Birthday = u.Birthday, MenuCode = u.MenuCode, MenuName = u.MenuName }) .ToListAsync(); if (users == null || !users.Any()) { throw new ArgumentException("No users found"); } return users; } }