blob: f8075f58b4fec9c6154bdd05c01fd6183d39e048 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
using System.Threading.Tasks;
using AutoMapper;
using ExamTemplate.Data.Models;
using ExamTemplate.Data.Repositories;
using ExamTemplate.Services.Models;
using Microsoft.AspNetCore.Identity;
namespace ExamTemplate.Services
{
public class UserService
{
private readonly IMapper _autoMapper;
private readonly UserRepository _userRepository;
private readonly SignInManager<User> _signInManager;
public UserService(IMapper autoMapper, UserRepository userRepository, SignInManager<User> signInManager)
{
this._autoMapper = autoMapper;
this._userRepository = userRepository;
this._signInManager = signInManager;
}
public async Task<bool> RegisterUserAsync(RegisterUserServiceModel registerUserServiceModel)
{
User user = this._autoMapper.Map<User>(registerUserServiceModel);
bool userCreateResult = await this._userRepository.AddAsync(user, registerUserServiceModel.Password);
bool addRoleResult = await this._userRepository.AddRoleToUserAsync(user, Role.UserRole);
return userCreateResult && addRoleResult;
}
public async Task<bool> LoginUserAsync(LoginUserServiceModel loginUserServiceModel)
{
User user = await this._userRepository.GetByUsernameAsync(loginUserServiceModel.Username);
var result = await this._signInManager.PasswordSignInAsync(loginUserServiceModel.Username, loginUserServiceModel.Password, false, false);
return result.Succeeded;
}
}
}
|