aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2020-12-17 11:21:14 +0200
committerSyndamia <kamen.d.mladenov@protonmail.com>2020-12-17 11:21:14 +0200
commit5811d878180a85af5c41b9cb3b8f272d589539e9 (patch)
tree16c27c0c797f004d20634e29d1752c994386537d /src/DevHive.Data
parentedf40b339c999c936d0e022100d67c853cd1b792 (diff)
downloadDevHive-5811d878180a85af5c41b9cb3b8f272d589539e9.tar
DevHive-5811d878180a85af5c41b9cb3b8f272d589539e9.tar.gz
DevHive-5811d878180a85af5c41b9cb3b8f272d589539e9.zip
Roles are now in a seperate table, user can contain multiple roles (BUT JWT doesn't support mutiple yet), added migrations, userrepository returns user roles with the user
Diffstat (limited to 'src/DevHive.Data')
-rw-r--r--src/DevHive.Data/Models/Role.cs3
-rw-r--r--src/DevHive.Data/Models/User.cs3
-rw-r--r--src/DevHive.Data/Repositories/DevHiveContext.cs4
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs7
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs7
5 files changed, 22 insertions, 2 deletions
diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs
index b4c7009..0bf2ab9 100644
--- a/src/DevHive.Data/Models/Role.cs
+++ b/src/DevHive.Data/Models/Role.cs
@@ -1,6 +1,7 @@
using System;
using Microsoft.AspNetCore.Identity;
using System.ComponentModel.DataAnnotations.Schema;
+using System.Collections.Generic;
namespace DevHive.Data.Models
{
@@ -8,5 +9,7 @@ namespace DevHive.Data.Models
public class Role : IdentityRole<Guid>
{
public const string DefaultRole = "User";
+
+ public virtual List<User> Users { get; set; }
}
}
diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs
index a3731cb..ecd7af8 100644
--- a/src/DevHive.Data/Models/User.cs
+++ b/src/DevHive.Data/Models/User.cs
@@ -1,4 +1,5 @@
using System;
+using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;
@@ -19,7 +20,7 @@ namespace DevHive.Data.Models
public string ProfilePicture { get; set; }
- public string Role { get; set; }
+ public virtual List<Role> Roles { get; set; }
//public List<User> Friends { get; set; }
}
diff --git a/src/DevHive.Data/Repositories/DevHiveContext.cs b/src/DevHive.Data/Repositories/DevHiveContext.cs
index 2f0fa55..373eb5e 100644
--- a/src/DevHive.Data/Repositories/DevHiveContext.cs
+++ b/src/DevHive.Data/Repositories/DevHiveContext.cs
@@ -18,6 +18,10 @@ namespace DevHive.Data.Repositories
builder.Entity<User>()
.HasIndex(x => x.UserName)
.IsUnique();
+
+ builder.Entity<Role>()
+ .HasIndex(x => x.Id)
+ .IsUnique();
base.OnModelCreating(builder);
}
diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs
index 2652760..21c29db 100644
--- a/src/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/DevHive.Data/Repositories/RoleRepository.cs
@@ -34,6 +34,13 @@ namespace DevHive.Data.Repositories
.FindAsync(id);
}
+ public async Task<Role> GetByNameAsync(string name)
+ {
+ return await this._context
+ .Set<Role>()
+ .FirstOrDefaultAsync(x => x.Name == name);
+ }
+
//Update
public async Task<bool> EditAsync(Role newEntity)
{
diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs
index f5a074b..1508947 100644
--- a/src/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/DevHive.Data/Repositories/UserRepository.cs
@@ -33,6 +33,7 @@ namespace DevHive.Data.Repositories
{
return this._context
.Set<User>()
+ .Include(x => x.Roles)
.AsNoTracking()
.AsEnumerable();
}
@@ -41,13 +42,17 @@ namespace DevHive.Data.Repositories
{
return await this._context
.Set<User>()
- .FindAsync(id);
+ .Include(x => x.Roles)
+ // To also return the roles, you need to include the roles table,
+ // but then you loose FindAsync, because there is id of role and id of user
+ .FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<User> GetByUsername(string username)
{
return await this._context
.Set<User>()
+ .Include(u => u.Roles)
.FirstOrDefaultAsync(x => x.UserName == username);
}