-
Notifications
You must be signed in to change notification settings - Fork 33
/
RoleTable.cs
131 lines (111 loc) · 4.04 KB
/
RoleTable.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
namespace AspNet.Identity.MySQL
{
/// <summary>
/// Class that represents the Role table in the MySQL Database
/// </summary>
public class RoleTable
{
private MySQLDatabase _database;
/// <summary>
/// Constructor that takes a MySQLDatabase instance
/// </summary>
/// <param name="database"></param>
public RoleTable(MySQLDatabase database)
{
_database = database;
}
/// <summary>
/// Deltes a role from the Roles table
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns></returns>
public int Delete(string roleId)
{
string commandText = "Delete from Roles where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", roleId);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Inserts a new Role in the Roles table
/// </summary>
/// <param name="roleName">The role's name</param>
/// <returns></returns>
public int Insert(IdentityRole role)
{
string commandText = "Insert into Roles (Id, Name) values (@id, @name)";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@name", role.Name);
parameters.Add("@id", role.Id);
return _database.Execute(commandText, parameters);
}
/// <summary>
/// Returns a role name given the roleId
/// </summary>
/// <param name="roleId">The role Id</param>
/// <returns>Role name</returns>
public string GetRoleName(string roleId)
{
string commandText = "Select Name from Roles where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", roleId);
return _database.GetStrValue(commandText, parameters);
}
/// <summary>
/// Returns the role Id given a role name
/// </summary>
/// <param name="roleName">Role's name</param>
/// <returns>Role's Id</returns>
public string GetRoleId(string roleName)
{
string roleId = null;
string commandText = "Select Id from Roles where Name = @name";
Dictionary<string, object> parameters = new Dictionary<string, object>() { { "@name", roleName } };
var result = _database.QueryValue(commandText, parameters);
if (result != null)
{
return Convert.ToString(result);
}
return roleId;
}
/// <summary>
/// Gets the IdentityRole given the role Id
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public IdentityRole GetRoleById(string roleId)
{
var roleName = GetRoleName(roleId);
IdentityRole role = null;
if(roleName != null)
{
role = new IdentityRole(roleName, roleId);
}
return role;
}
/// <summary>
/// Gets the IdentityRole given the role name
/// </summary>
/// <param name="roleName"></param>
/// <returns></returns>
public IdentityRole GetRoleByName(string roleName)
{
var roleId = GetRoleId(roleName);
IdentityRole role = null;
if (roleId != null)
{
role = new IdentityRole(roleName, roleId);
}
return role;
}
public int Update(IdentityRole role)
{
string commandText = "Update Roles set Name = @name where Id = @id";
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@id", role.Id);
return _database.Execute(commandText, parameters);
}
}
}