50 lines
1.4 KiB
C#
50 lines
1.4 KiB
C#
using Contracts.DTO;
|
|
using Contracts.Mappers;
|
|
using Contracts.Repositories;
|
|
using Contracts.SearchModels;
|
|
using Contracts.Services;
|
|
using Contracts.ViewModels;
|
|
using Services.Support.Exceptions;
|
|
|
|
namespace Services.Domain;
|
|
|
|
public class AuthService : IAuthService
|
|
{
|
|
private readonly IUserRepo _userRepo;
|
|
|
|
public AuthService(IUserRepo userRepo)
|
|
{
|
|
_userRepo = userRepo;
|
|
}
|
|
|
|
public async Task<UserViewModel> Login(UserLoginDTO loginData)
|
|
{
|
|
if (loginData == null || string.IsNullOrWhiteSpace(loginData.Name)
|
|
|| string.IsNullOrWhiteSpace(loginData.Password))
|
|
{
|
|
throw new ArgumentException("Неверные данные для входа");
|
|
}
|
|
|
|
var user = await _userRepo.Get(new UserSearch() { Name = loginData.Name });
|
|
if (user == null)
|
|
{
|
|
throw new UserNotFoundException($"Пользователь {loginData.Name} не найден");
|
|
}
|
|
|
|
return user.ToView();
|
|
}
|
|
|
|
public async Task<UserViewModel> Register(UserDto user)
|
|
{
|
|
var existingUser = await _userRepo.Get(new UserSearch() { Name = user.Name });
|
|
if (existingUser != null)
|
|
{
|
|
throw new AlreadyExistsException("Такой пользователь уже существует");
|
|
}
|
|
|
|
var createdUser = await _userRepo.Create(user);
|
|
|
|
return createdUser.ToView();
|
|
}
|
|
}
|