-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoleService.cs
More file actions
56 lines (47 loc) · 1.81 KB
/
RoleService.cs
File metadata and controls
56 lines (47 loc) · 1.81 KB
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
using ECommerceAPI.Application.Abstractions.Services;
using ECommerceAPI.Domain.Entities.Identity;
using Microsoft.AspNetCore.Identity;
namespace ECommerceAPI.Persistence.Services
{
public class RoleService : IRoleService
{
readonly RoleManager<AppRole> _roleManager;
public RoleService(RoleManager<AppRole> roleManager)
{
_roleManager = roleManager;
}
public async Task<bool> CreateRole(string name)
{
IdentityResult result = await _roleManager.CreateAsync(new AppRole { Id = Guid.NewGuid().ToString(), Name = name });
return result.Succeeded;
}
public async Task<bool> DeleteRole(string id)
{
AppRole role = await _roleManager.FindByIdAsync(id);
IdentityResult result = await _roleManager.DeleteAsync(role);
return result.Succeeded;
}
public (object, int) GetAllRoles(int page, int size)
{
var query = _roleManager.Roles;
IQueryable<AppRole> rolesQuery = null;
if (page != -1 && size != -1)
rolesQuery = query.Skip(page * size).Take(size);
else
rolesQuery = query;
return (rolesQuery.Select(r => new { r.Id, r.Name }), query.Count());
}
public async Task<(string id, string name)> GetRoleById(string id)
{
string role = await _roleManager.GetRoleIdAsync(new AppRole { Id = id });
return (id, role);
}
public async Task<bool> UpdateRole(string id, string name)
{
AppRole role = await _roleManager.FindByIdAsync(id);
role.Name = name;
IdentityResult result = await _roleManager.UpdateAsync(role);
return result.Succeeded;
}
}
}