diff options
| author | transtrike <transtrike@gmail.com> | 2021-01-24 00:07:44 +0200 |
|---|---|---|
| committer | transtrike <transtrike@gmail.com> | 2021-01-24 00:07:44 +0200 |
| commit | f910a2a63cb83b35c6589591400a69c8f7f7917c (patch) | |
| tree | 2e601172f734d53ab7a903426ea2d674b5048a76 /src | |
| parent | e01a81954e0fba2c4521e03a76f48a970a87994f (diff) | |
| download | DevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.tar DevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.tar.gz DevHive-f910a2a63cb83b35c6589591400a69c8f7f7917c.zip | |
Migrations added; CRUD over Posts&Comments successfully completed
Diffstat (limited to 'src')
29 files changed, 538 insertions, 475 deletions
diff --git a/src/DevHive.Data/Interfaces/Models/IComment.cs b/src/DevHive.Data/Interfaces/Models/IComment.cs index 6368e3b..9a72da4 100644 --- a/src/DevHive.Data/Interfaces/Models/IComment.cs +++ b/src/DevHive.Data/Interfaces/Models/IComment.cs @@ -6,7 +6,7 @@ namespace DevHive.Data.Interfaces.Models { Guid PostId { get; set; } - Guid IssuerId { get; set; } + Guid CreatorId { get; set; } string Message { get; set; } diff --git a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs deleted file mode 100644 index 6eb1e38..0000000 --- a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs +++ /dev/null @@ -1,411 +0,0 @@ -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/20210121083441_UserRefactor.Designer.cs b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs index 7c7a092..0e4b103 100644 --- a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.Designer.cs +++ b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.Designer.cs @@ -10,8 +10,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { [DbContext(typeof(DevHiveContext))] - [Migration("20210121083441_UserRefactor")] - partial class UserRefactor + [Migration("20210123215634_PostAndComment_Implemented")] + partial class PostAndComment_Implemented { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -27,13 +27,13 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property<Guid>("IssuerId") + b.Property<Guid>("CreatorId") .HasColumnType("uuid"); b.Property<string>("Message") .HasColumnType("text"); - b.Property<Guid?>("PostId") + b.Property<Guid>("PostId") .HasColumnType("uuid"); b.Property<DateTime>("TimeCreated") @@ -66,7 +66,7 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property<Guid>("IssuerId") + b.Property<Guid>("CreatorId") .HasColumnType("uuid"); b.Property<string>("Message") @@ -353,7 +353,9 @@ namespace DevHive.Data.Migrations { b.HasOne("DevHive.Data.Models.Post", null) .WithMany("Comments") - .HasForeignKey("PostId"); + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("DevHive.Data.Models.User", b => diff --git a/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs new file mode 100644 index 0000000..4c9f3bd --- /dev/null +++ b/src/DevHive.Data/Migrations/20210123215634_PostAndComment_Implemented.cs @@ -0,0 +1,411 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + public partial class PostAndComment_Implemented : 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), + CreatorId = 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) + }, + 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), + PostId = table.Column<Guid>(type: "uuid", nullable: false), + CreatorId = 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) + }, + 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.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.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 0727d33..755c274 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -25,13 +25,13 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property<Guid>("IssuerId") + b.Property<Guid>("CreatorId") .HasColumnType("uuid"); b.Property<string>("Message") .HasColumnType("text"); - b.Property<Guid?>("PostId") + b.Property<Guid>("PostId") .HasColumnType("uuid"); b.Property<DateTime>("TimeCreated") @@ -64,7 +64,7 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property<Guid>("IssuerId") + b.Property<Guid>("CreatorId") .HasColumnType("uuid"); b.Property<string>("Message") @@ -351,7 +351,9 @@ namespace DevHive.Data.Migrations { b.HasOne("DevHive.Data.Models.Post", null) .WithMany("Comments") - .HasForeignKey("PostId"); + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); modelBuilder.Entity("DevHive.Data.Models.User", b => diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs index 5f4207d..c953355 100644 --- a/src/DevHive.Data/Models/Comment.cs +++ b/src/DevHive.Data/Models/Comment.cs @@ -9,7 +9,7 @@ namespace DevHive.Data.Models public Guid PostId { get; set; } - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index f2b2786..7ad8ff2 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -7,7 +7,9 @@ namespace DevHive.Data.Models public class Language : ILanguage { public Guid Id { get; set; } + public string Name { get; set; } - public HashSet<User> Users { get; set; } + + public HashSet<User> Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index f7bca43..1b1ec4d 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -16,8 +16,8 @@ namespace DevHive.Data.Models public DateTime TimeCreated { get; set; } - public List<Comment> Comments { get; set; } + public List<Comment> Comments { get; set; } = new(); - // public List<string> Files { get; set; } + // public List<string> Files { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e0855aa..259d867 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 HashSet<User> Users { get; set; } + public HashSet<User> Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index a0728d5..6f98f0b 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -7,7 +7,9 @@ namespace DevHive.Data.Models public class Technology : ITechnology { public Guid Id { get; set; } + public string Name { get; set; } - public HashSet<User> Users { get; set; } + + public HashSet<User> Users { get; set; } = new(); } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 2ac7adf..df440d5 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -19,15 +19,15 @@ namespace DevHive.Data.Models /// Languages that the user uses or is familiar with /// </summary> // [Unique] - public HashSet<Language> Languages { get; set; } + public HashSet<Language> Languages { get; set; } = new(); /// <summary> /// Technologies that the user uses or is familiar with /// </summary> - public HashSet<Technology> Technologies { get; set; } = new HashSet<Technology>(); + public HashSet<Technology> Technologies { get; set; } = new(); - public HashSet<Role> Roles { get; set; } = new HashSet<Role>(); + public HashSet<Role> Roles { get; set; } = new(); - public HashSet<User> Friends { get; set; } = new HashSet<User>(); + public HashSet<User> Friends { get; set; } = new(); } } diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index 880631a..006326a 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -20,7 +20,7 @@ namespace DevHive.Data.Repositories public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + .FirstOrDefaultAsync(p => p.CreatorId == issuerId && p.TimeCreated == timeCreated); } #endregion diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index a79eacf..e8180de 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -17,6 +17,13 @@ namespace DevHive.Data.Repositories } #region Read + public override async Task<Post> GetByIdAsync(Guid id) + { + return await this._context.Posts + .Include(x => x.Comments) + .FirstOrDefaultAsync(x => x.Id == id); + } + public async Task<Post> GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) { return await this._context.Posts diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index 3cea801..ac3c8f6 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -14,7 +14,10 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage)); CreateMap<Comment, ReadCommentServiceModel>() - .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id)); + .ForMember(dest => dest.CommentId, src => src.MapFrom(p => p.Id)) + .ForMember(dest => dest.IssuerFirstName, src => src.Ignore()) + .ForMember(dest => dest.IssuerLastName, src => src.Ignore()) + .ForMember(dest => dest.IssuerUsername, src => src.Ignore()); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index e4924a5..cea7b1c 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -16,7 +16,10 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.Message, src => src.MapFrom(p => p.NewMessage)); CreateMap<Post, ReadPostServiceModel>() - .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id)); + .ForMember(dest => dest.PostId, src => src.MapFrom(p => p.Id)) + .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) + .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) + .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); } } } diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs index 4dfd848..8d49659 100644 --- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs @@ -6,10 +6,8 @@ namespace DevHive.Services.Models.Post.Comment { public Guid PostId { get; set; } - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } - - public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs index c6ff612..12e29a0 100644 --- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs @@ -6,7 +6,11 @@ namespace DevHive.Services.Models.Post.Comment { public Guid CommentId { get; set; } - public Guid IssuerId { get; set; } + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } public Guid PostId { get; set; } diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs index 51cd739..3827d4d 100644 --- a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs @@ -4,8 +4,12 @@ namespace DevHive.Services.Models.Post.Comment { public class UpdateCommentServiceModel { + public Guid CreatorId { get; set; } + public Guid CommentId { get; set; } + public Guid PostId { get; set; } + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs index 6b83f3e..36f6351 100644 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -4,12 +4,10 @@ namespace DevHive.Services.Models.Post.Post { public class CreatePostServiceModel { - public Guid IssuerId { get; set; } + public Guid CreatorId { get; set; } public string Message { get; set; } - public DateTime TimeCreated { get; set; } - // public List<IFormFile> Files { get; set; } } } diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs index 52b9232..3e673c1 100644 --- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs @@ -8,13 +8,17 @@ namespace DevHive.Services.Models.Post.Post { public Guid PostId { get; set; } - public Guid CreatorId { get; set; } + public string CreatorFirstName { get; set; } + + public string CreatorLastName { get; set; } + + public string CreatorUsername { get; set; } public string Message { get; set; } public DateTime TimeCreated { get; set; } - public List<ReadCommentServiceModel> Comments { get; set; } + public List<ReadCommentServiceModel> Comments { get; set; } = new(); //public List<string> Files { get; set; } } diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs index 67ee711..8924b07 100644 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -6,6 +6,8 @@ namespace DevHive.Services.Models.Post.Post { public Guid PostId { get; set; } + public Guid CreatorId { get; set; } + public string NewMessage { get; set; } // public List<IFormFile> Files { get; set; } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 377fe05..7fc975c 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -31,6 +31,9 @@ namespace DevHive.Services.Services #region Create public async Task<Guid> CreatePost(CreatePostServiceModel createPostServiceModel) { + if(!await this._userRepository.DoesUserExistAsync(createPostServiceModel.CreatorId)) + throw new ArgumentException("User does not exist!"); + Post post = this._postMapper.Map<Post>(createPostServiceModel); post.TimeCreated = DateTime.Now; @@ -38,7 +41,7 @@ namespace DevHive.Services.Services if (success) { Post newPost = await this._postRepository - .GetPostByCreatorAndTimeCreatedAsync(createPostServiceModel.IssuerId, createPostServiceModel.TimeCreated); + .GetPostByCreatorAndTimeCreatedAsync(post.CreatorId, post.TimeCreated); return newPost.Id; } @@ -52,13 +55,13 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Comment comment = this._postMapper.Map<Comment>(createCommentServiceModel); - createCommentServiceModel.TimeCreated = DateTime.Now; + comment.TimeCreated = DateTime.Now; bool success = await this._commentRepository.AddAsync(comment); if (success) { Comment newComment = await this._commentRepository - .GetCommentByIssuerAndTimeCreatedAsync(createCommentServiceModel.IssuerId, createCommentServiceModel.TimeCreated); + .GetCommentByIssuerAndTimeCreatedAsync(comment.CreatorId, comment.TimeCreated); return newComment.Id; } @@ -73,7 +76,15 @@ namespace DevHive.Services.Services Post post = await this._postRepository.GetByIdAsync(id) ?? throw new ArgumentException("The post does not exist!"); - return this._postMapper.Map<ReadPostServiceModel>(post); + User user = await this._userRepository.GetByIdAsync(post.CreatorId) ?? + throw new ArgumentException("User does not exist He could've been deleted!"); + + ReadPostServiceModel readPostServiceModel = this._postMapper.Map<ReadPostServiceModel>(post); + readPostServiceModel.CreatorFirstName = user.FirstName; + readPostServiceModel.CreatorLastName = user.LastName; + readPostServiceModel.CreatorUsername = user.UserName; + + return readPostServiceModel; } public async Task<ReadCommentServiceModel> GetCommentById(Guid id) @@ -81,7 +92,14 @@ namespace DevHive.Services.Services Comment comment = await this._commentRepository.GetByIdAsync(id) ?? throw new ArgumentException("The comment does not exist"); - return this._postMapper.Map<ReadCommentServiceModel>(comment); + User user = await this._userRepository.GetByIdAsync(comment.CreatorId); + + ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map<ReadCommentServiceModel>(comment); + readCommentServiceModel.IssuerFirstName = user.FirstName; + readCommentServiceModel.IssuerLastName = user.LastName; + readCommentServiceModel.IssuerUsername = user.UserName; + + return readCommentServiceModel; } #endregion @@ -92,6 +110,8 @@ namespace DevHive.Services.Services throw new ArgumentException("Post does not exist!"); Post post = this._postMapper.Map<Post>(updatePostServiceModel); + post.TimeCreated = DateTime.Now; + bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); if (result) @@ -106,6 +126,8 @@ namespace DevHive.Services.Services throw new ArgumentException("Comment does not exist!"); Comment comment = this._postMapper.Map<Comment>(updateCommentServiceModel); + comment.TimeCreated = DateTime.Now; + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); if (result) @@ -138,29 +160,45 @@ namespace DevHive.Services.Services #region Validations public async Task<bool> ValidateJwtForPost(Guid postId, string rawTokenData) { - Post post = await this._postRepository.GetByIdAsync(postId); + Post post = await this._postRepository.GetByIdAsync(postId) ?? + throw new ArgumentException("Post does not exist!"); User user = await this.GetUserForValidation(rawTokenData); - return post.CreatorId == user.Id; + //If user made the post + if (post.CreatorId == user.Id) + return true; + //If user is admin + else if(user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; } public async Task<bool> ValidateJwtForComment(Guid commentId, string rawTokenData) { - Comment comment = await this._commentRepository.GetByIdAsync(commentId); + Comment comment = await this._commentRepository.GetByIdAsync(commentId) ?? + throw new ArgumentException("Comment does not exist!"); User user = await this.GetUserForValidation(rawTokenData); - return comment.IssuerId == user.Id; + //If user made the comment + if (comment.CreatorId == user.Id) + return true; + //If user is admin + else if(user.Roles.Any(x => x.Name == Role.AdminRole)) + return true; + else + return false; } private async Task<User> GetUserForValidation(string rawTokenData) { - var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); //HashSet<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - User user = await this._userRepository.GetByUsernameAsync(jwtUserName) - ?? throw new ArgumentException("User does not exist!"); + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); return user; } diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 296704e..a28ee16 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -11,10 +11,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<CreateCommentWebModel, CreateCommentServiceModel>(); CreateMap<UpdateCommentWebModel, UpdateCommentServiceModel>(); - CreateMap<ReadCommentServiceModel, ReadCommentWebModel>() - .ForMember(dest => dest.IssuerFirstName, src => src.Ignore()) - .ForMember(dest => dest.IssuerLastName, src => src.Ignore()) - .ForMember(dest => dest.IssuerUsername, src => src.Ignore()); + CreateMap<ReadCommentServiceModel, ReadCommentWebModel>(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs index 0e966cc..bc7bc06 100644 --- a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -11,10 +11,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap<CreatePostWebModel, CreatePostServiceModel>(); CreateMap<UpdatePostWebModel, UpdatePostServiceModel>(); - CreateMap<ReadPostServiceModel, ReadPostWebModel>() - .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) - .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) - .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); + CreateMap<ReadPostServiceModel, ReadPostWebModel>(); } } } diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 8b8b525..b5e1c98 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -31,7 +31,7 @@ namespace DevHive.Web.Controllers { CreatePostServiceModel createPostServiceModel = this._postMapper.Map<CreatePostServiceModel>(createPostWebModel); - createPostServiceModel.IssuerId = userId; + createPostServiceModel.CreatorId = userId; Guid id = await this._postService.CreatePost(createPostServiceModel); @@ -46,7 +46,7 @@ namespace DevHive.Web.Controllers { CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map<CreateCommentServiceModel>(createCommentWebModel); - createCommentServiceModel.IssuerId = userId; + createCommentServiceModel.CreatorId = userId; Guid id = await this._postService.AddComment(createCommentServiceModel); @@ -83,11 +83,12 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task<IActionResult> Update(Guid userId, [FromBody] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForPost(userId, authorization)) + if (!await this._postService.ValidateJwtForPost(updatePostWebModel.PostId, authorization)) return new UnauthorizedResult(); UpdatePostServiceModel updatePostServiceModel = this._postMapper.Map<UpdatePostServiceModel>(updatePostWebModel); + updatePostServiceModel.CreatorId = userId; Guid id = await this._postService.UpdatePost(updatePostServiceModel); @@ -100,11 +101,12 @@ namespace DevHive.Web.Controllers [Route("Comment")] public async Task<IActionResult> UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForComment(userId, authorization)) + if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) return new UnauthorizedResult(); UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map<UpdateCommentServiceModel>(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; Guid id = await this._postService.UpdateComment(updateCommentServiceModel); diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 0206542..c68a32b 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -23,7 +23,7 @@ namespace DevHive.Web.Controllers } [HttpPost] - [Authorize(Policy = "Administrator")] + // [Authorize(Policy = "Administrator")] public async Task<IActionResult> Create([FromBody] CreateRoleWebModel createRoleWebModel) { CreateRoleServiceModel roleServiceModel = diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs index 6dff49e..cb1c60a 100644 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs @@ -6,6 +6,8 @@ namespace DevHive.Web.Models.Post.Comment { public Guid CommentId { get; set; } + public Guid PostId { get; set; } + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs index 647b30e..b7b4cf4 100644 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -8,10 +8,6 @@ namespace DevHive.Web.Models.Post.Post { [NotNull] [Required] - public Guid CreatorId { get; set; } - - [NotNull] - [Required] public string Message { get; set; } // public List<IFormFile> Files { get; set; } diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs index fe42715..685f08b 100644 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs @@ -12,6 +12,6 @@ namespace DevHive.Web.Models.Post.Post [NotNull] [Required] - public string Message { get; set; } + public string NewMessage { get; set; } } } |
