Files
survey-beta/survey-beta/Services/UsersServices.cs
majed adel 3aabe1a367 PATCH
Refactored DTOs to use AutoMapper instead of manual mapping and made some additional improvements and fixes.
Added : GetAllSurveys&DeleteUser.
2025-02-07 06:40:57 -08:00

123 lines
3.9 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;
using survey_beta.DTOs.Create;
using survey_beta.DTOs.Default;
using survey_beta.Models;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using AutoMapper;
public class UsersServices
{
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
private readonly IConfiguration _configuration;
private readonly IMapper _mapper;
public UsersServices(UserManager<User> userManager, SignInManager<User> signInManager, IConfiguration configuration, IMapper mapper)
{
_userManager = userManager;
_signInManager = signInManager;
_configuration = configuration;
_mapper = mapper;
}
public async Task<UserDto> CreateUserAsync(CreateUserDto createUserDto)
{
var existingUser = await _userManager.FindByEmailAsync(createUserDto.Email);
if (existingUser != null)
{
throw new Exception("User with this email already exists.");
}
var user = _mapper.Map<User>(createUserDto);
var result = await _userManager.CreateAsync(user, createUserDto.Password);
if (!result.Succeeded)
{
throw new Exception("Failed to create user: " + string.Join(", ", result.Errors.Select(e => e.Description)));
}
var userDto = _mapper.Map<UserDto>(user);
userDto.Token = GenerateJwtToken(user);
return userDto;
}
public async Task<UserDto> SignInAsync(LoginDto loginDto)
{
var user = await _userManager.FindByNameAsync(loginDto.Username);
if (user == null)
{
throw new Exception("Invalid login attempt.");
}
var result = await _signInManager.PasswordSignInAsync(user, loginDto.Password, false, false);
if (!result.Succeeded)
{
throw new Exception("Invalid login attempt.");
}
var userDto = _mapper.Map<UserDto>(user);
userDto.Token = GenerateJwtToken(user);
return userDto;
}
public async Task<UserDto> GetUserByIdAsync(string userId)
{
var user = await _userManager.FindByIdAsync(userId);
if (user == null)
{
throw new Exception("User not found.");
}
return _mapper.Map<UserDto>(user);
}
public async Task<UserDto> GetUserByUsernameAsync(string username)
{
var user = await _userManager.FindByNameAsync(username);
if (user == null)
{
throw new Exception("User not found.");
}
return _mapper.Map<UserDto>(user);
}
private string GenerateJwtToken(User user)
{
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Name, user.Fullname)
};
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var token = new JwtSecurityToken(
issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);
return new JwtSecurityTokenHandler().WriteToken(token);
}
public async Task<List<UserDto>> GetAllUsersAsync()
{
var users = _userManager.Users.ToList();
return _mapper.Map<List<UserDto>>(users);
}
public async Task<bool> DeleteUsersAsync(string userid)
{
var user = await _userManager.FindByIdAsync(userid);
if (user == null) return false;
var result = await _userManager.DeleteAsync(user);
return result.Succeeded;
}
}