blob: 91a8c738fe0fc0b500a21cdeee1dfee94b43dfe5 (
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Interfaces.Repositories;
using DevHive.Data.Models;
using DevHive.Services.Interfaces;
using DevHive.Services.Models.Identity.Role;
using DevHive.Services.Models.Language;
namespace DevHive.Services.Services
{
public class RoleService : IRoleService
{
private readonly IRoleRepository _roleRepository;
private readonly IMapper _roleMapper;
public RoleService(IRoleRepository roleRepository, IMapper mapper)
{
this._roleRepository = roleRepository;
this._roleMapper = mapper;
}
public async Task<Guid> CreateRole(CreateRoleServiceModel roleServiceModel)
{
if (await this._roleRepository.DoesNameExist(roleServiceModel.Name))
throw new ArgumentException("Role already exists!");
Role role = this._roleMapper.Map<Role>(roleServiceModel);
bool success = await this._roleRepository.AddAsync(role);
if(success)
{
Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name);
return newRole.Id;
}
else
return Guid.Empty;
}
public async Task<RoleServiceModel> GetRoleById(Guid id)
{
Role role = await this._roleRepository.GetByIdAsync(id)
?? throw new ArgumentException("Role does not exist!");
return this._roleMapper.Map<RoleServiceModel>(role);
}
public async Task<bool> UpdateRole(UpdateRoleServiceModel updateRoleServiceModel)
{
if (!await this._roleRepository.DoesRoleExist(updateRoleServiceModel.Id))
throw new ArgumentException("Role does not exist!");
if (await this._roleRepository.DoesNameExist(updateRoleServiceModel.Name))
throw new ArgumentException("Role name already exists!");
Role role = this._roleMapper.Map<Role>(updateRoleServiceModel);
return await this._roleRepository.EditAsync(updateRoleServiceModel.Id, role);
}
public async Task<bool> DeleteRole(Guid id)
{
if (!await this._roleRepository.DoesRoleExist(id))
throw new ArgumentException("Role does not exist!");
Role role = await this._roleRepository.GetByIdAsync(id);
return await this._roleRepository.DeleteAsync(role);
}
}
}
|