2023-05-18 19:42:14 +03:00
|
|
|
|
using AutoMapper;
|
|
|
|
|
using NG_2023_Kanban.BusinessLayer.Interfaces;
|
|
|
|
|
using NG_2023_Kanban.BusinessLayer.Models;
|
2023-05-17 12:36:49 +03:00
|
|
|
|
using NG_2023_Kanban.DataLayer.Repositories;
|
|
|
|
|
using NG_2023_Kanban.DataLayer.Entities;
|
|
|
|
|
using NG_2023_Kanban.DataLayer.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace NG_2023_Kanban.BusinessLayer.Services
|
|
|
|
|
{
|
|
|
|
|
public class UserService : IUserService
|
|
|
|
|
{
|
|
|
|
|
private readonly IUserRepository _userRepository;
|
2023-05-18 19:42:14 +03:00
|
|
|
|
private readonly IMapper _mapper;
|
2023-05-17 12:36:49 +03:00
|
|
|
|
|
2023-05-18 19:42:14 +03:00
|
|
|
|
public UserService(IUserRepository userRepository, IMapper mapper)
|
2023-05-17 12:36:49 +03:00
|
|
|
|
{
|
|
|
|
|
_userRepository = userRepository;
|
2023-05-18 19:42:14 +03:00
|
|
|
|
_mapper = mapper;
|
2023-05-17 12:36:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 21:44:06 +03:00
|
|
|
|
public async Task<UserModel> GetAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
return _mapper.Map<UserModel>(await _userRepository.GetAsync(id));
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 22:38:01 +03:00
|
|
|
|
public async Task<ICollection<UserModel>> GetAllAsync()
|
|
|
|
|
{
|
|
|
|
|
return _mapper.Map<ICollection<UserModel>>(await _userRepository.GetAllAsync());
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-21 18:36:49 +03:00
|
|
|
|
public async Task UpdateAsync(int id, UserModel user)
|
|
|
|
|
{
|
|
|
|
|
var entity = _mapper.Map<User>(user);
|
|
|
|
|
await _userRepository.UpdateAsync(id, entity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
await _userRepository.DeleteAsync(id);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 19:42:14 +03:00
|
|
|
|
public async Task<UserModel?> LoginAsync(UserModel user)
|
2023-05-17 12:36:49 +03:00
|
|
|
|
{
|
|
|
|
|
var data = await _userRepository.FindAsync(x => x.Username == user.Username && x.Password == user.Password);
|
|
|
|
|
if (data.Any())
|
2023-05-18 19:42:14 +03:00
|
|
|
|
return _mapper.Map<ICollection<UserModel>>(data).First();
|
2023-05-17 12:36:49 +03:00
|
|
|
|
else
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-18 19:42:14 +03:00
|
|
|
|
public async Task<UserModel> RegisterAsync(UserModel user)
|
2023-05-17 12:36:49 +03:00
|
|
|
|
{
|
2023-05-18 19:42:14 +03:00
|
|
|
|
var entity = _mapper.Map<User>(user);
|
|
|
|
|
await _userRepository.CreateAsync(entity);
|
|
|
|
|
return _mapper.Map<UserModel>(entity);
|
2023-05-17 12:36:49 +03:00
|
|
|
|
}
|
2023-05-22 19:52:34 +03:00
|
|
|
|
|
|
|
|
|
public async Task<bool> CheckAdminAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
var user = await GetAsync(id);
|
2023-05-30 20:54:17 +03:00
|
|
|
|
|
|
|
|
|
return user.Roles.Any(role => role.Name == "Administrator");
|
2023-05-22 19:52:34 +03:00
|
|
|
|
}
|
2023-05-17 12:36:49 +03:00
|
|
|
|
}
|
|
|
|
|
}
|