diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-21 22:13:16 +0200 |
| commit | 13a2ceda912f961a232c87236f1b29aa29bb6160 (patch) | |
| tree | 59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Data | |
| parent | a47ea20ab91017da53437f750ed8e0f939f5cdba (diff) | |
| parent | bda98b96433d7a9952524fab4ec65f96998b55de (diff) | |
| download | DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar.gz DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.zip | |
Merge branch 'refactor_user_updating' into dev
Diffstat (limited to 'src/DevHive.Data')
26 files changed, 745 insertions, 1550 deletions
diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 10fd004..c1bda49 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -12,6 +12,7 @@ namespace DevHive.Data public DbSet<Technology> Technologies { get; set; } public DbSet<Language> Languages { get; set; } + public DbSet<Post> Posts { get; set; } public DbSet<Comment> Comments { get; set; } protected override void OnModelCreating(ModelBuilder builder) diff --git a/src/DevHive.Data/Interfaces/Models/ILanguage.cs b/src/DevHive.Data/Interfaces/Models/ILanguage.cs index f757a3f..b77d5ae 100644 --- a/src/DevHive.Data/Interfaces/Models/ILanguage.cs +++ b/src/DevHive.Data/Interfaces/Models/ILanguage.cs @@ -1,7 +1,12 @@ +using System.Collections.Generic; +using DevHive.Data.Models; + namespace DevHive.Data.Interfaces.Models { public interface ILanguage : IModel { string Name { get; set; } + HashSet<User> Users { get; set; } + } } diff --git a/src/DevHive.Data/Interfaces/Models/IRole.cs b/src/DevHive.Data/Interfaces/Models/IRole.cs index 0623f07..c8b7068 100644 --- a/src/DevHive.Data/Interfaces/Models/IRole.cs +++ b/src/DevHive.Data/Interfaces/Models/IRole.cs @@ -5,6 +5,6 @@ namespace DevHive.Data.Interfaces.Models { public interface IRole { - List<User> Users { get; set; } + HashSet<User> Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/ITechnology.cs b/src/DevHive.Data/Interfaces/Models/ITechnology.cs index 9bd97f9..153f75f 100644 --- a/src/DevHive.Data/Interfaces/Models/ITechnology.cs +++ b/src/DevHive.Data/Interfaces/Models/ITechnology.cs @@ -1,7 +1,11 @@ +using System.Collections.Generic; +using DevHive.Data.Models; + namespace DevHive.Data.Interfaces.Models { public interface ITechnology : IModel { string Name { get; set; } + HashSet<User> Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index ef8c927..08ce385 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,9 +8,9 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList<Language> Languages { get; set; } - IList<Technology> Technologies { get; set; } - IList<Role> Roles { get; set; } - IList<User> Friends { get; set; } + HashSet<Language> Languages { get; set; } + HashSet<Technology> Technologies { get; set; } + HashSet<Role> Roles { get; set; } + HashSet<User> Friends { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs index 913d8c4..7a9c02e 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs @@ -9,12 +9,16 @@ namespace DevHive.Data.Interfaces.Repositories { Task<bool> AddCommentAsync(Comment entity); + Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); + Task<Comment> GetCommentByIdAsync(Guid id); + Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); Task<bool> EditCommentAsync(Comment newEntity); Task<bool> DeleteCommentAsync(Comment entity); Task<bool> DoesCommentExist(Guid id); + Task<bool> DoesPostExist(Guid postId); } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs index 40a78de..d9f7c7a 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs @@ -18,4 +18,4 @@ namespace DevHive.Data.Repositories.Interfaces //Delete Entity from database Task<bool> DeleteAsync(TEntity entity); } -}
\ No newline at end of file +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index eca6adb..c29669d 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -8,24 +8,15 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IUserRepository : IRepository<User> { - Task<bool> AddFriendToUserAsync(User user, User friend); - Task<bool> AddLanguageToUserAsync(User user, Language language); - Task<bool> AddTechnologyToUserAsync(User user, Technology technology); - + //Read Task<User> GetByUsernameAsync(string username); Language GetUserLanguage(User user, Language language); - IList<Language> GetUserLanguages(User user); - IList<Technology> GetUserTechnologies(User user); + HashSet<Language> GetUserLanguages(User user); + HashSet<Technology> GetUserTechnologies(User user); Technology GetUserTechnology(User user, Technology technology); IEnumerable<User> QueryAll(); - Task<bool> EditUserLanguageAsync(User user, Language oldLang, Language newLang); - Task<bool> EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech); - - Task<bool> RemoveFriendAsync(User user, User friend); - Task<bool> RemoveLanguageFromUserAsync(User user, Language language); - Task<bool> RemoveTechnologyFromUserAsync(User user, Technology technology); - + //Validations Task<bool> DoesEmailExistAsync(string email); Task<bool> DoesUserExistAsync(Guid id); Task<bool> DoesUserHaveThisFriendAsync(Guid userId, Guid friendId); diff --git a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.Designer.cs b/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.Designer.cs deleted file mode 100644 index dd0e8c7..0000000 --- a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.Designer.cs +++ /dev/null @@ -1,357 +0,0 @@ -// <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("20201219141035_DbContext_Moved")] - partial class DbContext_Moved - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .UseIdentityByDefaultColumns() - .HasAnnotation("Relational:MaxIdentifierLength", 63) - .HasAnnotation("ProductVersion", "5.0.1"); - - 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.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>("ProfilePicture") - .HasColumnType("text"); - - 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("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b => - { - b.Property<int>("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .UseIdentityByDefaultColumn(); - - 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") - .UseIdentityByDefaultColumn(); - - 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("DevHive.Data.Models.User", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Friends") - .HasForeignKey("UserId"); - }); - - 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("DevHive.Data.Models.User", b => - { - b.Navigation("Friends"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs b/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs deleted file mode 100644 index ccc5e91..0000000 --- a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs +++ /dev/null @@ -1,301 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -namespace DevHive.Data.Migrations -{ - public partial class DbContext_Moved : 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), - ProfilePicture = 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: "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.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_RoleUser_UsersId", - table: "RoleUser", - column: "UsersId"); - } - - 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: "Languages"); - - migrationBuilder.DropTable( - name: "RoleUser"); - - migrationBuilder.DropTable( - name: "Technologies"); - - migrationBuilder.DropTable( - name: "AspNetRoles"); - - migrationBuilder.DropTable( - name: "AspNetUsers"); - } - } -} diff --git a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs deleted file mode 100644 index 0df3199..0000000 --- a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.Designer.cs +++ /dev/null @@ -1,377 +0,0 @@ -// <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("20201230154737_CommentMigration")] - partial class CommentMigration - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .UseIdentityByDefaultColumns() - .HasAnnotation("Relational:MaxIdentifierLength", 63) - .HasAnnotation("ProductVersion", "5.0.1"); - - modelBuilder.Entity("DevHive.Data.Models.Comment", b => - { - b.Property<Guid>("Id") - .ValueGeneratedOnAdd() - .HasColumnType("uuid"); - - b.Property<DateTime>("Date") - .HasColumnType("timestamp without time zone"); - - b.Property<string>("Message") - .HasColumnType("text"); - - b.Property<Guid>("UserId") - .HasColumnType("uuid"); - - b.HasKey("Id"); - - 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.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>("ProfilePicture") - .HasColumnType("text"); - - 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("Microsoft.AspNetCore.Identity.IdentityRoleClaim<System.Guid>", b => - { - b.Property<int>("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .UseIdentityByDefaultColumn(); - - 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") - .UseIdentityByDefaultColumn(); - - 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("DevHive.Data.Models.User", b => - { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Friends") - .HasForeignKey("UserId"); - }); - - 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("DevHive.Data.Models.User", b => - { - b.Navigation("Friends"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs deleted file mode 100644 index a442bfa..0000000 --- a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace DevHive.Data.Migrations -{ - public partial class CommentMigration : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Comments", - columns: table => new - { - Id = table.Column<Guid>(type: "uuid", nullable: false), - UserId = table.Column<Guid>(type: "uuid", nullable: false), - Message = table.Column<string>(type: "text", nullable: true), - Date = table.Column<DateTime>(type: "timestamp without time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Comments", x => x.Id); - }); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Comments"); - } - } -} diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs deleted file mode 100644 index d698f10..0000000 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -namespace DevHive.Data.Migrations -{ - public partial class User_Implements_Languages : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.RenameColumn( - name: "UserId", - table: "Comments", - newName: "IssuerId"); - - migrationBuilder.RenameColumn( - name: "Date", - table: "Comments", - newName: "TimeCreated"); - - migrationBuilder.RenameColumn( - name: "ProfilePicture", - table: "AspNetUsers", - newName: "ProfilePictureUrl"); - - migrationBuilder.AddColumn<Guid>( - name: "UserId", - table: "Technologies", - type: "uuid", - nullable: true); - - migrationBuilder.AddColumn<Guid>( - name: "UserId", - table: "Languages", - type: "uuid", - nullable: true); - - migrationBuilder.CreateIndex( - name: "IX_Technologies_UserId", - table: "Technologies", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_Languages_UserId", - table: "Languages", - column: "UserId"); - - migrationBuilder.AddForeignKey( - name: "FK_Languages_AspNetUsers_UserId", - table: "Languages", - column: "UserId", - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - - migrationBuilder.AddForeignKey( - name: "FK_Technologies_AspNetUsers_UserId", - table: "Technologies", - column: "UserId", - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropForeignKey( - name: "FK_Languages_AspNetUsers_UserId", - table: "Languages"); - - migrationBuilder.DropForeignKey( - name: "FK_Technologies_AspNetUsers_UserId", - table: "Technologies"); - - migrationBuilder.DropIndex( - name: "IX_Technologies_UserId", - table: "Technologies"); - - migrationBuilder.DropIndex( - name: "IX_Languages_UserId", - table: "Languages"); - - migrationBuilder.DropColumn( - name: "UserId", - table: "Technologies"); - - migrationBuilder.DropColumn( - name: "UserId", - table: "Languages"); - - migrationBuilder.RenameColumn( - name: "TimeCreated", - table: "Comments", - newName: "Date"); - - migrationBuilder.RenameColumn( - name: "IssuerId", - table: "Comments", - newName: "UserId"); - - migrationBuilder.RenameColumn( - name: "ProfilePictureUrl", - table: "AspNetUsers", - newName: "ProfilePicture"); - } - } -} diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs index 1605b5b..7c7a092 100644 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs +++ b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs @@ -10,8 +10,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { [DbContext(typeof(DevHiveContext))] - [Migration("20210112111416_User_Implements_Languages")] - partial class User_Implements_Languages + [Migration("20210121083441_UserRefactor")] + partial class UserRefactor { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -33,11 +33,16 @@ namespace DevHive.Data.Migrations 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("PostId"); + b.ToTable("Comments"); }); @@ -50,14 +55,29 @@ namespace DevHive.Data.Migrations b.Property<string>("Name") .HasColumnType("text"); - b.Property<Guid?>("UserId") + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property<Guid>("Id") + .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.HasKey("Id"); + b.Property<Guid>("IssuerId") + .HasColumnType("uuid"); - b.HasIndex("UserId"); + b.Property<string>("Message") + .HasColumnType("text"); - b.ToTable("Languages"); + b.Property<DateTime>("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.ToTable("Posts"); }); modelBuilder.Entity("DevHive.Data.Models.Role", b => @@ -96,13 +116,8 @@ namespace DevHive.Data.Migrations b.Property<string>("Name") .HasColumnType("text"); - b.Property<Guid?>("UserId") - .HasColumnType("uuid"); - b.HasKey("Id"); - b.HasIndex("UserId"); - b.ToTable("Technologies"); }); @@ -188,6 +203,21 @@ namespace DevHive.Data.Migrations 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") @@ -304,18 +334,26 @@ namespace DevHive.Data.Migrations b.ToTable("RoleUser"); }); - modelBuilder.Entity("DevHive.Data.Models.Language", b => + modelBuilder.Entity("TechnologyUser", b => { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Languages") - .HasForeignKey("UserId"); + 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.Technology", b => + modelBuilder.Entity("DevHive.Data.Models.Comment", b => { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Technologies") - .HasForeignKey("UserId"); + b.HasOne("DevHive.Data.Models.Post", null) + .WithMany("Comments") + .HasForeignKey("PostId"); }); modelBuilder.Entity("DevHive.Data.Models.User", b => @@ -325,6 +363,21 @@ namespace DevHive.Data.Migrations .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) @@ -391,13 +444,29 @@ namespace DevHive.Data.Migrations .IsRequired(); }); - modelBuilder.Entity("DevHive.Data.Models.User", b => + modelBuilder.Entity("TechnologyUser", b => { - b.Navigation("Friends"); + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); - b.Navigation("Languages"); + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); - b.Navigation("Technologies"); + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs new file mode 100644 index 0000000..ea1af2e --- /dev/null +++ b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs @@ -0,0 +1,411 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + public partial class UserRefactor : 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), + ProfilePictureUrl = 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: "Posts", + columns: table => new + { + Id = table.Column<Guid>(type: "uuid", nullable: false), + IssuerId = table.Column<Guid>(type: "uuid", nullable: false), + TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false), + Message = table.Column<string>(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Posts", 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: "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: "Comments", + columns: table => new + { + Id = table.Column<Guid>(type: "uuid", nullable: false), + IssuerId = table.Column<Guid>(type: "uuid", nullable: false), + Message = table.Column<string>(type: "text", nullable: true), + TimeCreated = table.Column<DateTime>(type: "timestamp without time zone", nullable: false), + PostId = table.Column<Guid>(type: "uuid", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.Id); + table.ForeignKey( + name: "FK_Comments_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + 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.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_PostId", + table: "Comments", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_LanguageUser_UsersId", + table: "LanguageUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_RoleUser_UsersId", + table: "RoleUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_TechnologyUser_UsersId", + table: "TechnologyUser", + column: "UsersId"); + } + + 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: "RoleUser"); + + migrationBuilder.DropTable( + name: "TechnologyUser"); + + migrationBuilder.DropTable( + name: "Posts"); + + migrationBuilder.DropTable( + name: "Languages"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + + migrationBuilder.DropTable( + name: "Technologies"); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 7197c81..0727d33 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -31,11 +31,16 @@ namespace DevHive.Data.Migrations 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("PostId"); + b.ToTable("Comments"); }); @@ -48,14 +53,29 @@ namespace DevHive.Data.Migrations b.Property<string>("Name") .HasColumnType("text"); - b.Property<Guid?>("UserId") + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property<Guid>("Id") + .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.HasKey("Id"); + b.Property<Guid>("IssuerId") + .HasColumnType("uuid"); - b.HasIndex("UserId"); + b.Property<string>("Message") + .HasColumnType("text"); - b.ToTable("Languages"); + b.Property<DateTime>("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.ToTable("Posts"); }); modelBuilder.Entity("DevHive.Data.Models.Role", b => @@ -94,13 +114,8 @@ namespace DevHive.Data.Migrations b.Property<string>("Name") .HasColumnType("text"); - b.Property<Guid?>("UserId") - .HasColumnType("uuid"); - b.HasKey("Id"); - b.HasIndex("UserId"); - b.ToTable("Technologies"); }); @@ -186,6 +201,21 @@ namespace DevHive.Data.Migrations 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") @@ -302,18 +332,26 @@ namespace DevHive.Data.Migrations b.ToTable("RoleUser"); }); - modelBuilder.Entity("DevHive.Data.Models.Language", b => + modelBuilder.Entity("TechnologyUser", b => { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Languages") - .HasForeignKey("UserId"); + 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.Technology", b => + modelBuilder.Entity("DevHive.Data.Models.Comment", b => { - b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Technologies") - .HasForeignKey("UserId"); + b.HasOne("DevHive.Data.Models.Post", null) + .WithMany("Comments") + .HasForeignKey("PostId"); }); modelBuilder.Entity("DevHive.Data.Models.User", b => @@ -323,6 +361,21 @@ namespace DevHive.Data.Migrations .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) @@ -389,13 +442,29 @@ namespace DevHive.Data.Migrations .IsRequired(); }); - modelBuilder.Entity("DevHive.Data.Models.User", b => + modelBuilder.Entity("TechnologyUser", b => { - b.Navigation("Friends"); + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); - b.Navigation("Languages"); + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); - b.Navigation("Technologies"); + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index 2983107..f2b2786 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using DevHive.Data.Interfaces.Models; namespace DevHive.Data.Models @@ -7,5 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } + public HashSet<User> Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e63f007..e0855aa 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -12,6 +12,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public List<User> Users { get; set; } + public HashSet<User> Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 36cec32..a0728d5 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using DevHive.Data.Interfaces.Models; namespace DevHive.Data.Models @@ -7,5 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } + public HashSet<User> Users { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 64e138f..2ac7adf 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -18,15 +18,16 @@ namespace DevHive.Data.Models /// <summary> /// Languages that the user uses or is familiar with /// </summary> - public IList<Language> Languages { get; set; } + // [Unique] + public HashSet<Language> Languages { get; set; } /// <summary> /// Technologies that the user uses or is familiar with /// </summary> - public IList<Technology> Technologies { get; set; } + public HashSet<Technology> Technologies { get; set; } = new HashSet<Technology>(); - public IList<Role> Roles { get; set; } = new List<Role>(); + public HashSet<Role> Roles { get; set; } = new HashSet<Role>(); - public IList<User> Friends { get; set; } = new List<User>(); + public HashSet<User> Friends { get; set; } = new HashSet<User>(); } } diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs new file mode 100644 index 0000000..dabb35b --- /dev/null +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -0,0 +1,65 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Repositories.Interfaces; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class BaseRepository<TEntity> : IRepository<TEntity> + where TEntity : class + { + private readonly DbContext _context; + + public BaseRepository(DbContext context) + { + this._context = context; + this._context.ChangeTracker.AutoDetectChangesEnabled = false; + } + + public virtual async Task<bool> AddAsync(TEntity entity) + { + await this._context + .Set<TEntity>() + .AddAsync(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task<TEntity> GetByIdAsync(Guid id) + { + return await this._context + .Set<TEntity>() + .FindAsync(id); + } + + public virtual async Task<bool> EditAsync(TEntity newEntity) + { + // Old way(backup) + // User user = await this._context.Users + // .FirstOrDefaultAsync(x => x.Id == entity.Id); + + // this._context.Update(user); + // this._context.Entry(entity).CurrentValues.SetValues(entity); + + this._context + .Set<TEntity>() + .Update(newEntity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task<bool> DeleteAsync(TEntity entity) + { + this._context.Remove(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task<bool> SaveChangesAsync(DbContext context) + { + int result = await context.SaveChangesAsync(); + + return result >= 1; + } + } +} diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index e644fc4..d7ee609 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,75 +1,31 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : ILanguageRepository + public class LanguageRepository : BaseRepository<Language>, ILanguageRepository { private readonly DevHiveContext _context; public LanguageRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - - public async Task<bool> AddAsync(Language entity) - { - await this._context - .Set<Language>() - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Read - - public async Task<Language> GetByIdAsync(Guid id) - { - return await this._context - .Set<Language>() - .FindAsync(id); - } - public async Task<Language> GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion - #region Update - - public async Task<bool> EditAsync(Language newEntity) - { - this._context - .Set<Language>() - .Update(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - - public async Task<bool> DeleteAsync(Language entity) - { - this._context - .Set<Language>() - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Validations - public async Task<bool> DoesLanguageNameExistAsync(string languageName) { return await this._context.Languages diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 3be14e3..9230a2e 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,107 +1,84 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : BaseRepository<Post>, IPostRepository { private readonly DevHiveContext _context; public PostRepository(DevHiveContext context) + : base(context) { this._context = context; } - //Create - public async Task<bool> AddAsync(Post post) - { - await this._context - .Set<Post>() - .AddAsync(post); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - + #region Create public async Task<bool> AddCommentAsync(Comment entity) { - await this._context - .Set<Comment>() + await this._context.Comments .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion - //Read - public async Task<Post> GetByIdAsync(Guid id) + #region Read + public async Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { - return await this._context - .Set<Post>() - .FindAsync(id); + return await this._context.Posts + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); } public async Task<Comment> GetCommentByIdAsync(Guid id) { - return await this._context - .Set<Comment>() + return await this._context.Comments .FindAsync(id); } - //Update - public async Task<bool> EditAsync(Post newPost) + public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { - this._context - .Set<Post>() - .Update(newPost); - - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this._context.Comments + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); } + #endregion + #region Update public async Task<bool> EditCommentAsync(Comment newEntity) { - this._context - .Set<Comment>() + this._context.Comments .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - //Delete - public async Task<bool> DeleteAsync(Post post) - { - this._context - .Set<Post>() - .Remove(post); - - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion + #region Delete public async Task<bool> DeleteCommentAsync(Comment entity) { - this._context - .Set<Comment>() + this._context.Comments .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } + #endregion #region Validations - public async Task<bool> DoesPostExist(Guid postId) { - return await this._context - .Set<Post>() + return await this._context.Posts .AsNoTracking() .AnyAsync(r => r.Id == postId); } public async Task<bool> DoesCommentExist(Guid id) { - return await this._context - .Set<Comment>() + return await this._context.Comments .AsNoTracking() .AnyAsync(r => r.Id == id); } diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index ca3fb8b..156792d 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,83 +1,43 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRoleRepository + public class RoleRepository : BaseRepository<Role>, IRoleRepository { private readonly DevHiveContext _context; public RoleRepository(DevHiveContext context) + :base(context) { this._context = context; } - //Create - public async Task<bool> AddAsync(Role entity) - { - await this._context - .Set<Role>() - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - //Read - public async Task<Role> GetByIdAsync(Guid id) - { - return await this._context - .Set<Role>() - .FindAsync(id); - } - + #region Read public async Task<Role> GetByNameAsync(string name) { - return await this._context - .Set<Role>() + return await this._context.Roles .FirstOrDefaultAsync(x => x.Name == name); } + #endregion - //Update - public async Task<bool> EditAsync(Role newEntity) - { - Role role = await this.GetByIdAsync(newEntity.Id); - - this._context - .Entry(role) - .CurrentValues - .SetValues(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - //Delete - public async Task<bool> DeleteAsync(Role entity) - { - this._context - .Set<Role>() - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - + #region Validations public async Task<bool> DoesNameExist(string name) { - return await this._context - .Set<Role>() + return await this._context.Roles .AsNoTracking() .AnyAsync(r => r.Name == name); } public async Task<bool> DoesRoleExist(Guid id) { - return await this._context - .Set<Role>() + return await this._context.Roles .AsNoTracking() .AnyAsync(r => r.Id == id); } + #endregion } } diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 73827a7..83cc7aa 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,79 +1,34 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; - namespace DevHive.Data.Repositories { - public class TechnologyRepository : ITechnologyRepository + public class TechnologyRepository : BaseRepository<Technology>, ITechnologyRepository { private readonly DevHiveContext _context; public TechnologyRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - - public async Task<bool> AddAsync(Technology entity) - { - await this._context - .Set<Technology>() - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Read - - public async Task<Technology> GetByIdAsync(Guid id) - { - return await this._context - .Set<Technology>() - .FindAsync(id); - } public async Task<Technology> GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion - #region Edit - - public async Task<bool> EditAsync(Technology newEntity) - { - this._context - .Set<Technology>() - .Update(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - - public async Task<bool> DeleteAsync(Technology entity) - { - this._context - .Set<Technology>() - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Validations - public async Task<bool> DoesTechnologyNameExistAsync(string technologyName) { - return await this._context - .Set<Technology>() + return await this._context.Technologies .AsNoTracking() .AnyAsync(r => r.Name == technologyName); } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 6d4a0bf..1511c63 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -2,59 +2,22 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IUserRepository + public class UserRepository : BaseRepository<User>, IUserRepository { private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - - public async Task<bool> AddAsync(User entity) - { - await this._context.Users - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> AddFriendToUserAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Add(friend); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> AddLanguageToUserAsync(User user, Language language) - { - this._context.Update(user); - - user.Languages.Add(language); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> AddTechnologyToUserAsync(User user, Technology technology) - { - this._context.Update(user); - - user.Technologies.Add(technology); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Read public IEnumerable<User> QueryAll() @@ -65,7 +28,7 @@ namespace DevHive.Data.Repositories .AsEnumerable(); } - public async Task<User> GetByIdAsync(Guid id) + public override async Task<User> GetByIdAsync(Guid id) { return await this._context.Users .Include(x => x.Friends) @@ -78,11 +41,13 @@ namespace DevHive.Data.Repositories public async Task<User> GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } - public IList<Language> GetUserLanguages(User user) + public HashSet<Language> GetUserLanguages(User user) { return user.Languages; } @@ -93,7 +58,7 @@ namespace DevHive.Data.Repositories .FirstOrDefault(x => x.Id == language.Id); } - public IList<Technology> GetUserTechnologies(User user) + public HashSet<Technology> GetUserTechnologies(User user) { return user.Technologies; } @@ -105,83 +70,12 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - - public async Task<bool> EditAsync(User newEntity) - { - User user = await this.GetByIdAsync(newEntity.Id); - - this._context - .Entry(user) - .CurrentValues - .SetValues(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> EditUserLanguageAsync(User user, Language oldLang, Language newLang) - { - this._context.Update(user); - - user.Languages.Remove(oldLang); - user.Languages.Add(newLang); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech) - { - this._context.Update(user); - - user.Technologies.Remove(oldTech); - user.Technologies.Add(newTech); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - - public async Task<bool> DeleteAsync(User entity) - { - this._context.Users - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> RemoveFriendAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Remove(friend); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> RemoveLanguageFromUserAsync(User user, Language language) - { - this._context.Update(user); - - user.Languages.Remove(language); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task<bool> RemoveTechnologyFromUserAsync(User user, Technology technology) - { - this._context.Update(user); - - user.Technologies.Remove(technology); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task<bool> DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -213,6 +107,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } |
