hanyadikhetvan-dotnethf/HanyadikHetVan/Services/UserService.cs

37 lines
983 B
C#
Raw Normal View History

2021-05-19 19:39:46 +02:00
using HanyadikHetVan.Data.Entities;
using HanyadikHetVan.DTO;
using Microsoft.AspNetCore.Identity;
using System;
using System.Threading.Tasks;
namespace HanyadikHetVan.Services
{
public class UserService
{
private readonly UserManager<User> _userManager;
public UserService(UserManager<User> userManager)
{
_userManager = userManager ?? throw new ArgumentNullException(nameof(userManager));
}
2021-05-20 01:36:54 +02:00
public async Task<UserDTO> CreateUser(UserDTO user)
2021-05-19 19:39:46 +02:00
{
var newUser = new User
{
2021-05-20 01:36:54 +02:00
UserName = user.Email,
Email = user.Email,
2021-05-19 19:39:46 +02:00
EmailConfirmed = true,
};
2021-05-20 01:36:54 +02:00
var result = await _userManager.CreateAsync(newUser, user.Password);
2021-05-19 19:39:46 +02:00
if (!result.Succeeded)
{
throw new Exception("Failed to create user.");
}
2021-05-20 01:36:54 +02:00
return new UserDTO() { Email = user.Email };
2021-05-19 19:39:46 +02:00
}
}
}