aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-02-16 18:07:51 +0200
committertranstrike <transtrike@gmail.com>2021-02-16 18:07:51 +0200
commit72502154725594cf31878aa944f4bc9d9f3521a3 (patch)
tree6ea1f475f35fba320b32664084d4c39e2d084e49
parenta2ca63701b71eed2ffa3fff6de16b9babe8bba08 (diff)
downloadDevHive-72502154725594cf31878aa944f4bc9d9f3521a3.tar
DevHive-72502154725594cf31878aa944f4bc9d9f3521a3.tar.gz
DevHive-72502154725594cf31878aa944f4bc9d9f3521a3.zip
UserManager&RoleManager logic moved to Repo; Password hashing and validation moved to Repo and using ASPNET Core hashing methods; Added Migrations; Fixed Roles not added to user
-rw-r--r--src/.stylecop.json25
-rw-r--r--src/Common/DevHive.Common.Models/Misc/PasswordModifications.cs13
-rw-r--r--src/Data/DevHive.Data.Models/User.cs2
-rw-r--r--src/Data/DevHive.Data.Tests/UserRepositoryTests.cs8
-rw-r--r--src/Data/DevHive.Data/Interfaces/IUserRepository.cs8
-rw-r--r--src/Data/DevHive.Data/Migrations/20210216152915_Inital.Designer.cs667
-rw-r--r--src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs586
-rw-r--r--src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs665
-rw-r--r--src/Data/DevHive.Data/Repositories/RoleRepository.cs37
-rw-r--r--src/Data/DevHive.Data/Repositories/UserRepository.cs82
-rw-r--r--src/Services/DevHive.Services.Tests/UserService.Tests.cs2
-rw-r--r--src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs9
-rw-r--r--src/Services/DevHive.Services/Services/RoleService.cs4
-rw-r--r--src/Services/DevHive.Services/Services/UserService.cs74
-rw-r--r--src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs12
-rw-r--r--src/Web/DevHive.Web/Controllers/UserController.cs9
-rw-r--r--src/Web/DevHive.Web/Startup.cs1
17 files changed, 2081 insertions, 123 deletions
diff --git a/src/.stylecop.json b/src/.stylecop.json
new file mode 100644
index 0000000..6954967
--- /dev/null
+++ b/src/.stylecop.json
@@ -0,0 +1,25 @@
+{
+ "$schema": "https://raw.githubusercontent.com/DotNetAnalyzers/StyleCopAnalyzers/master/StyleCop.Analyzers/StyleCop.Analyzers/Settings/stylecop.schema.json",
+ "settings": {
+ "indentation": {
+ "useTabs": true,
+ "tabSize": 4,
+ "indentationSize": 4
+ },
+ "orderingRules": {
+ "elementOrder": [
+ "kind",
+ "accessibility",
+ "constant",
+ "static",
+ "readonly"
+ ],
+ "blankLinesBetweenUsingGroups": "require",
+ "systemUsingDirectivesFirst": true,
+ "usingDirectivesPlacement": "outsideNamespace"
+ },
+ "namingRules": {
+
+ }
+ }
+}
diff --git a/src/Common/DevHive.Common.Models/Misc/PasswordModifications.cs b/src/Common/DevHive.Common.Models/Misc/PasswordModifications.cs
deleted file mode 100644
index f10a334..0000000
--- a/src/Common/DevHive.Common.Models/Misc/PasswordModifications.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-using System.Security.Cryptography;
-using System.Text;
-
-namespace DevHive.Common.Models.Misc
-{
- public static class PasswordModifications
- {
- public static string GeneratePasswordHash(string password)
- {
- return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password)));
- }
- }
-}
diff --git a/src/Data/DevHive.Data.Models/User.cs b/src/Data/DevHive.Data.Models/User.cs
index bf4ea50..bac5425 100644
--- a/src/Data/DevHive.Data.Models/User.cs
+++ b/src/Data/DevHive.Data.Models/User.cs
@@ -14,7 +14,7 @@ namespace DevHive.Data.Models
public string LastName { get; set; }
- public ProfilePicture ProfilePicture { get; set; }
+ public ProfilePicture ProfilePicture { get; set; } = new() { PictureURL = "/assets/images/feed/profile-pic.png" };
public HashSet<Language> Languages { get; set; } = new();
diff --git a/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs b/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs
index 43e9a36..5f84e34 100644
--- a/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs
+++ b/src/Data/DevHive.Data.Tests/UserRepositoryTests.cs
@@ -234,7 +234,7 @@ namespace DevHive.Data.Tests
// }
#endregion
- #region DoesUserHaveThisUsername
+ #region DoesUserHaveThisUsernameAsync
[Test]
public async Task DoesUserHaveThisUsername_ReturnsTrue_WhenUserHasTheGivenUsername()
{
@@ -242,9 +242,9 @@ namespace DevHive.Data.Tests
this._context.Users.Add(dummyUser);
await this._context.SaveChangesAsync();
- bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, dummyUser.UserName);
+ bool result = this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, dummyUser.UserName);
- Assert.IsTrue(result, "DoesUserHaveThisUsername does not return true when the user has the given name");
+ Assert.IsTrue(result, "DoesUserHaveThisUsernameAsync does not return true when the user has the given name");
}
[Test]
@@ -255,7 +255,7 @@ namespace DevHive.Data.Tests
this._context.Users.Add(dummyUser);
await this._context.SaveChangesAsync();
- bool result = this._userRepository.DoesUserHaveThisUsername(dummyUser.Id, username);
+ bool result = this._userRepository.DoesUserHaveThisUsernameAsync(dummyUser.Id, username);
Assert.IsFalse(result, "DoesUserNameExistAsync does not return false when user doesnt have the given name");
}
diff --git a/src/Data/DevHive.Data/Interfaces/IUserRepository.cs b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs
index 917809d..aec0eb3 100644
--- a/src/Data/DevHive.Data/Interfaces/IUserRepository.cs
+++ b/src/Data/DevHive.Data/Interfaces/IUserRepository.cs
@@ -8,15 +8,17 @@ namespace DevHive.Data.Interfaces
{
public interface IUserRepository : IRepository<User>
{
- //Read
+ Task<bool> AddRoleToUser(User user, string roleName);
+
Task<User> GetByUsernameAsync(string username);
Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl);
- //Validations
+ Task<bool> VerifyPassword(User user, string password);
+ Task<bool> IsInRoleAsync(User user, string roleName);
Task<bool> ValidateFriendsCollectionAsync(List<string> usernames);
Task<bool> DoesEmailExistAsync(string email);
Task<bool> DoesUserExistAsync(Guid id);
Task<bool> DoesUsernameExistAsync(string username);
- bool DoesUserHaveThisUsername(Guid id, string username);
+ Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username);
}
}
diff --git a/src/Data/DevHive.Data/Migrations/20210216152915_Inital.Designer.cs b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.Designer.cs
new file mode 100644
index 0000000..a85f657
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.Designer.cs
@@ -0,0 +1,667 @@
+// <auto-generated />
+using System;
+using DevHive.Data;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ [DbContext(typeof(DevHiveContext))]
+ [Migration("20210216152915_Inital")]
+ partial class Inital
+ {
+ protected override void BuildTargetModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("Relational:MaxIdentifierLength", 63)
+ .HasAnnotation("ProductVersion", "5.0.3")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("Comments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Language", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Languages");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.ToTable("Posts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("PictureURL")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("ProfilePicture");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<int>("Rate")
+ .HasColumnType("integer");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId")
+ .IsUnique();
+
+ b.ToTable("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("FileUrl")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("PostAttachments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.RatedPost", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "PostId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("RatedPosts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.UserRate", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<bool>("Liked")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("UserRates");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Role", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedName")
+ .IsUnique()
+ .HasDatabaseName("RoleNameIndex");
+
+ b.ToTable("AspNetRoles");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Technology", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Technologies");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<int>("AccessFailedCount")
+ .HasColumnType("integer");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Email")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<bool>("EmailConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("FirstName")
+ .HasColumnType("text");
+
+ b.Property<string>("LastName")
+ .HasColumnType("text");
+
+ b.Property<bool>("LockoutEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<DateTimeOffset?>("LockoutEnd")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<string>("NormalizedEmail")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedUserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("PasswordHash")
+ .HasColumnType("text");
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnType("text");
+
+ b.Property<bool>("PhoneNumberConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("SecurityStamp")
+ .HasColumnType("text");
+
+ b.Property<bool>("TwoFactorEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("UserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedEmail")
+ .HasDatabaseName("EmailIndex");
+
+ b.HasIndex("NormalizedUserName")
+ .IsUnique()
+ .HasDatabaseName("UserNameIndex");
+
+ b.HasIndex("UserId");
+
+ b.HasIndex("UserName")
+ .IsUnique();
+
+ b.ToTable("AspNetUsers");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.Property<Guid>("LanguagesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LanguagesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("LanguageUser");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetRoleClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderKey")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderDisplayName")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LoginProvider", "ProviderKey");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserLogins");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetUserRoles");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.Property<string>("Value")
+ .HasColumnType("text");
+
+ b.HasKey("UserId", "LoginProvider", "Name");
+
+ b.ToTable("AspNetUserTokens");
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.Property<Guid>("RolesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("RolesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("RoleUser");
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.Property<Guid>("TechnologiesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("TechnologiesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("TechnologyUser");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Comments")
+ .HasForeignKey("CreatorId");
+
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Comments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Creator");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Posts")
+ .HasForeignKey("CreatorId");
+
+ b.Navigation("Creator");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithOne("ProfilePicture")
+ .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithOne("Rating")
+ .HasForeignKey("DevHive.Data.Models.Rating", "PostId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Attachments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.RatedPost", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany()
+ .HasForeignKey("PostId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany("RatedPosts")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.UserRate", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany()
+ .HasForeignKey("PostId");
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany()
+ .HasForeignKey("UserId");
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany("Friends")
+ .HasForeignKey("UserId");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Language", null)
+ .WithMany()
+ .HasForeignKey("LanguagesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RolesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Technology", null)
+ .WithMany()
+ .HasForeignKey("TechnologiesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Navigation("Attachments");
+
+ b.Navigation("Comments");
+
+ b.Navigation("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Navigation("Comments");
+
+ b.Navigation("Friends");
+
+ b.Navigation("Posts");
+
+ b.Navigation("ProfilePicture");
+
+ b.Navigation("RatedPosts");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs
new file mode 100644
index 0000000..131501e
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/20210216152915_Inital.cs
@@ -0,0 +1,586 @@
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ public partial class Inital : Migration
+ {
+ protected override void Up(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.CreateTable(
+ name: "AspNetRoles",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ ConcurrencyStamp = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetRoles", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUsers",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ FirstName = table.Column<string>(type: "text", nullable: true),
+ LastName = table.Column<string>(type: "text", nullable: true),
+ UserId = table.Column<Guid>(type: "uuid", nullable: true),
+ UserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedUserName = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ Email = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ NormalizedEmail = table.Column<string>(type: "character varying(256)", maxLength: 256, nullable: true),
+ EmailConfirmed = table.Column<bool>(type: "boolean", nullable: false),
+ PasswordHash = table.Column<string>(type: "text", nullable: true),
+ SecurityStamp = table.Column<string>(type: "text", nullable: true),
+ ConcurrencyStamp = table.Column<string>(type: "text", nullable: true),
+ PhoneNumber = table.Column<string>(type: "text", nullable: true),
+ PhoneNumberConfirmed = table.Column<bool>(type: "boolean", nullable: false),
+ TwoFactorEnabled = table.Column<bool>(type: "boolean", nullable: false),
+ LockoutEnd = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
+ LockoutEnabled = table.Column<bool>(type: "boolean", nullable: false),
+ AccessFailedCount = table.Column<int>(type: "integer", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUsers", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetUsers_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Languages",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Languages", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Technologies",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Technologies", x => x.Id);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetRoleClaims",
+ columns: table => new
+ {
+ Id = table.Column<int>(type: "integer", nullable: false)
+ .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
+ RoleId = table.Column<Guid>(type: "uuid", nullable: false),
+ ClaimType = table.Column<string>(type: "text", nullable: true),
+ ClaimValue = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetRoleClaims_AspNetRoles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserClaims",
+ columns: table => new
+ {
+ Id = table.Column<int>(type: "integer", nullable: false)
+ .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ ClaimType = table.Column<string>(type: "text", nullable: true),
+ ClaimValue = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserClaims", x => x.Id);
+ table.ForeignKey(
+ name: "FK_AspNetUserClaims_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserLogins",
+ columns: table => new
+ {
+ LoginProvider = table.Column<string>(type: "text", nullable: false),
+ ProviderKey = table.Column<string>(type: "text", nullable: false),
+ ProviderDisplayName = table.Column<string>(type: "text", nullable: true),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey });
+ table.ForeignKey(
+ name: "FK_AspNetUserLogins_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserRoles",
+ columns: table => new
+ {
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ RoleId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });
+ table.ForeignKey(
+ name: "FK_AspNetUserRoles_AspNetRoles_RoleId",
+ column: x => x.RoleId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_AspNetUserRoles_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "AspNetUserTokens",
+ columns: table => new
+ {
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ LoginProvider = table.Column<string>(type: "text", nullable: false),
+ Name = table.Column<string>(type: "text", nullable: false),
+ Value = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name });
+ table.ForeignKey(
+ name: "FK_AspNetUserTokens_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Posts",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
+ Message = table.Column<string>(type: "text", nullable: true),
+ TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Posts", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Posts_AspNetUsers_CreatorId",
+ column: x => x.CreatorId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "ProfilePicture",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ PictureURL = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_ProfilePicture", x => x.Id);
+ table.ForeignKey(
+ name: "FK_ProfilePicture_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "RoleUser",
+ columns: table => new
+ {
+ RolesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_RoleUser_AspNetRoles_RolesId",
+ column: x => x.RolesId,
+ principalTable: "AspNetRoles",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_RoleUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "LanguageUser",
+ columns: table => new
+ {
+ LanguagesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_LanguageUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_LanguageUser_Languages_LanguagesId",
+ column: x => x.LanguagesId,
+ principalTable: "Languages",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "TechnologyUser",
+ columns: table => new
+ {
+ TechnologiesId = table.Column<Guid>(type: "uuid", nullable: false),
+ UsersId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId });
+ table.ForeignKey(
+ name: "FK_TechnologyUser_AspNetUsers_UsersId",
+ column: x => x.UsersId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_TechnologyUser_Technologies_TechnologiesId",
+ column: x => x.TechnologiesId,
+ principalTable: "Technologies",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Comments",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true),
+ CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
+ Message = table.Column<string>(type: "text", nullable: true),
+ TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Comments", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Comments_AspNetUsers_CreatorId",
+ column: x => x.CreatorId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ table.ForeignKey(
+ name: "FK_Comments_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "PostAttachments",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true),
+ FileUrl = table.Column<string>(type: "text", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_PostAttachments", x => x.Id);
+ table.ForeignKey(
+ name: "FK_PostAttachments_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "RatedPosts",
+ columns: table => new
+ {
+ UserId = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_RatedPosts", x => new { x.UserId, x.PostId });
+ table.ForeignKey(
+ name: "FK_RatedPosts_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ table.ForeignKey(
+ name: "FK_RatedPosts_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "Rating",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: false),
+ Rate = table.Column<int>(type: "integer", nullable: false)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_Rating", x => x.Id);
+ table.ForeignKey(
+ name: "FK_Rating_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Cascade);
+ });
+
+ migrationBuilder.CreateTable(
+ name: "UserRates",
+ columns: table => new
+ {
+ Id = table.Column<Guid>(type: "uuid", nullable: false),
+ UserId = table.Column<Guid>(type: "uuid", nullable: true),
+ Liked = table.Column<bool>(type: "boolean", nullable: false),
+ PostId = table.Column<Guid>(type: "uuid", nullable: true)
+ },
+ constraints: table =>
+ {
+ table.PrimaryKey("PK_UserRates", x => x.Id);
+ table.ForeignKey(
+ name: "FK_UserRates_AspNetUsers_UserId",
+ column: x => x.UserId,
+ principalTable: "AspNetUsers",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ table.ForeignKey(
+ name: "FK_UserRates_Posts_PostId",
+ column: x => x.PostId,
+ principalTable: "Posts",
+ principalColumn: "Id",
+ onDelete: ReferentialAction.Restrict);
+ });
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetRoleClaims_RoleId",
+ table: "AspNetRoleClaims",
+ column: "RoleId");
+
+ migrationBuilder.CreateIndex(
+ name: "RoleNameIndex",
+ table: "AspNetRoles",
+ column: "NormalizedName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserClaims_UserId",
+ table: "AspNetUserClaims",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserLogins_UserId",
+ table: "AspNetUserLogins",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUserRoles_RoleId",
+ table: "AspNetUserRoles",
+ column: "RoleId");
+
+ migrationBuilder.CreateIndex(
+ name: "EmailIndex",
+ table: "AspNetUsers",
+ column: "NormalizedEmail");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUsers_UserId",
+ table: "AspNetUsers",
+ column: "UserId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_AspNetUsers_UserName",
+ table: "AspNetUsers",
+ column: "UserName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "UserNameIndex",
+ table: "AspNetUsers",
+ column: "NormalizedUserName",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Comments_CreatorId",
+ table: "Comments",
+ column: "CreatorId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Comments_PostId",
+ table: "Comments",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_LanguageUser_UsersId",
+ table: "LanguageUser",
+ column: "UsersId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_PostAttachments_PostId",
+ table: "PostAttachments",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Posts_CreatorId",
+ table: "Posts",
+ column: "CreatorId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_ProfilePicture_UserId",
+ table: "ProfilePicture",
+ column: "UserId",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_RatedPosts_PostId",
+ table: "RatedPosts",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_Rating_PostId",
+ table: "Rating",
+ column: "PostId",
+ unique: true);
+
+ migrationBuilder.CreateIndex(
+ name: "IX_RoleUser_UsersId",
+ table: "RoleUser",
+ column: "UsersId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_TechnologyUser_UsersId",
+ table: "TechnologyUser",
+ column: "UsersId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserRates_PostId",
+ table: "UserRates",
+ column: "PostId");
+
+ migrationBuilder.CreateIndex(
+ name: "IX_UserRates_UserId",
+ table: "UserRates",
+ column: "UserId");
+ }
+
+ protected override void Down(MigrationBuilder migrationBuilder)
+ {
+ migrationBuilder.DropTable(
+ name: "AspNetRoleClaims");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserClaims");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserLogins");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserRoles");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUserTokens");
+
+ migrationBuilder.DropTable(
+ name: "Comments");
+
+ migrationBuilder.DropTable(
+ name: "LanguageUser");
+
+ migrationBuilder.DropTable(
+ name: "PostAttachments");
+
+ migrationBuilder.DropTable(
+ name: "ProfilePicture");
+
+ migrationBuilder.DropTable(
+ name: "RatedPosts");
+
+ migrationBuilder.DropTable(
+ name: "Rating");
+
+ migrationBuilder.DropTable(
+ name: "RoleUser");
+
+ migrationBuilder.DropTable(
+ name: "TechnologyUser");
+
+ migrationBuilder.DropTable(
+ name: "UserRates");
+
+ migrationBuilder.DropTable(
+ name: "Languages");
+
+ migrationBuilder.DropTable(
+ name: "AspNetRoles");
+
+ migrationBuilder.DropTable(
+ name: "Technologies");
+
+ migrationBuilder.DropTable(
+ name: "Posts");
+
+ migrationBuilder.DropTable(
+ name: "AspNetUsers");
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs
new file mode 100644
index 0000000..8db3abf
--- /dev/null
+++ b/src/Data/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs
@@ -0,0 +1,665 @@
+// <auto-generated />
+using System;
+using DevHive.Data;
+using Microsoft.EntityFrameworkCore;
+using Microsoft.EntityFrameworkCore.Infrastructure;
+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
+using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
+
+namespace DevHive.Data.Migrations
+{
+ [DbContext(typeof(DevHiveContext))]
+ partial class DevHiveContextModelSnapshot : ModelSnapshot
+ {
+ protected override void BuildModel(ModelBuilder modelBuilder)
+ {
+#pragma warning disable 612, 618
+ modelBuilder
+ .HasAnnotation("Relational:MaxIdentifierLength", 63)
+ .HasAnnotation("ProductVersion", "5.0.3")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("Comments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Language", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Languages");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("CreatorId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("Message")
+ .HasColumnType("text");
+
+ b.Property<DateTime>("TimeCreated")
+ .HasColumnType("timestamp without time zone");
+
+ b.HasKey("Id");
+
+ b.HasIndex("CreatorId");
+
+ b.ToTable("Posts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("PictureURL")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId")
+ .IsUnique();
+
+ b.ToTable("ProfilePicture");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<int>("Rate")
+ .HasColumnType("integer");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId")
+ .IsUnique();
+
+ b.ToTable("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("FileUrl")
+ .HasColumnType("text");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("PostAttachments");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.RatedPost", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("PostId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "PostId");
+
+ b.HasIndex("PostId");
+
+ b.ToTable("RatedPosts");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.UserRate", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<bool>("Liked")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("PostId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("PostId");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("UserRates");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Role", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedName")
+ .IsUnique()
+ .HasDatabaseName("RoleNameIndex");
+
+ b.ToTable("AspNetRoles");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Technology", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.HasKey("Id");
+
+ b.ToTable("Technologies");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Property<Guid>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("uuid");
+
+ b.Property<int>("AccessFailedCount")
+ .HasColumnType("integer");
+
+ b.Property<string>("ConcurrencyStamp")
+ .IsConcurrencyToken()
+ .HasColumnType("text");
+
+ b.Property<string>("Email")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<bool>("EmailConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("FirstName")
+ .HasColumnType("text");
+
+ b.Property<string>("LastName")
+ .HasColumnType("text");
+
+ b.Property<bool>("LockoutEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<DateTimeOffset?>("LockoutEnd")
+ .HasColumnType("timestamp with time zone");
+
+ b.Property<string>("NormalizedEmail")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("NormalizedUserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.Property<string>("PasswordHash")
+ .HasColumnType("text");
+
+ b.Property<string>("PhoneNumber")
+ .HasColumnType("text");
+
+ b.Property<bool>("PhoneNumberConfirmed")
+ .HasColumnType("boolean");
+
+ b.Property<string>("SecurityStamp")
+ .HasColumnType("text");
+
+ b.Property<bool>("TwoFactorEnabled")
+ .HasColumnType("boolean");
+
+ b.Property<Guid?>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("UserName")
+ .HasMaxLength(256)
+ .HasColumnType("character varying(256)");
+
+ b.HasKey("Id");
+
+ b.HasIndex("NormalizedEmail")
+ .HasDatabaseName("EmailIndex");
+
+ b.HasIndex("NormalizedUserName")
+ .IsUnique()
+ .HasDatabaseName("UserNameIndex");
+
+ b.HasIndex("UserId");
+
+ b.HasIndex("UserName")
+ .IsUnique();
+
+ b.ToTable("AspNetUsers");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.Property<Guid>("LanguagesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LanguagesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("LanguageUser");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetRoleClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.Property<int>("Id")
+ .ValueGeneratedOnAdd()
+ .HasColumnType("integer")
+ .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
+
+ b.Property<string>("ClaimType")
+ .HasColumnType("text");
+
+ b.Property<string>("ClaimValue")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("Id");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserClaims");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderKey")
+ .HasColumnType("text");
+
+ b.Property<string>("ProviderDisplayName")
+ .HasColumnType("text");
+
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.HasKey("LoginProvider", "ProviderKey");
+
+ b.HasIndex("UserId");
+
+ b.ToTable("AspNetUserLogins");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("RoleId")
+ .HasColumnType("uuid");
+
+ b.HasKey("UserId", "RoleId");
+
+ b.HasIndex("RoleId");
+
+ b.ToTable("AspNetUserRoles");
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.Property<Guid>("UserId")
+ .HasColumnType("uuid");
+
+ b.Property<string>("LoginProvider")
+ .HasColumnType("text");
+
+ b.Property<string>("Name")
+ .HasColumnType("text");
+
+ b.Property<string>("Value")
+ .HasColumnType("text");
+
+ b.HasKey("UserId", "LoginProvider", "Name");
+
+ b.ToTable("AspNetUserTokens");
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.Property<Guid>("RolesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("RolesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("RoleUser");
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.Property<Guid>("TechnologiesId")
+ .HasColumnType("uuid");
+
+ b.Property<Guid>("UsersId")
+ .HasColumnType("uuid");
+
+ b.HasKey("TechnologiesId", "UsersId");
+
+ b.HasIndex("UsersId");
+
+ b.ToTable("TechnologyUser");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Comment", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Comments")
+ .HasForeignKey("CreatorId");
+
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Comments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Creator");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "Creator")
+ .WithMany("Posts")
+ .HasForeignKey("CreatorId");
+
+ b.Navigation("Creator");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithOne("ProfilePicture")
+ .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Rating", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithOne("Rating")
+ .HasForeignKey("DevHive.Data.Models.Rating", "PostId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.PostAttachments", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany("Attachments")
+ .HasForeignKey("PostId");
+
+ b.Navigation("Post");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.RatedPost", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany()
+ .HasForeignKey("PostId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany("RatedPosts")
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Relational.UserRate", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Post", "Post")
+ .WithMany()
+ .HasForeignKey("PostId");
+
+ b.HasOne("DevHive.Data.Models.User", "User")
+ .WithMany()
+ .HasForeignKey("UserId");
+
+ b.Navigation("Post");
+
+ b.Navigation("User");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany("Friends")
+ .HasForeignKey("UserId");
+ });
+
+ modelBuilder.Entity("LanguageUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Language", null)
+ .WithMany()
+ .HasForeignKey("LanguagesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RoleId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<System.Guid>", b =>
+ {
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UserId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("RoleUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Role", null)
+ .WithMany()
+ .HasForeignKey("RolesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("TechnologyUser", b =>
+ {
+ b.HasOne("DevHive.Data.Models.Technology", null)
+ .WithMany()
+ .HasForeignKey("TechnologiesId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+
+ b.HasOne("DevHive.Data.Models.User", null)
+ .WithMany()
+ .HasForeignKey("UsersId")
+ .OnDelete(DeleteBehavior.Cascade)
+ .IsRequired();
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.Post", b =>
+ {
+ b.Navigation("Attachments");
+
+ b.Navigation("Comments");
+
+ b.Navigation("Rating");
+ });
+
+ modelBuilder.Entity("DevHive.Data.Models.User", b =>
+ {
+ b.Navigation("Comments");
+
+ b.Navigation("Friends");
+
+ b.Navigation("Posts");
+
+ b.Navigation("ProfilePicture");
+
+ b.Navigation("RatedPosts");
+ });
+#pragma warning restore 612, 618
+ }
+ }
+}
diff --git a/src/Data/DevHive.Data/Repositories/RoleRepository.cs b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
index 7d07c1b..99e0634 100644
--- a/src/Data/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/Data/DevHive.Data/Repositories/RoleRepository.cs
@@ -2,53 +2,54 @@ using System;
using System.Threading.Tasks;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
+using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace DevHive.Data.Repositories
{
public class RoleRepository : BaseRepository<Role>, IRoleRepository
{
- private readonly DevHiveContext _context;
+ private readonly RoleManager<Role> _roleManager;
- public RoleRepository(DevHiveContext context)
+ public RoleRepository(DevHiveContext context, RoleManager<Role> roleManager)
: base(context)
{
- this._context = context;
+ this._roleManager = roleManager;
}
+ #region Create
+ public override async Task<bool> AddAsync(Role entity)
+ {
+ IdentityResult result = await this._roleManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
#region Read
public async Task<Role> GetByNameAsync(string name)
{
- return await this._context.Roles
- .FirstOrDefaultAsync(x => x.Name == name);
+ return await this._roleManager.FindByNameAsync(name);
}
#endregion
public override async Task<bool> EditAsync(Guid id, Role newEntity)
{
- Role role = await this.GetByIdAsync(id);
-
- this._context
- .Entry(role)
- .CurrentValues
- .SetValues(newEntity);
+ newEntity.Id = id;
+ IdentityResult result = await this._roleManager.UpdateAsync(newEntity);
- return await this.SaveChangesAsync();
+ return result.Succeeded;
}
#region Validations
public async Task<bool> DoesNameExist(string name)
{
- return await this._context.Roles
- .AsNoTracking()
- .AnyAsync(r => r.Name == name);
+ return await this._roleManager.RoleExistsAsync(name);
}
public async Task<bool> DoesRoleExist(Guid id)
{
- return await this._context.Roles
- .AsNoTracking()
- .AnyAsync(r => r.Id == id);
+ return await this._roleManager.Roles.AnyAsync(r => r.Id == id);
}
#endregion
}
diff --git a/src/Data/DevHive.Data/Repositories/UserRepository.cs b/src/Data/DevHive.Data/Repositories/UserRepository.cs
index 9fa6eca..d570480 100644
--- a/src/Data/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/Data/DevHive.Data/Repositories/UserRepository.cs
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
using System.Threading.Tasks;
-using AutoMapper.Mappers;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
-using DevHive.Data.Models.Relational;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -14,18 +10,42 @@ namespace DevHive.Data.Repositories
{
public class UserRepository : BaseRepository<User>, IUserRepository
{
- private readonly DevHiveContext _context;
+ private readonly UserManager<User> _userManager;
+ private readonly RoleManager<Role> _roleManager;
- public UserRepository(DevHiveContext context)
+ public UserRepository(DevHiveContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
: base(context)
{
- this._context = context;
+ this._userManager = userManager;
+ this._roleManager = roleManager;
}
+ #region Create
+ public override async Task<bool> AddAsync(User entity)
+ {
+ entity.PasswordHash = this._userManager.PasswordHasher.HashPassword(entity, entity.PasswordHash).ToString();
+ IdentityResult result = await this._userManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+
+ public async Task<bool> AddRoleToUser(User user, string roleName)
+ {
+ bool succeeded = (await this._userManager.AddToRoleAsync(user, roleName)).Succeeded;
+ if (succeeded)
+ {
+ user.Roles.Add(await this._roleManager.FindByNameAsync(roleName));
+ succeeded = await this.SaveChangesAsync();
+ }
+
+ return succeeded;
+ }
+ #endregion
+
#region Read
public override async Task<User> GetByIdAsync(Guid id)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -37,7 +57,7 @@ namespace DevHive.Data.Repositories
public async Task<User> GetByUsernameAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -49,6 +69,14 @@ namespace DevHive.Data.Repositories
#endregion
#region Update
+ public override async Task<bool> EditAsync(Guid id, User newEntity)
+ {
+ newEntity.Id = id;
+ IdentityResult result = await this._userManager.UpdateAsync(newEntity);
+
+ return result.Succeeded;
+ }
+
public async Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl)
{
User user = await this.GetByIdAsync(userId);
@@ -59,24 +87,41 @@ namespace DevHive.Data.Repositories
}
#endregion
+ #region Delete
+ public override async Task<bool> DeleteAsync(User entity)
+ {
+ IdentityResult result = await this._userManager.DeleteAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
#region Validations
+ public async Task<bool> VerifyPassword(User user, string password)
+ {
+ return await this._userManager.CheckPasswordAsync(user, password);
+ }
+
+ public async Task<bool> IsInRoleAsync(User user, string roleName)
+ {
+ return await this._userManager.IsInRoleAsync(user, roleName);
+ }
+
public async Task<bool> DoesUserExistAsync(Guid id)
{
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(x => x.Id == id);
+ return await this._userManager.Users.AnyAsync(x => x.Id == id);
}
public async Task<bool> DoesUsernameExistAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.UserName == username);
}
public async Task<bool> DoesEmailExistAsync(string email)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.Email == email);
}
@@ -87,7 +132,7 @@ namespace DevHive.Data.Repositories
foreach (var username in usernames)
{
- if (!await this._context.Users.AnyAsync(x => x.UserName == username))
+ if (!await this.DoesUsernameExistAsync(username))
{
valid = false;
break;
@@ -96,11 +141,10 @@ namespace DevHive.Data.Repositories
return valid;
}
- public bool DoesUserHaveThisUsername(Guid id, string username)
+ public async Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username)
{
- return this._context.Users
- .AsNoTracking()
- .Any(x => x.Id == id &&
+ return await this._userManager.Users
+ .AnyAsync(x => x.Id == id &&
x.UserName == username);
}
#endregion
diff --git a/src/Services/DevHive.Services.Tests/UserService.Tests.cs b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
index ce997c1..550106f 100644
--- a/src/Services/DevHive.Services.Tests/UserService.Tests.cs
+++ b/src/Services/DevHive.Services.Tests/UserService.Tests.cs
@@ -274,7 +274,7 @@ namespace DevHive.Services.Tests
//
// this.UserRepositoryMock.Setup(p => p.DoesUserExistAsync(It.IsAny<Guid>())).Returns(Task.FromResult(true));
// this.UserRepositoryMock.Setup(p => p.DoesUsernameExistAsync(It.IsAny<string>())).Returns(Task.FromResult(false));
- // this.UserRepositoryMock.Setup(p => p.DoesUserHaveThisUsername(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true);
+ // this.UserRepositoryMock.Setup(p => p.DoesUserHaveThisUsernameAsync(It.IsAny<Guid>(), It.IsAny<string>())).Returns(true);
// this.UserRepositoryMock.Setup(p => p.EditAsync(It.IsAny<Guid>(), It.IsAny<User>())).Returns(Task.FromResult(shouldPass));
// this.UserRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny<Guid>())).Returns(Task.FromResult(user));
// this.MapperMock.Setup(p => p.Map<User>(It.IsAny<UpdateUserServiceModel>())).Returns(user);
diff --git a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
index 043fcb7..99b41a8 100644
--- a/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
+++ b/src/Services/DevHive.Services/Configurations/Mapping/UserMappings.cs
@@ -1,8 +1,6 @@
using DevHive.Data.Models;
using AutoMapper;
using DevHive.Services.Models.User;
-using DevHive.Common.Models.Misc;
-using DevHive.Data.Models.Relational;
namespace DevHive.Services.Configurations.Mapping
{
@@ -11,19 +9,18 @@ namespace DevHive.Services.Configurations.Mapping
public UserMappings()
{
CreateMap<UserServiceModel, User>();
- CreateMap<RegisterServiceModel, User>();
+ CreateMap<RegisterServiceModel, User>()
+ .ForMember(dest => dest.PasswordHash, src => src.MapFrom(p => p.Password));
CreateMap<FriendServiceModel, User>()
.ForMember(dest => dest.Friends, src => src.Ignore());
CreateMap<UpdateUserServiceModel, User>()
- .ForMember(dest => dest.Friends, src => src.Ignore())
- .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password));
+ .ForMember(dest => dest.Friends, src => src.Ignore());
CreateMap<UpdateFriendServiceModel, User>();
CreateMap<User, UserServiceModel>()
.ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL))
.ForMember(dest => dest.Friends, src => src.MapFrom(p => p.Friends));
CreateMap<User, UpdateUserServiceModel>()
- .ForMember(x => x.Password, opt => opt.Ignore())
.ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL));
CreateMap<User, FriendServiceModel>();
}
diff --git a/src/Services/DevHive.Services/Services/RoleService.cs b/src/Services/DevHive.Services/Services/RoleService.cs
index 1285421..a5da759 100644
--- a/src/Services/DevHive.Services/Services/RoleService.cs
+++ b/src/Services/DevHive.Services/Services/RoleService.cs
@@ -39,8 +39,8 @@ namespace DevHive.Services.Services
public async Task<RoleServiceModel> GetRoleById(Guid id)
{
- Role role = await this._roleRepository.GetByIdAsync(id)
- ?? throw new ArgumentException("Role does not exist!");
+ Role role = await this._roleRepository.GetByIdAsync(id) ??
+ throw new ArgumentException("Role does not exist!");
return this._roleMapper.Map<RoleServiceModel>(role);
}
diff --git a/src/Services/DevHive.Services/Services/UserService.cs b/src/Services/DevHive.Services/Services/UserService.cs
index e31eb8d..dfd45cc 100644
--- a/src/Services/DevHive.Services/Services/UserService.cs
+++ b/src/Services/DevHive.Services/Services/UserService.cs
@@ -15,7 +15,7 @@ using DevHive.Data.Interfaces;
using System.Linq;
using DevHive.Common.Models.Misc;
using Microsoft.AspNetCore.Http;
-using Microsoft.AspNetCore.Identity;
+using Newtonsoft.Json;
namespace DevHive.Services.Services
{
@@ -25,8 +25,6 @@ namespace DevHive.Services.Services
private readonly IRoleRepository _roleRepository;
private readonly ILanguageRepository _languageRepository;
private readonly ITechnologyRepository _technologyRepository;
- private readonly UserManager<User> _userManager;
- private readonly RoleManager<Role> _roleManager;
private readonly IMapper _userMapper;
private readonly JwtOptions _jwtOptions;
private readonly ICloudService _cloudService;
@@ -35,8 +33,6 @@ namespace DevHive.Services.Services
ILanguageRepository languageRepository,
IRoleRepository roleRepository,
ITechnologyRepository technologyRepository,
- UserManager<User> userManager,
- RoleManager<Role> roleManager,
IMapper mapper,
JwtOptions jwtOptions,
ICloudService cloudService)
@@ -47,8 +43,6 @@ namespace DevHive.Services.Services
this._jwtOptions = jwtOptions;
this._languageRepository = languageRepository;
this._technologyRepository = technologyRepository;
- this._userManager = userManager;
- this._roleManager = roleManager;
this._cloudService = cloudService;
}
@@ -64,7 +58,7 @@ namespace DevHive.Services.Services
User user = await this._userRepository.GetByUsernameAsync(loginModel.UserName);
- if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password))
+ if (!await this._userRepository.VerifyPassword(user, loginModel.Password))
throw new ArgumentException("Incorrect password!");
return new TokenModel(WriteJWTSecurityToken(user.Id, user.UserName, user.Roles));
@@ -82,22 +76,16 @@ namespace DevHive.Services.Services
throw new ArgumentException("Email already exists!");
User user = this._userMapper.Map<User>(registerModel);
- user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password);
- user.ProfilePicture = new ProfilePicture() { PictureURL = "/assets/images/feed/profile-pic.png" };
- // Make sure the default role exists
- //TODO: Move when project starts
- if (!await this._roleRepository.DoesNameExist(Role.DefaultRole))
- await this._roleRepository.AddAsync(new Role { Name = Role.DefaultRole });
+ bool userResult = await this._userRepository.AddAsync(user);
+ bool roleResult = await this._userRepository.AddRoleToUser(user, Role.DefaultRole);
- user.Roles.Add(await this._roleRepository.GetByNameAsync(Role.DefaultRole));
-
- IdentityResult userResult = await this._userManager.CreateAsync(user);
- User createdUser = await this._userRepository.GetByUsernameAsync(registerModel.UserName);
-
- if (!userResult.Succeeded)
+ if (!userResult)
throw new ArgumentException("Unable to create a user");
+ if (!roleResult)
+ throw new ArgumentException("Unable to add role to user");
+ User createdUser = await this._userRepository.GetByUsernameAsync(registerModel.UserName);
return new TokenModel(WriteJWTSecurityToken(createdUser.Id, createdUser.UserName, createdUser.Roles));
}
#endregion
@@ -125,20 +113,20 @@ namespace DevHive.Services.Services
{
await this.ValidateUserOnUpdate(updateUserServiceModel);
- User currentUser = await this._userRepository.GetByIdAsync(updateUserServiceModel.Id);
- await this.PopulateUserModel(currentUser, updateUserServiceModel);
+ User user = await this._userRepository.GetByIdAsync(updateUserServiceModel.Id);
+ await this.PopulateUserModel(user, updateUserServiceModel);
if (updateUserServiceModel.Friends.Count > 0)
- await this.CreateRelationToFriends(currentUser, updateUserServiceModel.Friends.ToList());
+ await this.CreateRelationToFriends(user, updateUserServiceModel.Friends.ToList());
else
- currentUser.Friends.Clear();
+ user.Friends.Clear();
- IdentityResult result = await this._userManager.UpdateAsync(currentUser);
+ bool result = await this._userRepository.EditAsync(user.Id, user);
- if (!result.Succeeded)
+ if (!result)
throw new InvalidOperationException("Unable to edit user!");
- User newUser = await this._userRepository.GetByIdAsync(currentUser.Id);
+ User newUser = await this._userRepository.GetByIdAsync(user.Id);
return this._userMapper.Map<UserServiceModel>(newUser);
}
@@ -175,16 +163,14 @@ namespace DevHive.Services.Services
throw new ArgumentException("User does not exist!");
User user = await this._userRepository.GetByIdAsync(id);
- IdentityResult result = await this._userManager.DeleteAsync(user);
-
- return result.Succeeded;
+ return await this._userRepository.DeleteAsync(user);
}
#endregion
#region Validations
/// <summary>
/// Checks whether the given user, gotten by the "id" property,
- /// is the same user as the one in the token (uness the user in the token has the admin role)
+ /// is the same user as the one in the token (unless the user in the token has the admin role)
/// and the roles in the token are the same as those in the user, gotten by the id in the token
/// </summary>
public async Task<bool> ValidJWT(Guid id, string rawTokenData)
@@ -192,15 +178,16 @@ namespace DevHive.Services.Services
// There is authorization name in the beginning, i.e. "Bearer eyJh..."
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
- Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims).First());
- List<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
+ Guid jwtUserID = new(UserService.GetClaimTypeValues("ID", jwt.Claims).First());
+ List<string> jwtRoleNames = UserService.GetClaimTypeValues("role", jwt.Claims);
User user = await this._userRepository.GetByIdAsync(jwtUserID)
?? throw new ArgumentException("User does not exist!");
- /* Check if user is trying to do something to himself, unless he's an admin */
+ /* Check if he is an admin */
+ if (user.Roles.Any(x => x.Name == Role.AdminRole))
+ return true;
- /* Check roles */
if (!jwtRoleNames.Contains(Role.AdminRole) && user.Id != id)
return false;
@@ -219,7 +206,7 @@ namespace DevHive.Services.Services
/// <summary>
/// Returns all values from a given claim type
/// </summary>
- private List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
+ private static List<string> GetClaimTypeValues(string type, IEnumerable<Claim> claims)
{
List<string> toReturn = new();
@@ -243,7 +230,7 @@ namespace DevHive.Services.Services
if (updateUserServiceModel.Friends.Any(x => x.UserName == updateUserServiceModel.UserName))
throw new ArgumentException("You cant add yourself as a friend(sry, bro)!");
- if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName)
+ if (!await this._userRepository.DoesUserHaveThisUsernameAsync(updateUserServiceModel.Id, updateUserServiceModel.UserName)
&& await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName))
throw new ArgumentException("Username already exists!");
@@ -296,19 +283,16 @@ namespace DevHive.Services.Services
if (!await this._roleRepository.DoesNameExist(Role.AdminRole))
{
- Role adminRole = new()
- {
- Name = Role.AdminRole
- };
+ Role adminRole = new() { Name = Role.AdminRole };
adminRole.Users.Add(user);
await this._roleRepository.AddAsync(adminRole);
}
- Role admin = await this._roleManager.FindByNameAsync(Role.AdminRole);
+ Role admin = await this._roleRepository.GetByNameAsync(Role.AdminRole);
user.Roles.Add(admin);
- await this._userManager.UpdateAsync(user);
+ await this._userRepository.EditAsync(user.Id, user);
User newUser = await this._userRepository.GetByIdAsync(userId);
@@ -323,7 +307,7 @@ namespace DevHive.Services.Services
user.Email = updateUserServiceModel.Email;
//Do NOT allow a user to change his roles, unless he is an Admin
- bool isAdmin = await this._userManager.IsInRoleAsync(user, Role.AdminRole);
+ bool isAdmin = await this._userRepository.IsInRoleAsync(user, Role.AdminRole);
if (isAdmin)
{
@@ -371,7 +355,7 @@ namespace DevHive.Services.Services
user.Friends.Add(amigo);
amigo.Friends.Add(user);
- await this._userManager.UpdateAsync(amigo);
+ await this._userRepository.EditAsync(amigo.Id, amigo);
}
}
#endregion
diff --git a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
index 84108ae..687e83f 100644
--- a/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
+++ b/src/Web/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs
@@ -60,7 +60,7 @@ namespace DevHive.Web.Configurations.Extensions
});
}
- public static async Task UseDatabaseConfiguration(this IApplicationBuilder app)
+ public static void UseDatabaseConfiguration(this IApplicationBuilder app)
{
app.UseHttpsRedirection();
app.UseRouting();
@@ -75,21 +75,21 @@ namespace DevHive.Web.Configurations.Extensions
var roleManager = (RoleManager<Role>)serviceScope.ServiceProvider.GetService(typeof(RoleManager<Role>));
- if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.DefaultRole))
+ if (!dbContext.Roles.Any(x => x.Name == Role.DefaultRole))
{
Role defaultRole = new() { Name = Role.DefaultRole };
- await roleManager.CreateAsync(defaultRole);
+ roleManager.CreateAsync(defaultRole).Wait();
}
- if (!await dbContext.Roles.AnyAsync(x => x.Name == Role.AdminRole))
+ if (!dbContext.Roles.Any(x => x.Name == Role.AdminRole))
{
Role adminRole = new() { Name = Role.AdminRole };
- await roleManager.CreateAsync(adminRole);
+ roleManager.CreateAsync(adminRole).Wait();
}
- await dbContext.SaveChangesAsync();
+ dbContext.SaveChanges();
}
}
}
diff --git a/src/Web/DevHive.Web/Controllers/UserController.cs b/src/Web/DevHive.Web/Controllers/UserController.cs
index 25faa8a..214fba7 100644
--- a/src/Web/DevHive.Web/Controllers/UserController.cs
+++ b/src/Web/DevHive.Web/Controllers/UserController.cs
@@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using DevHive.Common.Models.Identity;
using DevHive.Services.Interfaces;
-using Microsoft.Extensions.Hosting;
namespace DevHive.Web.Controllers
{
@@ -32,8 +31,8 @@ namespace DevHive.Web.Controllers
{
LoginServiceModel loginServiceModel = this._userMapper.Map<LoginServiceModel>(loginModel);
- TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel);
- TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
+ TokenModel tokenModel = await this._userService.LoginUser(loginServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel);
return new OkObjectResult(tokenWebModel);
}
@@ -45,8 +44,8 @@ namespace DevHive.Web.Controllers
{
RegisterServiceModel registerServiceModel = this._userMapper.Map<RegisterServiceModel>(registerModel);
- TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel);
- TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(TokenModel);
+ TokenModel tokenModel = await this._userService.RegisterUser(registerServiceModel);
+ TokenWebModel tokenWebModel = this._userMapper.Map<TokenWebModel>(tokenModel);
return new CreatedResult("Register", tokenWebModel);
}
diff --git a/src/Web/DevHive.Web/Startup.cs b/src/Web/DevHive.Web/Startup.cs
index 46521cf..dbcf131 100644
--- a/src/Web/DevHive.Web/Startup.cs
+++ b/src/Web/DevHive.Web/Startup.cs
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using DevHive.Web.Configurations.Extensions;
using Newtonsoft.Json;
+using System.Threading.Tasks;
namespace DevHive.Web
{