blob: b3e734daedeb40fe75a52e00a52217612f320930 (
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
|
using System;
using System.Threading.Tasks;
using AutoMapper;
using DevHive.Data.Models;
using DevHive.Data.Repositories;
using DevHive.Services.Models.Identity.Role;
using Microsoft.AspNetCore.Mvc;
namespace DevHive.Services.Services
{
public class RoleService
{
private readonly RoleRepository _roleRepository;
private readonly IMapper _roleMapper;
public RoleService(DevHiveContext context, IMapper mapper)
{
this._roleRepository = new RoleRepository(context);
this._roleMapper = mapper;
}
public async Task<IActionResult> CreateRole(RoleServiceModel roleServiceModel)
{
if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name))
return new BadRequestObjectResult("Invalid role name!");
Role role = this._roleMapper.Map<Role>(roleServiceModel);
await this._roleRepository.AddAsync(role);
return new CreatedResult("CreateRole", role);
}
public async Task<IActionResult> GetRoleById(Guid id)
{
Role role = await this._roleRepository.GetByIdAsync(id);
if (role == null)
return new NotFoundObjectResult("Role does not exist!");
return new ObjectResult(role);
}
public async Task<IActionResult> UpdateRole(RoleServiceModel roleServiceModel)
{
if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id))
return new NotFoundObjectResult("Role does not exist!");
if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name))
return new BadRequestObjectResult("Role name already exists!");
Role role = this._roleMapper.Map<Role>(roleServiceModel);
await this._roleRepository.EditAsync(role);
return new AcceptedResult("UpdateRole", role);
}
public async Task<IActionResult> DeleteRole(Guid id)
{
if (!await this._roleRepository.DoesRoleExist(id))
return new NotFoundObjectResult("Role does not exist!");
Role role = await this._roleRepository.GetByIdAsync(id);
await this._roleRepository.DeleteAsync(role);
return new OkResult();
}
}
}
|