From 6deaa6edcd8e347d5ed28ee3389cb8712cc64ea3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 11:05:26 +0200 Subject: The return of the interfaces --- src/DevHive.Services/Interfaces/IPostService.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/DevHive.Services/Interfaces/IPostService.cs (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs new file mode 100644 index 0000000..9cb21ed --- /dev/null +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Interfaces +{ + public interface IPostService + { + Task CreatePost(CreatePostServiceModel postServiceModel); + Task AddComment(CreateCommentServiceModel commentServiceModel); + + Task GetCommentById(Guid id); + Task GetPostById(Guid id); + + Task UpdateComment(UpdateCommentServiceModel commentServiceModel); + Task UpdatePost(UpdatePostServiceModel postServiceModel); + + Task DeleteComment(Guid id); + Task DeletePost(Guid id); + + Task ValidateJwtForComment(Guid commentId, string rawTokenData); + } +} \ No newline at end of file -- cgit v1.2.3 From e20e89e83c73864d5dc3615733836a1f1d1565c6 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 14 Jan 2021 11:09:39 +0200 Subject: More formatting fixes --- src/DevHive.Data/DevHiveContext.cs | 2 +- src/DevHive.Data/Interfaces/IPostRepository.cs | 4 +- src/DevHive.Data/Interfaces/IRoleRepository.cs | 4 +- .../Migrations/20201219141035_DbContext_Moved.cs | 530 ++++++++++----------- .../Migrations/20201230154737_CommentMigration.cs | 48 +- .../20210112111416_User_Implements_Languages.cs | 200 ++++---- src/DevHive.Data/Models/Language.cs | 2 +- .../Configurations/Mapping/PostMappings.cs | 2 +- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/UserMappings.cs | 2 +- src/DevHive.Services/Interfaces/IPostService.cs | 6 +- src/DevHive.Services/Interfaces/IRoleService.cs | 4 +- .../Models/Identity/User/BaseUserServiceModel.cs | 2 +- .../Models/Post/Post/CreatePostServiceModel.cs | 4 +- .../Models/Post/Post/UpdatePostServiceModel.cs | 6 +- .../TechnologyRepository.Tests.cs | 378 +++++++-------- src/DevHive.Tests/DevHive.Web.Tests/UnitTest1.cs | 18 - 17 files changed, 598 insertions(+), 616 deletions(-) delete mode 100644 src/DevHive.Tests/DevHive.Web.Tests/UnitTest1.cs (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index c9959dc..10fd004 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -22,7 +22,7 @@ namespace DevHive.Data builder.Entity() .HasMany(x => x.Roles); - + builder.Entity() .HasMany(x => x.Friends); diff --git a/src/DevHive.Data/Interfaces/IPostRepository.cs b/src/DevHive.Data/Interfaces/IPostRepository.cs index 9c75ecc..a02fd08 100644 --- a/src/DevHive.Data/Interfaces/IPostRepository.cs +++ b/src/DevHive.Data/Interfaces/IPostRepository.cs @@ -10,11 +10,11 @@ namespace DevHive.Data.Interfaces Task AddCommentAsync(Comment entity); Task GetCommentByIdAsync(Guid id); - + Task EditCommentAsync(Comment newEntity); Task DeleteCommentAsync(Comment entity); Task DoesCommentExist(Guid id); Task DoesPostExist(Guid postId); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Interfaces/IRoleRepository.cs b/src/DevHive.Data/Interfaces/IRoleRepository.cs index 48761db..a1080bb 100644 --- a/src/DevHive.Data/Interfaces/IRoleRepository.cs +++ b/src/DevHive.Data/Interfaces/IRoleRepository.cs @@ -8,8 +8,8 @@ namespace DevHive.Data.Interfaces public interface IRoleRepository : IRepository { Task GetByNameAsync(string name); - + Task DoesNameExist(string name); Task DoesRoleExist(Guid id); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs b/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs index 0c27917..ccc5e91 100644 --- a/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs +++ b/src/DevHive.Data/Migrations/20201219141035_DbContext_Moved.cs @@ -4,298 +4,298 @@ 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(type: "uuid", nullable: false), - Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoles", x => x.Id); - }); + public partial class DbContext_Moved : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "AspNetUsers", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - FirstName = table.Column(type: "text", nullable: true), - LastName = table.Column(type: "text", nullable: true), - ProfilePicture = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: true), - UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - EmailConfirmed = table.Column(type: "boolean", nullable: false), - PasswordHash = table.Column(type: "text", nullable: true), - SecurityStamp = table.Column(type: "text", nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true), - PhoneNumber = table.Column(type: "text", nullable: true), - PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), - TwoFactorEnabled = table.Column(type: "boolean", nullable: false), - LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), - LockoutEnabled = table.Column(type: "boolean", nullable: false), - AccessFailedCount = table.Column(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: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "text", nullable: true), + LastName = table.Column(type: "text", nullable: true), + ProfilePicture = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: true), + UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "text", nullable: true), + SecurityStamp = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true), + PhoneNumber = table.Column(type: "text", nullable: true), + PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), + TwoFactorEnabled = table.Column(type: "boolean", nullable: false), + LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), + LockoutEnabled = table.Column(type: "boolean", nullable: false), + AccessFailedCount = table.Column(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(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Languages", x => x.Id); - }); + migrationBuilder.CreateTable( + name: "Languages", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Languages", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "Technologies", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Technologies", x => x.Id); - }); + migrationBuilder.CreateTable( + name: "Technologies", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Technologies", x => x.Id); + }); - migrationBuilder.CreateTable( - name: "AspNetRoleClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - RoleId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(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: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(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(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(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: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(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(type: "text", nullable: false), - ProviderKey = table.Column(type: "text", nullable: false), - ProviderDisplayName = table.Column(type: "text", nullable: true), - UserId = table.Column(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: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "text", nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "text", nullable: true), + UserId = table.Column(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(type: "uuid", nullable: false), - RoleId = table.Column(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: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + RoleId = table.Column(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(type: "uuid", nullable: false), - LoginProvider = table.Column(type: "text", nullable: false), - Name = table.Column(type: "text", nullable: false), - Value = table.Column(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: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + LoginProvider = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + Value = table.Column(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(type: "uuid", nullable: false), - UsersId = table.Column(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: "RoleUser", + columns: table => new + { + RolesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(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: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); - migrationBuilder.CreateIndex( - name: "RoleNameIndex", - table: "AspNetRoles", - column: "NormalizedName", - unique: true); + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); - migrationBuilder.CreateIndex( - name: "IX_AspNetUserClaims_UserId", - table: "AspNetUserClaims", - column: "UserId"); + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); - migrationBuilder.CreateIndex( - name: "IX_AspNetUserLogins_UserId", - table: "AspNetUserLogins", - 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: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); - migrationBuilder.CreateIndex( - name: "EmailIndex", - table: "AspNetUsers", - column: "NormalizedEmail"); + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserId", - table: "AspNetUsers", - column: "UserId"); + 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: "IX_AspNetUsers_UserName", + table: "AspNetUsers", + column: "UserName", + unique: true); - migrationBuilder.CreateIndex( - name: "UserNameIndex", - table: "AspNetUsers", - column: "NormalizedUserName", - unique: true); + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); - migrationBuilder.CreateIndex( - name: "IX_RoleUser_UsersId", - table: "RoleUser", - column: "UsersId"); - } + migrationBuilder.CreateIndex( + name: "IX_RoleUser_UsersId", + table: "RoleUser", + column: "UsersId"); + } - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AspNetRoleClaims"); + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); - migrationBuilder.DropTable( - name: "AspNetUserClaims"); + migrationBuilder.DropTable( + name: "AspNetUserClaims"); - migrationBuilder.DropTable( - name: "AspNetUserLogins"); + migrationBuilder.DropTable( + name: "AspNetUserLogins"); - migrationBuilder.DropTable( - name: "AspNetUserRoles"); + migrationBuilder.DropTable( + name: "AspNetUserRoles"); - migrationBuilder.DropTable( - name: "AspNetUserTokens"); + migrationBuilder.DropTable( + name: "AspNetUserTokens"); - migrationBuilder.DropTable( - name: "Languages"); + migrationBuilder.DropTable( + name: "Languages"); - migrationBuilder.DropTable( - name: "RoleUser"); + migrationBuilder.DropTable( + name: "RoleUser"); - migrationBuilder.DropTable( - name: "Technologies"); + migrationBuilder.DropTable( + name: "Technologies"); - migrationBuilder.DropTable( - name: "AspNetRoles"); + migrationBuilder.DropTable( + name: "AspNetRoles"); - migrationBuilder.DropTable( - name: "AspNetUsers"); - } - } + migrationBuilder.DropTable( + name: "AspNetUsers"); + } + } } diff --git a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs index b952be5..a442bfa 100644 --- a/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs +++ b/src/DevHive.Data/Migrations/20201230154737_CommentMigration.cs @@ -3,29 +3,29 @@ 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(type: "uuid", nullable: false), - UserId = table.Column(type: "uuid", nullable: false), - Message = table.Column(type: "text", nullable: true), - Date = table.Column(type: "timestamp without time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Comments", x => x.Id); - }); - } + public partial class CommentMigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + Message = table.Column(type: "text", nullable: true), + Date = table.Column(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"); - } - } + 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 index a51ad09..d698f10 100644 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs @@ -3,104 +3,104 @@ 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( - name: "UserId", - table: "Technologies", - type: "uuid", - nullable: true); - - migrationBuilder.AddColumn( - 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"); - } - } + 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( + name: "UserId", + table: "Technologies", + type: "uuid", + nullable: true); + + migrationBuilder.AddColumn( + 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/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index fb3a042..556d019 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -4,6 +4,6 @@ namespace DevHive.Data.Models public class Language : IModel { public Guid Id { get; set; } - public string Name { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index 695ffdb..7f99c66 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -12,5 +12,5 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); } - } + } } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index db4473e..65b0b5a 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -11,5 +11,5 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); } - } + } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index ca8fa20..d57c6ba 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -14,5 +14,5 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); } - } + } } diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 9cb21ed..dd886b4 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -15,10 +15,10 @@ namespace DevHive.Services.Interfaces Task UpdateComment(UpdateCommentServiceModel commentServiceModel); Task UpdatePost(UpdatePostServiceModel postServiceModel); - + Task DeleteComment(Guid id); Task DeletePost(Guid id); - + Task ValidateJwtForComment(Guid commentId, string rawTokenData); } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index 3bf236c..2c7195c 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -11,7 +11,7 @@ namespace DevHive.Services.Interfaces Task GetRoleById(Guid id); Task UpdateRole(RoleModel roleServiceModel); - + Task DeleteRole(Guid id); } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs index 2e75848..514f82a 100644 --- a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Services.Models.Identity.User { - public class BaseUserServiceModel + public class BaseUserServiceModel { public string UserName { get; set; } public string Email { get; set; } diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs index 97225c7..1bf60ae 100644 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -5,5 +5,5 @@ namespace DevHive.Services.Models.Post.Post public class CreatePostServiceModel : BasePostServiceModel { public DateTime TimeCreated { get; set; } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs index de61a72..5a4f621 100644 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -1,7 +1,5 @@ namespace DevHive.Services.Models.Post.Post { public class UpdatePostServiceModel : BasePostServiceModel - { - - } -} \ No newline at end of file + { } +} diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 6ff5fa2..18d72b9 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -8,192 +8,194 @@ using System.Threading.Tasks; namespace DevHive.Data.Tests { - [TestFixture] - public class TechnologyRepositoryTests - { - private const string TECHNOLOGY_NAME = "Technology test name"; - - protected DevHiveContext Context { get; set; } - - protected TechnologyRepository TechnologyRepository { get; set; } - - [SetUp] - public void Setup() - { - var optionsBuilder = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); - - this.Context = new DevHiveContext(optionsBuilder.Options); - - TechnologyRepository = new TechnologyRepository(Context); - } - - [TearDown] - public void TearDown() - { - this.Context.Database.EnsureDeleted(); - } - - #region AddAsync - [Test] - public void AddAsync_AddsTheGivenTechnologyToTheDatabase() - { - AddEntity(); - - int numberOfTechnologies = Context.Technologies.Count(); - - Assert.True(numberOfTechnologies > 0, "Technologies repo does not store Technologies correctly"); - } - #endregion - - #region GetByIdAsync - [Test] - public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() - { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; - - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - - Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); - }).GetAwaiter().GetResult(); - } - - [Test] - public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() - { - Task.Run(async () => - { - Guid id = new Guid(); - - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - - Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); - }).GetAwaiter().GetResult(); - } - #endregion - - #region DoesTechnologyExistAsync - [Test] - public void DoesTechnologyExist_ReturnsTrue_IfIdExists() - { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; - - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - - Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); - }).GetAwaiter().GetResult(); - } - - [Test] - public void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() - { - Task.Run(async () => - { - Guid id = new Guid(); - - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - - Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); - }).GetAwaiter().GetResult(); - } - #endregion - - #region DoesTechnologyNameExistAsync - [Test] - public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() - { - Task.Run(async () => - { - AddEntity(); - - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - - Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); - }).GetAwaiter().GetResult(); - } - - [Test] - public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() - { - Task.Run(async () => - { - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - - Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); - }).GetAwaiter().GetResult(); - } - #endregion - - #region EditAsync - [Test] - public void EditAsync_UpdatesEntity() - { - Task.Run(async () => - { - string newName = "New name"; - Guid id = new Guid(); - Technology technology = new Technology - { - Name = TECHNOLOGY_NAME, - Id = id - }; - Technology newTechnology = new Technology - { - Name = newName, - Id = id - }; - await this.TechnologyRepository.AddAsync(technology); - - bool result = await this.TechnologyRepository.EditAsync(newTechnology); - - Assert.IsTrue(result); - }).GetAwaiter().GetResult(); - } - #endregion - - #region DeleteAsync - [Test] - public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() - { - Task.Run(async () => - { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - - bool result = await this.TechnologyRepository.DeleteAsync(technology); - - Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); - - }).GetAwaiter().GetResult(); - } - #endregion - - #region HelperMethods - private void AddEntity(string name = TECHNOLOGY_NAME) - { - Task.Run(async () => - { - Technology technology = new Technology - { - Name = name - }; - - await this.TechnologyRepository.AddAsync(technology); - }).GetAwaiter().GetResult(); - } - #endregion - - //Task.Run(async () => - //{ - // - //}).GetAwaiter().GetResult(); - } + [TestFixture] + public class TechnologyRepositoryTests + { + private const string TECHNOLOGY_NAME = "Technology test name"; + + protected DevHiveContext Context { get; set; } + + protected TechnologyRepository TechnologyRepository { get; set; } + + [SetUp] + public void Setup() + { + var optionsBuilder = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "DevHive_Test_Database"); + + this.Context = new DevHiveContext(optionsBuilder.Options); + + TechnologyRepository = new TechnologyRepository(Context); + } + + [TearDown] + public void TearDown() + { + this.Context.Database.EnsureDeleted(); + } + + #region AddAsync + + [Test] + public void AddAsync_AddsTheGivenTechnologyToTheDatabase() + { + AddEntity(); + + int numberOfTechnologies = Context.Technologies.Count(); + + Assert.True(numberOfTechnologies > 0, "Technologies repo does not store Technologies correctly"); + } + #endregion + + #region GetByIdAsync + + [Test] + public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() + { + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; + + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + + Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); + }).GetAwaiter().GetResult(); + } + + [Test] + public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() + { + Task.Run(async () => + { + Guid id = new Guid(); + + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + + Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); + }).GetAwaiter().GetResult(); + } + #endregion + + #region DoesTechnologyExistAsync + [Test] + public void DoesTechnologyExist_ReturnsTrue_IfIdExists() + { + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; + + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + + Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); + }).GetAwaiter().GetResult(); + } + + [Test] + public void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() + { + Task.Run(async () => + { + Guid id = new Guid(); + + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + + Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); + }).GetAwaiter().GetResult(); + } + #endregion + + #region DoesTechnologyNameExistAsync + [Test] + public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() + { + Task.Run(async () => + { + AddEntity(); + + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + + Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); + } + + [Test] + public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() + { + Task.Run(async () => + { + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + + Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); + } + #endregion + + #region EditAsync + [Test] + public void EditAsync_UpdatesEntity() + { + Task.Run(async () => + { + string newName = "New name"; + Guid id = new Guid(); + Technology technology = new Technology + { + Name = TECHNOLOGY_NAME, + Id = id + }; + Technology newTechnology = new Technology + { + Name = newName, + Id = id + }; + await this.TechnologyRepository.AddAsync(technology); + + bool result = await this.TechnologyRepository.EditAsync(newTechnology); + + Assert.IsTrue(result); + }).GetAwaiter().GetResult(); + } + #endregion + + #region DeleteAsync + [Test] + public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() + { + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + + bool result = await this.TechnologyRepository.DeleteAsync(technology); + + Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + + }).GetAwaiter().GetResult(); + } + #endregion + + #region HelperMethods + private void AddEntity(string name = TECHNOLOGY_NAME) + { + Task.Run(async () => + { + Technology technology = new Technology + { + Name = name + }; + + await this.TechnologyRepository.AddAsync(technology); + }).GetAwaiter().GetResult(); + } + #endregion + + //Task.Run(async () => + //{ + // + //}).GetAwaiter().GetResult(); + } } diff --git a/src/DevHive.Tests/DevHive.Web.Tests/UnitTest1.cs b/src/DevHive.Tests/DevHive.Web.Tests/UnitTest1.cs deleted file mode 100644 index e6f54b0..0000000 --- a/src/DevHive.Tests/DevHive.Web.Tests/UnitTest1.cs +++ /dev/null @@ -1,18 +0,0 @@ -using NUnit.Framework; - -namespace DevHive.Web.Tests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void Test1() - { - Assert.Pass(); - } - } -} \ No newline at end of file -- cgit v1.2.3 From 83f63ad729d585d597bdcf0afc05b7d56344223e Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 13:38:24 +0200 Subject: Lang&Tech layers now return id on Create --- src/DevHive.Data/DevHiveContext.cs | 1 + .../Interfaces/Repositories/IPostRepository.cs | 4 ++++ src/DevHive.Data/Repositories/PostRepository.cs | 14 +++++++++++++ .../Interfaces/ILanguageService.cs | 2 +- src/DevHive.Services/Interfaces/IPostService.cs | 4 ++-- .../Interfaces/ITechnologyService.cs | 2 +- .../Models/Post/Comment/BaseCommentServiceModel.cs | 3 ++- src/DevHive.Services/Services/LanguageService.cs | 14 +++++++++---- src/DevHive.Services/Services/PostService.cs | 24 +++++++++++++++++----- src/DevHive.Services/Services/TechnologyService.cs | 14 +++++++++---- src/DevHive.Web/Controllers/LanguageController.cs | 9 ++++---- src/DevHive.Web/Controllers/PostController.cs | 18 ++++++++-------- .../Controllers/TechnologyController.cs | 13 ++++++------ .../Models/Post/Comment/CommentWebModel.cs | 3 ++- src/DevHive.code-workspace | 2 +- 15 files changed, 85 insertions(+), 42 deletions(-) (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') 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 Technologies { get; set; } public DbSet Languages { get; set; } + public DbSet Posts { get; set; } public DbSet Comments { get; set; } protected override void OnModelCreating(ModelBuilder builder) 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 AddCommentAsync(Comment entity); + Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); + Task GetCommentByIdAsync(Guid id); + Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); Task EditCommentAsync(Comment newEntity); Task DeleteCommentAsync(Comment entity); Task DoesCommentExist(Guid id); + Task DoesPostExist(Guid postId); } } diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 3be14e3..c5e8569 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -43,6 +43,13 @@ namespace DevHive.Data.Repositories .FindAsync(id); } + public async Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + { + return await this._context.Posts + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); + } + public async Task GetCommentByIdAsync(Guid id) { return await this._context @@ -50,6 +57,13 @@ namespace DevHive.Data.Repositories .FindAsync(id); } + public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + { + return await this._context.Comments + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); + } + //Update public async Task EditAsync(Post newPost) { diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index 4d16ea3..0df9a95 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -6,7 +6,7 @@ namespace DevHive.Services.Interfaces { public interface ILanguageService { - Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); + Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); Task GetLanguageById(Guid id); diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index dd886b4..4364c67 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -7,8 +7,8 @@ namespace DevHive.Services.Interfaces { public interface IPostService { - Task CreatePost(CreatePostServiceModel postServiceModel); - Task AddComment(CreateCommentServiceModel commentServiceModel); + Task CreatePost(CreatePostServiceModel postServiceModel); + Task AddComment(CreateCommentServiceModel commentServiceModel); Task GetCommentById(Guid id); Task GetPostById(Guid id); diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs index 9e1e955..9c5661d 100644 --- a/src/DevHive.Services/Interfaces/ITechnologyService.cs +++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs @@ -6,7 +6,7 @@ namespace DevHive.Services.Interfaces { public interface ITechnologyService { - Task Create(CreateTechnologyServiceModel technologyServiceModel); + Task Create(CreateTechnologyServiceModel technologyServiceModel); Task GetTechnologyById(Guid id); diff --git a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs index 3aa92ee..54d6838 100644 --- a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs +++ b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs @@ -5,7 +5,8 @@ namespace DevHive.Services.Models.Post.Comment public class BaseCommentServiceModel { public Guid Id { get; set; } + public Guid PostId { get; set; } public Guid IssuerId { get; set; } public string Message { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index f457a31..e9c401e 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -21,15 +21,21 @@ namespace DevHive.Services.Services #region Create - public async Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel) + public async Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel) { if (await this._languageRepository.DoesLanguageNameExistAsync(createLanguageServiceModel.Name)) throw new ArgumentException("Language already exists!"); Language language = this._languageMapper.Map(createLanguageServiceModel); - bool result = await this._languageRepository.AddAsync(language); - - return result; + bool success = await this._languageRepository.AddAsync(language); + + if(success) + { + Language newLanguage = await this._languageRepository.GetByNameAsync(createLanguageServiceModel.Name); + return newLanguage.Id; + } + else + return Guid.Empty; } #endregion diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 6e83ad4..f2f60d1 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -26,21 +26,35 @@ namespace DevHive.Services.Services } //Create - public async Task CreatePost(CreatePostServiceModel postServiceModel) + public async Task CreatePost(CreatePostServiceModel postServiceModel) { Post post = this._postMapper.Map(postServiceModel); - return await this._postRepository.AddAsync(post); + bool success = await this._postRepository.AddAsync(post); + + if(success) + { + Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated); + return newPost.Id; + } + else + return Guid.Empty; } - public async Task AddComment(CreateCommentServiceModel commentServiceModel) + public async Task AddComment(CreateCommentServiceModel commentServiceModel) { commentServiceModel.TimeCreated = DateTime.Now; Comment comment = this._postMapper.Map(commentServiceModel); - bool result = await this._postRepository.AddCommentAsync(comment); + bool success = await this._postRepository.AddCommentAsync(comment); - return result; + if(success) + { + Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated); + return newComment.Id; + } + else + return Guid.Empty; } //Read diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 4e74c83..1b2f0ff 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -21,15 +21,21 @@ namespace DevHive.Services.Services #region Create - public async Task Create(CreateTechnologyServiceModel technologyServiceModel) + public async Task Create(CreateTechnologyServiceModel technologyServiceModel) { if (await this._technologyRepository.DoesTechnologyNameExistAsync(technologyServiceModel.Name)) throw new ArgumentException("Technology already exists!"); Technology technology = this._technologyMapper.Map(technologyServiceModel); - bool result = await this._technologyRepository.AddAsync(technology); - - return result; + bool success = await this._technologyRepository.AddAsync(technology); + + if(success) + { + Technology newTechnology = await this._technologyRepository.GetByNameAsync(technologyServiceModel.Name); + return newTechnology.Id; + } + else + return Guid.Empty; } #endregion diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index bbac409..e2d0dec 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -26,12 +26,11 @@ namespace DevHive.Web.Controllers { CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); - bool result = await this._languageService.CreateLanguage(languageServiceModel); + Guid id = await this._languageService.CreateLanguage(languageServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create Language"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create language {createLanguageWebModel.Name}") : + new OkObjectResult(new { Id = id }); } [HttpGet] diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 15adb1b..2a08605 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -32,12 +32,11 @@ namespace DevHive.Web.Controllers CreatePostServiceModel postServiceModel = this._postMapper.Map(createPostModel); - bool result = await this._postService.CreatePost(postServiceModel); + Guid id = await this._postService.CreatePost(postServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create post!"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create post") : + new OkObjectResult(new { Id = id }); } [HttpPost] @@ -46,12 +45,11 @@ namespace DevHive.Web.Controllers { CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map(commentWebModel); - bool result = await this._postService.AddComment(createCommentServiceModel); + Guid id = await this._postService.AddComment(createCommentServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create the Comment"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create language") : + new OkObjectResult(new { Id = id }); } //Read diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 104b96e..ba2ffdc 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -22,16 +22,15 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateTechnologyWebModel technologyWebModel) + public async Task Create([FromBody] CreateTechnologyWebModel createTechnologyWebModel) { - CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(technologyWebModel); + CreateTechnologyServiceModel technologyServiceModel = this._technologyMapper.Map(createTechnologyWebModel); - bool result = await this._technologyService.Create(technologyServiceModel); + Guid id = await this._technologyService.Create(technologyServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create the Technology"); - - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create technology {createTechnologyWebModel.Name}") : + new OkObjectResult(new { Id = id }); } [HttpGet] diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs index 3cdc7c4..d66e5c9 100644 --- a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -5,7 +5,8 @@ namespace DevHive.Web.Models.Post.Comment public class CommentWebModel { public Guid IssuerId { get; set; } + public Guid PostId { get; set; } public string Message { get; set; } public DateTime TimeCreated { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace index 4f764c2..28b1e3c 100644 --- a/src/DevHive.code-workspace +++ b/src/DevHive.code-workspace @@ -62,7 +62,7 @@ "ASPNETCORE_ENVIRONMENT": "Development" }, "launchBrowser": { - "enabled": true + "enabled": false } }, ], -- cgit v1.2.3 From e01a81954e0fba2c4521e03a76f48a970a87994f Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 23 Jan 2021 22:34:43 +0200 Subject: All Post&Comment Implemented; Initializing testing... --- .../Interfaces/Repositories/ICommentRepository.cs | 13 +++ .../Interfaces/Repositories/IPostRepository.cs | 13 +-- src/DevHive.Data/Repositories/CommentRepository.cs | 37 +++++++ src/DevHive.Data/Repositories/PostRepository.cs | 54 +--------- src/DevHive.Services/Interfaces/IPostService.cs | 15 +-- src/DevHive.Services/Services/PostService.cs | 115 +++++++++++++-------- .../Extensions/ConfigureDependencyInjection.cs | 1 + src/DevHive.Web/Controllers/PostController.cs | 102 +++++++++--------- .../Controllers/TechnologyController.cs | 4 +- .../Models/Post/Comment/UpdateCommentWebModel.cs | 4 + .../Models/Post/Post/UpdatePostWebModel.cs | 4 + 11 files changed, 195 insertions(+), 167 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs create mode 100644 src/DevHive.Data/Repositories/CommentRepository.cs (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs new file mode 100644 index 0000000..b80c5a0 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/ICommentRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface ICommentRepository : IRepository + { + Task DoesCommentExist(Guid id); + Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs index 7a9c02e..aa0afc7 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs @@ -7,18 +7,7 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IPostRepository : IRepository { - Task AddCommentAsync(Comment entity); - - Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); - - Task GetCommentByIdAsync(Guid id); - Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); - - Task EditCommentAsync(Comment newEntity); - - Task DeleteCommentAsync(Comment entity); - Task DoesCommentExist(Guid id); - + Task GetPostByCreatorAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); Task DoesPostExist(Guid postId); } } diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs new file mode 100644 index 0000000..880631a --- /dev/null +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class CommentRepository : BaseRepository, ICommentRepository + { + private readonly DevHiveContext _context; + + public CommentRepository(DevHiveContext context) + : base(context) + { + this._context = context; + } + + #region Read + public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + { + return await this._context.Comments + .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + p.TimeCreated == timeCreated); + } + #endregion + + #region Validations + public async Task DoesCommentExist(Guid id) + { + return await this._context.Comments + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + #endregion + } +} diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 9230a2e..a79eacf 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -16,56 +16,13 @@ namespace DevHive.Data.Repositories this._context = context; } - #region Create - public async Task AddCommentAsync(Comment entity) - { - await this._context.Comments - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) + public async Task GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) { return await this._context.Posts - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && + .FirstOrDefaultAsync(p => p.CreatorId == creatorId && p.TimeCreated == timeCreated); } - - public async Task GetCommentByIdAsync(Guid id) - { - return await this._context.Comments - .FindAsync(id); - } - - public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) - { - return await this._context.Comments - .FirstOrDefaultAsync(p => p.IssuerId == issuerId && - p.TimeCreated == timeCreated); - } - #endregion - - #region Update - public async Task EditCommentAsync(Comment newEntity) - { - this._context.Comments - .Update(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteCommentAsync(Comment entity) - { - this._context.Comments - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } #endregion #region Validations @@ -75,13 +32,6 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(r => r.Id == postId); } - - public async Task DoesCommentExist(Guid id) - { - return await this._context.Comments - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } #endregion } } diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 4364c67..37c3354 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -7,18 +7,19 @@ namespace DevHive.Services.Interfaces { public interface IPostService { - Task CreatePost(CreatePostServiceModel postServiceModel); - Task AddComment(CreateCommentServiceModel commentServiceModel); + Task CreatePost(CreatePostServiceModel createPostServiceModel); + Task AddComment(CreateCommentServiceModel createPostServiceModel); - Task GetCommentById(Guid id); - Task GetPostById(Guid id); + Task GetPostById(Guid id); + Task GetCommentById(Guid id); - Task UpdateComment(UpdateCommentServiceModel commentServiceModel); - Task UpdatePost(UpdatePostServiceModel postServiceModel); + Task UpdatePost(UpdatePostServiceModel updatePostServiceModel); + Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); - Task DeleteComment(Guid id); Task DeletePost(Guid id); + Task DeleteComment(Guid id); + Task ValidateJwtForPost(Guid postId, string rawTokenData); Task ValidateJwtForComment(Guid commentId, string rawTokenData); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 2df3b41..377fe05 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -15,117 +15,141 @@ namespace DevHive.Services.Services { public class PostService : IPostService { - private readonly IPostRepository _postRepository; private readonly IUserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly ICommentRepository _commentRepository; private readonly IMapper _postMapper; - public PostService(IPostRepository postRepository, IUserRepository userRepository, IMapper postMapper) + public PostService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) { - this._postRepository = postRepository; this._userRepository = userRepository; + this._postRepository = postRepository; + this._commentRepository = commentRepository; this._postMapper = postMapper; } - //Create - public async Task CreatePost(CreatePostServiceModel postServiceModel) + #region Create + public async Task CreatePost(CreatePostServiceModel createPostServiceModel) { - Post post = this._postMapper.Map(postServiceModel); + Post post = this._postMapper.Map(createPostServiceModel); + post.TimeCreated = DateTime.Now; bool success = await this._postRepository.AddAsync(post); - if (success) { - Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated); + Post newPost = await this._postRepository + .GetPostByCreatorAndTimeCreatedAsync(createPostServiceModel.IssuerId, createPostServiceModel.TimeCreated); + return newPost.Id; } else return Guid.Empty; } - public async Task AddComment(CreateCommentServiceModel commentServiceModel) + public async Task AddComment(CreateCommentServiceModel createCommentServiceModel) { - commentServiceModel.TimeCreated = DateTime.Now; - Comment comment = this._postMapper.Map(commentServiceModel); + if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); - bool success = await this._postRepository.AddCommentAsync(comment); + Comment comment = this._postMapper.Map(createCommentServiceModel); + createCommentServiceModel.TimeCreated = DateTime.Now; + bool success = await this._commentRepository.AddAsync(comment); if (success) { - Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated); + Comment newComment = await this._commentRepository + .GetCommentByIssuerAndTimeCreatedAsync(createCommentServiceModel.IssuerId, createCommentServiceModel.TimeCreated); + return newComment.Id; } else return Guid.Empty; } + #endregion - //Read - public async Task GetPostById(Guid id) + #region Read + public async Task GetPostById(Guid id) { - Post post = await this._postRepository.GetByIdAsync(id) - ?? throw new ArgumentException("Post does not exist!"); + Post post = await this._postRepository.GetByIdAsync(id) ?? + throw new ArgumentException("The post does not exist!"); - return this._postMapper.Map(post); + return this._postMapper.Map(post); } - public async Task GetCommentById(Guid id) + public async Task GetCommentById(Guid id) { - Comment comment = await this._postRepository.GetCommentByIdAsync(id); - - if (comment == null) + Comment comment = await this._commentRepository.GetByIdAsync(id) ?? throw new ArgumentException("The comment does not exist"); - return this._postMapper.Map(comment); + return this._postMapper.Map(comment); } + #endregion - //Update - public async Task UpdatePost(UpdatePostServiceModel postServiceModel) + #region Update + public async Task UpdatePost(UpdatePostServiceModel updatePostServiceModel) { - if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId)) - throw new ArgumentException("Comment does not exist!"); + if (!await this._postRepository.DoesPostExist(updatePostServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); + + Post post = this._postMapper.Map(updatePostServiceModel); + bool result = await this._postRepository.EditAsync(updatePostServiceModel.PostId, post); - Post post = this._postMapper.Map(postServiceModel); - return await this._postRepository.EditAsync(postServiceModel.Id, post); + if (result) + return (await this._postRepository.GetByIdAsync(updatePostServiceModel.PostId)).Id; + else + return Guid.Empty; } - public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) + public async Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) { - if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id)) + if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) throw new ArgumentException("Comment does not exist!"); - Comment comment = this._postMapper.Map(commentServiceModel); - bool result = await this._postRepository.EditCommentAsync(comment); + Comment comment = this._postMapper.Map(updateCommentServiceModel); + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); - return result; + if (result) + return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; + else + return Guid.Empty; } + #endregion - //Delete + #region Delete public async Task DeletePost(Guid id) { + if (!await this._postRepository.DoesPostExist(id)) + throw new ArgumentException("Post does not exist!"); + Post post = await this._postRepository.GetByIdAsync(id); return await this._postRepository.DeleteAsync(post); } public async Task DeleteComment(Guid id) { - if (!await this._postRepository.DoesCommentExist(id)) + if (!await this._commentRepository.DoesCommentExist(id)) throw new ArgumentException("Comment does not exist!"); - Comment comment = await this._postRepository.GetCommentByIdAsync(id); - bool result = await this._postRepository.DeleteCommentAsync(comment); + Comment comment = await this._commentRepository.GetByIdAsync(id); + return await this._commentRepository.DeleteAsync(comment); + } + #endregion - return result; + #region Validations + public async Task ValidateJwtForPost(Guid postId, string rawTokenData) + { + Post post = await this._postRepository.GetByIdAsync(postId); + User user = await this.GetUserForValidation(rawTokenData); + + return post.CreatorId == user.Id; } - //Validate public async Task ValidateJwtForComment(Guid commentId, string rawTokenData) { - Comment comment = await this._postRepository.GetCommentByIdAsync(commentId); + Comment comment = await this._commentRepository.GetByIdAsync(commentId); User user = await this.GetUserForValidation(rawTokenData); - if (comment.IssuerId != user.Id) - return false; - - return true; + return comment.IssuerId == user.Id; } private async Task GetUserForValidation(string rawTokenData) @@ -151,5 +175,6 @@ namespace DevHive.Services.Services return toReturn; } + #endregion } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index f93f801..bcf16ac 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -16,6 +16,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 8b7344b..8b8b525 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -13,52 +13,56 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] + // [Authorize(Roles = "User")] public class PostController { private readonly IPostService _postService; private readonly IMapper _postMapper; - public PostController(IPostService postService, IMapper mapper) + public PostController(IPostService postService, IMapper postMapper) { this._postService = postService; - this._postMapper = mapper; + this._postMapper = postMapper; } - //Create + #region Create [HttpPost] - [Authorize(Roles = "User")] - public async Task Create([FromBody] CreatePostWebModel createPostModel) + public async Task Create(Guid userId, [FromBody] CreatePostWebModel createPostWebModel) { - CreatePostServiceModel postServiceModel = - this._postMapper.Map(createPostModel); + CreatePostServiceModel createPostServiceModel = + this._postMapper.Map(createPostWebModel); + createPostServiceModel.IssuerId = userId; - Guid id = await this._postService.CreatePost(postServiceModel); + Guid id = await this._postService.CreatePost(createPostServiceModel); return id == Guid.Empty ? - new BadRequestObjectResult("Could not create post") : + new BadRequestObjectResult("Could not create post!") : new OkObjectResult(new { Id = id }); } [HttpPost] [Route("Comment")] - public async Task AddComment([FromBody] CommentWebModel commentWebModel) + public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel) { - CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map(commentWebModel); + CreateCommentServiceModel createCommentServiceModel = + this._postMapper.Map(createCommentWebModel); + createCommentServiceModel.IssuerId = userId; Guid id = await this._postService.AddComment(createCommentServiceModel); return id == Guid.Empty ? - new BadRequestObjectResult("Could not create comment") : + new BadRequestObjectResult("Could not create comment!") : new OkObjectResult(new { Id = id }); } + #endregion - //Read + #region Read [HttpGet] [AllowAnonymous] public async Task GetById(Guid id) { - PostServiceModel postServiceModel = await this._postService.GetPostById(id); - PostWebModel postWebModel = this._postMapper.Map(postServiceModel); + ReadPostServiceModel postServiceModel = await this._postService.GetPostById(id); + ReadPostWebModel postWebModel = this._postMapper.Map(postServiceModel); return new OkObjectResult(postWebModel); } @@ -68,56 +72,58 @@ namespace DevHive.Web.Controllers [AllowAnonymous] public async Task GetCommentById(Guid id) { - CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id); - CommentWebModel commentWebModel = this._postMapper.Map(commentServiceModel); + ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id); + ReadCommentWebModel readCommentWebModel = this._postMapper.Map(readCommentServiceModel); - return new OkObjectResult(commentWebModel); + return new OkObjectResult(readCommentWebModel); } + #endregion - //Update + #region Update [HttpPut] - public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) + public async Task Update(Guid userId, [FromBody] UpdatePostWebModel updatePostWebModel, [FromHeader] string authorization) { - UpdatePostServiceModel postServiceModel = - this._postMapper.Map(updatePostModel); - postServiceModel.IssuerId = id; + if (!await this._postService.ValidateJwtForPost(userId, authorization)) + return new UnauthorizedResult(); - bool result = await this._postService.UpdatePost(postServiceModel); + UpdatePostServiceModel updatePostServiceModel = + this._postMapper.Map(updatePostWebModel); - if (!result) - return new BadRequestObjectResult("Could not update post!"); + Guid id = await this._postService.UpdatePost(updatePostServiceModel); - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update post!") : + new OkObjectResult(new { Id = id }); } [HttpPut] [Route("Comment")] - public async Task UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel, [FromHeader] string authorization) + public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) { - if (!await this._postService.ValidateJwtForComment(id, authorization)) + if (!await this._postService.ValidateJwtForComment(userId, authorization)) return new UnauthorizedResult(); - UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map(commentWebModel); - updateCommentServiceModel.Id = id; - - bool result = await this._postService.UpdateComment(updateCommentServiceModel); + UpdateCommentServiceModel updateCommentServiceModel = + this._postMapper.Map(updateCommentWebModel); - if (!result) - return new BadRequestObjectResult("Could not update Comment"); + Guid id = await this._postService.UpdateComment(updateCommentServiceModel); - return new OkResult(); + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); } + #endregion - //Delete + #region Delete [HttpDelete] - public async Task Delete(Guid id) + public async Task Delete(Guid id, [FromHeader] string authorization) { - bool result = await this._postService.DeletePost(id); - - if (!result) - return new BadRequestObjectResult("Could not delete post!"); + if (!await this._postService.ValidateJwtForPost(id, authorization)) + return new UnauthorizedResult(); - return new OkResult(); + return await this._postService.DeletePost(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); } [HttpDelete] @@ -127,12 +133,10 @@ namespace DevHive.Web.Controllers if (!await this._postService.ValidateJwtForComment(id, authorization)) return new UnauthorizedResult(); - bool result = await this._postService.DeleteComment(id); - - if (!result) - return new BadRequestObjectResult("Could not delete Comment"); - - return new OkResult(); + return await this._postService.DeleteComment(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); } + #endregion } } diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 9c6c094..3d7568b 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -17,10 +17,10 @@ namespace DevHive.Web.Controllers private readonly ITechnologyService _technologyService; private readonly IMapper _technologyMapper; - public TechnologyController(ITechnologyService technologyService, IMapper mapper) + public TechnologyController(ITechnologyService technologyService, IMapper technologyMapper) { this._technologyService = technologyService; - this._technologyMapper = mapper; + this._technologyMapper = technologyMapper; } [HttpPost] diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs index 8e78a48..6dff49e 100644 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs @@ -1,7 +1,11 @@ +using System; + namespace DevHive.Web.Models.Post.Comment { public class UpdateCommentWebModel { + public Guid CommentId { get; set; } + public string NewMessage { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs index 5b66436..fe42715 100644 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs @@ -6,6 +6,10 @@ namespace DevHive.Web.Models.Post.Post { public class UpdatePostWebModel { + [Required] + [NotNull] + public Guid PostId { get; set; } + [NotNull] [Required] public string Message { get; set; } -- cgit v1.2.3 From 3c7da624040169b7597ebc2691cf51943106a2a4 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 28 Jan 2021 21:18:39 +0200 Subject: Users with only the role User can now create comments and posts (while admins can't create them from other people's accounts) --- src/DevHive.Services/Interfaces/IPostService.cs | 1 + src/DevHive.Services/Services/PostService.cs | 7 +++++++ src/DevHive.Web/Controllers/PostController.cs | 11 ++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 37c3354..71b558c 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -19,6 +19,7 @@ namespace DevHive.Services.Interfaces Task DeletePost(Guid id); Task DeleteComment(Guid id); + Task ValidateJwtForCreating(Guid userId, string rawTokenData); Task ValidateJwtForPost(Guid postId, string rawTokenData); Task ValidateJwtForComment(Guid commentId, string rawTokenData); } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index c3dc82f..d80d815 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -169,6 +169,13 @@ namespace DevHive.Services.Services #endregion #region Validations + public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) + { + User user = await this.GetUserForValidation(rawTokenData); + + return user.Id == userId; + } + public async Task ValidateJwtForPost(Guid postId, string rawTokenData) { Post post = await this._postRepository.GetByIdAsync(postId) ?? diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 8bb1d66..0ca041f 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -27,9 +27,11 @@ namespace DevHive.Web.Controllers #region Create [HttpPost] - [Authorize(Roles = "Admin")] - public async Task Create(Guid userId, [FromBody] CreatePostWebModel createPostWebModel) + public async Task Create(Guid userId, [FromBody] CreatePostWebModel createPostWebModel, [FromHeader] string authorization) { + if (await this._postService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + CreatePostServiceModel createPostServiceModel = this._postMapper.Map(createPostWebModel); createPostServiceModel.CreatorId = userId; @@ -43,8 +45,11 @@ namespace DevHive.Web.Controllers [HttpPost] [Route("Comment")] - public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel) + public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) { + if (await this._postService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + CreateCommentServiceModel createCommentServiceModel = this._postMapper.Map(createCommentWebModel); createCommentServiceModel.CreatorId = userId; -- cgit v1.2.3 From ff91162eb83dcf19402240ae8fa06f70cbf2b9e0 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 30 Jan 2021 11:31:21 +0200 Subject: Separated comment models, controler and service from post's --- .../Configurations/Mapping/CommentMappings.cs | 2 +- .../Configurations/Mapping/PostMappings.cs | 2 +- src/DevHive.Services/Interfaces/ICommentService.cs | 20 +++ src/DevHive.Services/Interfaces/IPostService.cs | 10 +- .../Models/Comment/CreateCommentServiceModel.cs | 13 ++ .../Models/Comment/ReadCommentServiceModel.cs | 21 +++ .../Models/Comment/UpdateCommentServiceModel.cs | 15 ++ .../Models/Feed/ReadPageServiceModel.cs | 2 +- .../Post/Comment/CreateCommentServiceModel.cs | 13 -- .../Models/Post/Comment/ReadCommentServiceModel.cs | 21 --- .../Post/Comment/UpdateCommentServiceModel.cs | 15 -- .../Models/Post/CreatePostServiceModel.cs | 15 ++ .../Models/Post/Post/CreatePostServiceModel.cs | 15 -- .../Models/Post/Post/ReadPostServiceModel.cs | 26 ---- .../Models/Post/Post/UpdatePostServiceModel.cs | 17 --- .../Models/Post/ReadPostServiceModel.cs | 26 ++++ .../Models/Post/UpdatePostServiceModel.cs | 17 +++ src/DevHive.Services/Services/CommentService.cs | 156 +++++++++++++++++++++ src/DevHive.Services/Services/FeedService.cs | 2 +- src/DevHive.Services/Services/PostService.cs | 72 +--------- .../Extensions/ConfigureDependencyInjection.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- .../Configurations/Mapping/PostMappings.cs | 4 +- src/DevHive.Web/Controllers/CommentController.cs | 82 +++++++++++ src/DevHive.Web/Controllers/PostController.cs | 67 +-------- .../Models/Comment/CreateCommentWebModel.cs | 17 +++ .../Models/Comment/ReadCommentWebModel.cs | 21 +++ .../Models/Comment/UpdateCommentWebModel.cs | 13 ++ src/DevHive.Web/Models/Feed/ReadPageWebModel.cs | 2 +- .../Models/Post/Comment/CreateCommentWebModel.cs | 17 --- .../Models/Post/Comment/ReadCommentWebModel.cs | 21 --- .../Models/Post/Comment/UpdateCommentWebModel.cs | 13 -- src/DevHive.Web/Models/Post/CreatePostWebModel.cs | 16 +++ .../Models/Post/Post/CreatePostWebModel.cs | 17 --- .../Models/Post/Post/ReadPostWebModel.cs | 26 ---- .../Models/Post/Post/UpdatePostWebModel.cs | 21 --- src/DevHive.Web/Models/Post/ReadPostWebModel.cs | 26 ++++ src/DevHive.Web/Models/Post/UpdatePostWebModel.cs | 21 +++ 38 files changed, 497 insertions(+), 378 deletions(-) create mode 100644 src/DevHive.Services/Interfaces/ICommentService.cs create mode 100644 src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/CreatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/ReadPostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs create mode 100644 src/DevHive.Services/Services/CommentService.cs create mode 100644 src/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/CreatePostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs delete mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/ReadPostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/UpdatePostWebModel.cs (limited to 'src/DevHive.Services/Interfaces/IPostService.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index ac3c8f6..a43b64e 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Comment; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index c7466d9..81e6ecc 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Configurations.Mapping { diff --git a/src/DevHive.Services/Interfaces/ICommentService.cs b/src/DevHive.Services/Interfaces/ICommentService.cs new file mode 100644 index 0000000..e7409a8 --- /dev/null +++ b/src/DevHive.Services/Interfaces/ICommentService.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Comment; + +namespace DevHive.Services.Interfaces +{ + public interface ICommentService + { + Task AddComment(CreateCommentServiceModel createPostServiceModel); + + Task GetCommentById(Guid id); + + Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); + + Task DeleteComment(Guid id); + + Task ValidateJwtForCreating(Guid userId, string rawTokenData); + Task ValidateJwtForComment(Guid commentId, string rawTokenData); + } +} diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs index 71b558c..d35acfd 100644 --- a/src/DevHive.Services/Interfaces/IPostService.cs +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -1,26 +1,20 @@ using System; using System.Threading.Tasks; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Interfaces { - public interface IPostService + public interface IPostService { Task CreatePost(CreatePostServiceModel createPostServiceModel); - Task AddComment(CreateCommentServiceModel createPostServiceModel); Task GetPostById(Guid id); - Task GetCommentById(Guid id); Task UpdatePost(UpdatePostServiceModel updatePostServiceModel); - Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel); Task DeletePost(Guid id); - Task DeleteComment(Guid id); Task ValidateJwtForCreating(Guid userId, string rawTokenData); Task ValidateJwtForPost(Guid postId, string rawTokenData); - Task ValidateJwtForComment(Guid commentId, string rawTokenData); } } diff --git a/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs new file mode 100644 index 0000000..30e919b --- /dev/null +++ b/src/DevHive.Services/Models/Comment/CreateCommentServiceModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class CreateCommentServiceModel + { + public Guid PostId { get; set; } + + public Guid CreatorId { get; set; } + + public string Message { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs new file mode 100644 index 0000000..3196233 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/ReadCommentServiceModel.cs @@ -0,0 +1,21 @@ +using System; + +namespace DevHive.Services.Models.Comment +{ + public class ReadCommentServiceModel + { + public Guid CommentId { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public Guid PostId { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs new file mode 100644 index 0000000..3b78200 --- /dev/null +++ b/src/DevHive.Services/Models/Comment/UpdateCommentServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models.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/Feed/ReadPageServiceModel.cs b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs index f291de7..95f6845 100644 --- a/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs +++ b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Models { diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs deleted file mode 100644 index 8d49659..0000000 --- a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class CreateCommentServiceModel - { - public Guid PostId { get; set; } - - public Guid CreatorId { get; set; } - - public string Message { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs deleted file mode 100644 index 12e29a0..0000000 --- a/src/DevHive.Services/Models/Post/Comment/ReadCommentServiceModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Post.Comment -{ - public class ReadCommentServiceModel - { - public Guid CommentId { get; set; } - - public string IssuerFirstName { get; set; } - - public string IssuerLastName { get; set; } - - public string IssuerUsername { get; set; } - - public Guid PostId { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs deleted file mode 100644 index 3827d4d..0000000 --- a/src/DevHive.Services/Models/Post/Comment/UpdateCommentServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -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/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/CreatePostServiceModel.cs new file mode 100644 index 0000000..304eb90 --- /dev/null +++ b/src/DevHive.Services/Models/Post/CreatePostServiceModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Post +{ + public class CreatePostServiceModel + { + public Guid CreatorId { get; set; } + + public string Message { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs deleted file mode 100644 index 8676f6c..0000000 --- a/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Services.Models.Post.Post -{ - public class CreatePostServiceModel - { - public Guid CreatorId { get; set; } - - public string Message { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs deleted file mode 100644 index f0a4fe5..0000000 --- a/src/DevHive.Services/Models/Post/Post/ReadPostServiceModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using DevHive.Services.Models.Post.Comment; -using Microsoft.Extensions.FileProviders; - -namespace DevHive.Services.Models.Post.Post -{ - public class ReadPostServiceModel - { - public Guid PostId { 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 Comments { get; set; } = new(); - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs deleted file mode 100644 index 24b0b74..0000000 --- a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Services.Models.Post.Post -{ - public class UpdatePostServiceModel - { - public Guid PostId { get; set; } - - public Guid CreatorId { get; set; } - - public string NewMessage { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs b/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs new file mode 100644 index 0000000..04ec6bd --- /dev/null +++ b/src/DevHive.Services/Models/Post/ReadPostServiceModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Services.Models.Comment; +using Microsoft.Extensions.FileProviders; + +namespace DevHive.Services.Models.Post +{ + public class ReadPostServiceModel + { + public Guid PostId { 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 Comments { get; set; } = new(); + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs new file mode 100644 index 0000000..51b16bc --- /dev/null +++ b/src/DevHive.Services/Models/Post/UpdatePostServiceModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Post +{ + public class UpdatePostServiceModel + { + public Guid PostId { get; set; } + + public Guid CreatorId { get; set; } + + public string NewMessage { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs new file mode 100644 index 0000000..e0eb88a --- /dev/null +++ b/src/DevHive.Services/Services/CommentService.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Comment; +using System.IdentityModel.Tokens.Jwt; +using System.Security.Claims; +using DevHive.Services.Interfaces; +using DevHive.Data.Interfaces.Repositories; +using System.Linq; + +namespace DevHive.Services.Services +{ + public class CommentService : ICommentService + { + private readonly IUserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly ICommentRepository _commentRepository; + private readonly IMapper _postMapper; + + public CommentService(IUserRepository userRepository, IPostRepository postRepository, ICommentRepository commentRepository, IMapper postMapper) + { + this._userRepository = userRepository; + this._postRepository = postRepository; + this._commentRepository = commentRepository; + this._postMapper = postMapper; + } + + #region Create + public async Task AddComment(CreateCommentServiceModel createCommentServiceModel) + { + if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) + throw new ArgumentException("Post does not exist!"); + + Comment comment = this._postMapper.Map(createCommentServiceModel); + comment.TimeCreated = DateTime.Now; + + comment.Creator = await this._userRepository.GetByIdAsync(createCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); + + bool success = await this._commentRepository.AddAsync(comment); + if (success) + { + Comment newComment = await this._commentRepository + .GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); + + return newComment.Id; + } + else + return Guid.Empty; + } + #endregion + + #region Read + public async Task GetCommentById(Guid id) + { + Comment comment = await this._commentRepository.GetByIdAsync(id) ?? + throw new ArgumentException("The comment does not exist"); + + User user = await this._userRepository.GetByIdAsync(comment.Creator.Id) ?? + throw new ArgumentException("The user does not exist"); + + ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); + readCommentServiceModel.IssuerFirstName = user.FirstName; + readCommentServiceModel.IssuerLastName = user.LastName; + readCommentServiceModel.IssuerUsername = user.UserName; + + return readCommentServiceModel; + } + #endregion + + #region Update + public async Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) + { + if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._postMapper.Map(updateCommentServiceModel); + comment.TimeCreated = DateTime.Now; + + comment.Creator = await this._userRepository.GetByIdAsync(updateCommentServiceModel.CreatorId); + comment.Post = await this._postRepository.GetByIdAsync(updateCommentServiceModel.PostId); + + bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); + + if (result) + return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; + else + return Guid.Empty; + } + #endregion + + #region Delete + public async Task DeleteComment(Guid id) + { + if (!await this._commentRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._commentRepository.GetByIdAsync(id); + return await this._commentRepository.DeleteAsync(comment); + } + #endregion + + #region Validations + public async Task ValidateJwtForCreating(Guid userId, string rawTokenData) + { + User user = await this.GetUserForValidation(rawTokenData); + + return user.Id == userId; + } + + public async Task ValidateJwtForComment(Guid commentId, string rawTokenData) + { + Comment comment = await this._commentRepository.GetByIdAsync(commentId) ?? + throw new ArgumentException("Comment does not exist!"); + User user = await this.GetUserForValidation(rawTokenData); + + //If user made the comment + if (comment.Creator.Id == 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 GetUserForValidation(string rawTokenData) + { + JwtSecurityToken jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); + + Guid jwtUserId = Guid.Parse(this.GetClaimTypeValues("ID", jwt.Claims).First()); + //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + + User user = await this._userRepository.GetByIdAsync(jwtUserId) ?? + throw new ArgumentException("User does not exist!"); + + return user; + } + + + private List GetClaimTypeValues(string type, IEnumerable claims) + { + List toReturn = new(); + + foreach (var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + #endregion + } +} + diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs index 1bddac4..269471e 100644 --- a/src/DevHive.Services/Services/FeedService.cs +++ b/src/DevHive.Services/Services/FeedService.cs @@ -7,7 +7,7 @@ using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; using DevHive.Services.Models; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; namespace DevHive.Services.Services { diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 7ce7b58..0eaac94 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -3,8 +3,7 @@ using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Services.Models.Post.Comment; -using DevHive.Services.Models.Post.Post; +using DevHive.Services.Models.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using DevHive.Services.Interfaces; @@ -13,7 +12,7 @@ using System.Linq; namespace DevHive.Services.Services { - public class PostService : IPostService + public class PostService : IPostService { private readonly ICloudService _cloudService; private readonly IUserRepository _userRepository; @@ -55,29 +54,6 @@ namespace DevHive.Services.Services else return Guid.Empty; } - - public async Task AddComment(CreateCommentServiceModel createCommentServiceModel) - { - if (!await this._postRepository.DoesPostExist(createCommentServiceModel.PostId)) - throw new ArgumentException("Post does not exist!"); - - Comment comment = this._postMapper.Map(createCommentServiceModel); - comment.TimeCreated = DateTime.Now; - - comment.Creator = await this._userRepository.GetByIdAsync(createCommentServiceModel.CreatorId); - comment.Post = await this._postRepository.GetByIdAsync(createCommentServiceModel.PostId); - - bool success = await this._commentRepository.AddAsync(comment); - if (success) - { - Comment newComment = await this._commentRepository - .GetCommentByIssuerAndTimeCreatedAsync(comment.Creator.Id, comment.TimeCreated); - - return newComment.Id; - } - else - return Guid.Empty; - } #endregion #region Read @@ -96,22 +72,6 @@ namespace DevHive.Services.Services return readPostServiceModel; } - - public async Task GetCommentById(Guid id) - { - Comment comment = await this._commentRepository.GetByIdAsync(id) ?? - throw new ArgumentException("The comment does not exist"); - - User user = await this._userRepository.GetByIdAsync(comment.Creator.Id) ?? - throw new ArgumentException("The user does not exist"); - - ReadCommentServiceModel readCommentServiceModel = this._postMapper.Map(comment); - readCommentServiceModel.IssuerFirstName = user.FirstName; - readCommentServiceModel.IssuerLastName = user.LastName; - readCommentServiceModel.IssuerUsername = user.UserName; - - return readCommentServiceModel; - } #endregion #region Update @@ -146,25 +106,6 @@ namespace DevHive.Services.Services else return Guid.Empty; } - - public async Task UpdateComment(UpdateCommentServiceModel updateCommentServiceModel) - { - if (!await this._commentRepository.DoesCommentExist(updateCommentServiceModel.CommentId)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = this._postMapper.Map(updateCommentServiceModel); - comment.TimeCreated = DateTime.Now; - - comment.Creator = await this._userRepository.GetByIdAsync(updateCommentServiceModel.CreatorId); - comment.Post = await this._postRepository.GetByIdAsync(updateCommentServiceModel.PostId); - - bool result = await this._commentRepository.EditAsync(updateCommentServiceModel.CommentId, comment); - - if (result) - return (await this._commentRepository.GetByIdAsync(updateCommentServiceModel.CommentId)).Id; - else - return Guid.Empty; - } #endregion #region Delete @@ -185,15 +126,6 @@ namespace DevHive.Services.Services return await this._postRepository.DeleteAsync(post); } - - public async Task DeleteComment(Guid id) - { - if (!await this._commentRepository.DoesCommentExist(id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = await this._commentRepository.GetByIdAsync(id); - return await this._commentRepository.DeleteAsync(comment); - } #endregion #region Validations diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index fe2c788..8ba0d69 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -1,5 +1,4 @@ using DevHive.Data.Interfaces.Repositories; -using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Interfaces; using DevHive.Services.Services; @@ -8,7 +7,7 @@ using Microsoft.Extensions.DependencyInjection; namespace DevHive.Web.Configurations.Extensions { - public static class ConfigureDependencyInjection + public static class ConfigureDependencyInjection { public static void DependencyInjectionConfiguration(this IServiceCollection services, IConfiguration configuration) { @@ -25,6 +24,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(options => new CloudinaryService( diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index a28ee16..b8d6829 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; -using DevHive.Services.Models.Post.Comment; -using DevHive.Web.Models.Post.Comment; +using DevHive.Services.Models.Comment; +using DevHive.Web.Models.Comment; namespace DevHive.Web.Configurations.Mapping { @@ -15,6 +15,3 @@ namespace DevHive.Web.Configurations.Mapping } } } - - - diff --git a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs index bc7bc06..a5b46ee 100644 --- a/src/DevHive.Web/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; -using DevHive.Services.Models.Post.Post; -using DevHive.Web.Models.Post.Post; +using DevHive.Services.Models.Post; +using DevHive.Web.Models.Post; namespace DevHive.Web.Configurations.Mapping { diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs new file mode 100644 index 0000000..ebcb87a --- /dev/null +++ b/src/DevHive.Web/Controllers/CommentController.cs @@ -0,0 +1,82 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Comment; +using DevHive.Services.Models.Comment; +using Microsoft.AspNetCore.Authorization; +using DevHive.Services.Interfaces; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + [Authorize(Roles = "User,Admin")] + public class CommentController { + private readonly ICommentService _commentService; + private readonly IMapper _commentMapper; + + public CommentController(ICommentService commentService, IMapper commentMapper) + { + this._commentService = commentService; + this._commentMapper = commentMapper; + } + + [HttpPost] + public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForCreating(userId, authorization)) + return new UnauthorizedResult(); + + CreateCommentServiceModel createCommentServiceModel = + this._commentMapper.Map(createCommentWebModel); + createCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.AddComment(createCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Could not create comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpGet] + [AllowAnonymous] + public async Task GetCommentById(Guid id) + { + ReadCommentServiceModel readCommentServiceModel = await this._commentService.GetCommentById(id); + ReadCommentWebModel readCommentWebModel = this._commentMapper.Map(readCommentServiceModel); + + return new OkObjectResult(readCommentWebModel); + } + + [HttpPut] + public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) + return new UnauthorizedResult(); + + UpdateCommentServiceModel updateCommentServiceModel = + this._commentMapper.Map(updateCommentWebModel); + updateCommentServiceModel.CreatorId = userId; + + Guid id = await this._commentService.UpdateComment(updateCommentServiceModel); + + return id == Guid.Empty ? + new BadRequestObjectResult("Unable to update comment!") : + new OkObjectResult(new { Id = id }); + } + + [HttpDelete] + public async Task DeleteComment(Guid id, [FromHeader] string authorization) + { + if (!await this._commentService.ValidateJwtForComment(id, authorization)) + return new UnauthorizedResult(); + + return await this._commentService.DeleteComment(id) ? + new OkResult() : + new BadRequestObjectResult("Could not delete Comment"); + } + + } +} + diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index fe71519..53adfce 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -2,16 +2,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using AutoMapper; using System; -using DevHive.Web.Models.Post.Post; -using DevHive.Services.Models.Post.Post; -using DevHive.Web.Models.Post.Comment; -using DevHive.Services.Models.Post.Comment; +using DevHive.Web.Models.Post; +using DevHive.Services.Models.Post; using Microsoft.AspNetCore.Authorization; using DevHive.Services.Interfaces; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User,Admin")] public class PostController @@ -42,24 +40,6 @@ namespace DevHive.Web.Controllers new BadRequestObjectResult("Could not create post!") : new OkObjectResult(new { Id = id }); } - - [HttpPost] - [Route("Comment")] - public async Task AddComment(Guid userId, [FromBody] CreateCommentWebModel createCommentWebModel, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForCreating(userId, authorization)) - return new UnauthorizedResult(); - - CreateCommentServiceModel createCommentServiceModel = - this._postMapper.Map(createCommentWebModel); - createCommentServiceModel.CreatorId = userId; - - Guid id = await this._postService.AddComment(createCommentServiceModel); - - return id == Guid.Empty ? - new BadRequestObjectResult("Could not create comment!") : - new OkObjectResult(new { Id = id }); - } #endregion #region Read @@ -72,17 +52,6 @@ namespace DevHive.Web.Controllers return new OkObjectResult(postWebModel); } - - [HttpGet] - [Route("Comment")] - [AllowAnonymous] - public async Task GetCommentById(Guid id) - { - ReadCommentServiceModel readCommentServiceModel = await this._postService.GetCommentById(id); - ReadCommentWebModel readCommentWebModel = this._postMapper.Map(readCommentServiceModel); - - return new OkObjectResult(readCommentWebModel); - } #endregion #region Update @@ -102,24 +71,6 @@ namespace DevHive.Web.Controllers new BadRequestObjectResult("Unable to update post!") : new OkObjectResult(new { Id = id }); } - - [HttpPut] - [Route("Comment")] - public async Task UpdateComment(Guid userId, [FromBody] UpdateCommentWebModel updateCommentWebModel, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForComment(updateCommentWebModel.CommentId, authorization)) - return new UnauthorizedResult(); - - UpdateCommentServiceModel updateCommentServiceModel = - this._postMapper.Map(updateCommentWebModel); - updateCommentServiceModel.CreatorId = userId; - - Guid id = await this._postService.UpdateComment(updateCommentServiceModel); - - return id == Guid.Empty ? - new BadRequestObjectResult("Unable to update comment!") : - new OkObjectResult(new { Id = id }); - } #endregion #region Delete @@ -133,18 +84,6 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestObjectResult("Could not delete Comment"); } - - [HttpDelete] - [Route("Comment")] - public async Task DeleteComment(Guid id, [FromHeader] string authorization) - { - if (!await this._postService.ValidateJwtForComment(id, authorization)) - return new UnauthorizedResult(); - - return await this._postService.DeleteComment(id) ? - new OkResult() : - new BadRequestObjectResult("Could not delete Comment"); - } #endregion } } diff --git a/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs b/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs new file mode 100644 index 0000000..8b2bf8d --- /dev/null +++ b/src/DevHive.Web/Models/Comment/CreateCommentWebModel.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Comment +{ + public class CreateCommentWebModel + { + [NotNull] + [Required] + public Guid PostId { get; set; } + + [NotNull] + [Required] + public string Message { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs b/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs new file mode 100644 index 0000000..4d3aff7 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/ReadCommentWebModel.cs @@ -0,0 +1,21 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class ReadCommentWebModel + { + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public string Message { get; set; } + + public DateTime TimeCreated { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs new file mode 100644 index 0000000..b5d7970 --- /dev/null +++ b/src/DevHive.Web/Models/Comment/UpdateCommentWebModel.cs @@ -0,0 +1,13 @@ +using System; + +namespace DevHive.Web.Models.Comment +{ + public class UpdateCommentWebModel + { + public Guid CommentId { get; set; } + + public Guid PostId { get; set; } + + public string NewMessage { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs index 40d29c9..839aaa6 100644 --- a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs +++ b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using DevHive.Web.Models.Post.Post; +using DevHive.Web.Models.Post; namespace DevHive.Web.Controllers { diff --git a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs deleted file mode 100644 index 85c67bf..0000000 --- a/src/DevHive.Web/Models/Post/Comment/CreateCommentWebModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; - -namespace DevHive.Web.Models.Post.Comment -{ - public class CreateCommentWebModel - { - [NotNull] - [Required] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public string Message { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs deleted file mode 100644 index 5320c3c..0000000 --- a/src/DevHive.Web/Models/Post/Comment/ReadCommentWebModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Post.Comment -{ - public class ReadCommentWebModel - { - public Guid CommentId { get; set; } - - public Guid PostId { get; set; } - - public string IssuerFirstName { get; set; } - - public string IssuerLastName { get; set; } - - public string IssuerUsername { get; set; } - - public string Message { get; set; } - - public DateTime TimeCreated { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs deleted file mode 100644 index cb1c60a..0000000 --- a/src/DevHive.Web/Models/Post/Comment/UpdateCommentWebModel.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Post.Comment -{ - public class UpdateCommentWebModel - { - public Guid CommentId { get; set; } - - public Guid PostId { get; set; } - - public string NewMessage { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/CreatePostWebModel.cs new file mode 100644 index 0000000..256055a --- /dev/null +++ b/src/DevHive.Web/Models/Post/CreatePostWebModel.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class CreatePostWebModel + { + [NotNull] + [Required] + public string Message { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs deleted file mode 100644 index e35a813..0000000 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class CreatePostWebModel - { - [NotNull] - [Required] - public string Message { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs b/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs deleted file mode 100644 index 5d4da31..0000000 --- a/src/DevHive.Web/Models/Post/Post/ReadPostWebModel.cs +++ /dev/null @@ -1,26 +0,0 @@ -using System; -using System.Collections.Generic; -using DevHive.Web.Models.Post.Comment; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class ReadPostWebModel - { - public Guid PostId { 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 Comments { get; set; } - - public List Files { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs deleted file mode 100644 index ac84d2c..0000000 --- a/src/DevHive.Web/Models/Post/Post/UpdatePostWebModel.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using Microsoft.AspNetCore.Http; - -namespace DevHive.Web.Models.Post.Post -{ - public class UpdatePostWebModel - { - [Required] - [NotNull] - public Guid PostId { get; set; } - - [NotNull] - [Required] - public string NewMessage { get; set; } - - public List Files { get; set; } = new(); - } -} diff --git a/src/DevHive.Web/Models/Post/ReadPostWebModel.cs b/src/DevHive.Web/Models/Post/ReadPostWebModel.cs new file mode 100644 index 0000000..1d2669e --- /dev/null +++ b/src/DevHive.Web/Models/Post/ReadPostWebModel.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using DevHive.Web.Models.Comment; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class ReadPostWebModel + { + public Guid PostId { 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 Comments { get; set; } + + public List Files { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs b/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs new file mode 100644 index 0000000..a0c9b61 --- /dev/null +++ b/src/DevHive.Web/Models/Post/UpdatePostWebModel.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Post +{ + public class UpdatePostWebModel + { + [Required] + [NotNull] + public Guid PostId { get; set; } + + [NotNull] + [Required] + public string NewMessage { get; set; } + + public List Files { get; set; } = new(); + } +} -- cgit v1.2.3