From d80b44003ca03cd09bf28278bf2e243581c00332 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 10:23:15 +0200 Subject: Fixed GetById to return only public info --- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 src/DevHive.Web/Models/Identity/User/UserWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs new file mode 100644 index 0000000..e070d44 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -0,0 +1,11 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class UserWebModel + { + public string UserName { get; set; } + public string Email { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Role { get; set; } + } +} -- cgit v1.2.3 From 47c2a91612b54329ba0353b1296846bafc0e9775 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 16 Dec 2020 13:04:07 +0200 Subject: Changed models in Web to use inheritance of most repeated properties --- .../Models/Identity/User/BaseUserWebModel.cs | 30 ++++++++++++++++++++++ .../Models/Identity/User/RegisterWebModel.cs | 26 ++----------------- .../Models/Identity/User/UpdateUserWebModel.cs | 9 +++++-- .../Models/Identity/User/UserWebModel.cs | 6 +---- 4 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs new file mode 100644 index 0000000..ff9fac5 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -0,0 +1,30 @@ +using System.ComponentModel.DataAnnotations; +using DevHive.Web.Models.Identity.Validation; + +namespace DevHive.Web.Models.Identity.User +{ + public class BaseUserWebModel + { + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } + + [Required] + [EmailAddress] + public string Email { get; set; } + + [Required] + [MinLength(3)] + [MaxLength(30)] + [OnlyLetters(ErrorMessage = "First name can only contain letters!")] + public string FirstName { get; set; } + + [Required] + [MinLength(3)] + [MaxLength(30)] + [OnlyLetters(ErrorMessage = "Last name can only contain letters!")] + public string LastName { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 60e860a..22b178b 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -3,32 +3,10 @@ using DevHive.Web.Models.Identity.Validation; namespace DevHive.Web.Models.Identity.User { - public class RegisterWebModel + public class RegisterWebModel : BaseUserWebModel { - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string UserName { get; set; } - - [Required] - [EmailAddress] - public string Email { get; set; } - - [Required] - [MinLength(3)] - [MaxLength(30)] - [OnlyLetters(ErrorMessage = "First name can only contain letters!")] - public string FirstName { get; set; } - - [Required] - [MinLength(3)] - [MaxLength(30)] - [OnlyLetters(ErrorMessage = "Last name can only contain letters!")] - public string LastName { get; set; } - [Required] [GoodPassword] - public string Password { get; set; } + public virtual string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index e04e7da..fbe02a5 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,7 +1,12 @@ +using System.ComponentModel.DataAnnotations; +using DevHive.Web.Models.Identity.Validation; + namespace DevHive.Web.Models.Identity.User { - public class UpdateUserWebModel : UserWebModel + public class UpdateUserWebModel : BaseUserWebModel { - public string Password { get; set; } + [Required] + [GoodPassword] + public virtual string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index e070d44..88ee1b7 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,11 +1,7 @@ namespace DevHive.Web.Models.Identity.User { - public class UserWebModel + public class UserWebModel : BaseUserWebModel { - public string UserName { get; set; } - public string Email { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } public string Role { get; set; } } } -- cgit v1.2.3 From 94a3b0661106e91ab3a1a523af3c60df131a4f63 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:13:01 +0200 Subject: Friends implementation added. UserController/AddAFriend added --- src/DevHive.Common/Models/IdModel.cs | 9 + src/DevHive.Data/Models/User.cs | 4 +- src/DevHive.Data/Repositories/DevHiveContext.cs | 8 +- src/DevHive.Data/Repositories/UserRepository.cs | 9 + .../Models/Identity/User/UserServiceModel.cs | 7 +- src/DevHive.Services/Services/UserService.cs | 22 +- .../Configurations/Mapping/RoleMappings.cs | 1 + src/DevHive.Web/Controllers/UserController.cs | 11 + src/DevHive.Web/DevHive.Web.csproj | 22 +- .../20201217163729_Friends_Implemented.Designer.cs | 364 +++++++++++++++++++++ .../20201217163729_Friends_Implemented.cs | 45 +++ .../Migrations/DevHiveContextModelSnapshot.cs | 17 + .../Models/Identity/User/UserWebModel.cs | 6 +- 13 files changed, 505 insertions(+), 20 deletions(-) create mode 100644 src/DevHive.Common/Models/IdModel.cs create mode 100644 src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs create mode 100644 src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Common/Models/IdModel.cs b/src/DevHive.Common/Models/IdModel.cs new file mode 100644 index 0000000..a342e6d --- /dev/null +++ b/src/DevHive.Common/Models/IdModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Common.Models +{ + public class IdModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index fd0ee7b..eef0af2 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -14,8 +14,8 @@ namespace DevHive.Data.Models public string ProfilePicture { get; set; } - public virtual IList Roles { get; set; } + public virtual IList Roles { get; set; } = new List(); - //public List Friends { get; set; } + public IList Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Data/Repositories/DevHiveContext.cs b/src/DevHive.Data/Repositories/DevHiveContext.cs index 373eb5e..7fa8130 100644 --- a/src/DevHive.Data/Repositories/DevHiveContext.cs +++ b/src/DevHive.Data/Repositories/DevHiveContext.cs @@ -18,7 +18,13 @@ namespace DevHive.Data.Repositories builder.Entity() .HasIndex(x => x.UserName) .IsUnique(); - + + builder.Entity() + .HasMany(x => x.Roles); + + builder.Entity() + .HasMany(x => x.Friends); + builder.Entity() .HasIndex(x => x.Id) .IsUnique(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 130f96e..577f02f 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -43,6 +43,7 @@ namespace DevHive.Data.Repositories return await this._context .Set() .Include(x => x.Roles) + .Include(x => x.Friends) .FirstOrDefaultAsync(x => x.Id == id); } @@ -115,5 +116,13 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task AddFriend(User user, User friend) + { + this._context.Update(user); + user.Friends.Add(friend); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 576e502..868e7ba 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,7 +1,12 @@ +using System.Collections.Generic; +using DevHive.Data.Models; +using DevHive.Services.Models.Identity.Role; + namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public string Role { get; set;} + public IList Role { get; set; } = new List(); + public List Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index bd9eaa6..3e65dab 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -72,7 +72,13 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - return this._userMapper.Map(user); + //Here User has 1 role + + UserServiceModel model = this._userMapper.Map(user); + + //here model has 0 roles + + return model; } public async Task UpdateUser(UpdateUserServiceModel updateModel) @@ -105,6 +111,16 @@ namespace DevHive.Services.Services throw new InvalidOperationException("Unable to delete user!"); } + public async Task AddFriend(Guid userId, Guid friendId) + { + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriend(user, friend) : + throw new ArgumentException("Invalid user!"); + } + private string GeneratePasswordHash(string password) { return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); @@ -114,9 +130,9 @@ namespace DevHive.Services.Services { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List claims = new List() + List claims = new() { - new Claim(ClaimTypes.Role, roles[0].Name) // TODO: add support for mulitple roles + new Claim(ClaimTypes.Role, roles[0].Name) // TODO: add support for multiple roles }; SecurityTokenDescriptor tokenDescriptor = new() diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2743df3..4bc25c4 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -10,6 +10,7 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 1360bfe..cdb8dc1 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,6 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; +using DevHive.Common.Models; namespace DevHive.Web.Controllers { @@ -39,6 +40,7 @@ namespace DevHive.Web.Controllers return new OkObjectResult(tokenWebModel); } + //Create [HttpPost] [Route("Register")] [AllowAnonymous] @@ -52,6 +54,15 @@ namespace DevHive.Web.Controllers return new CreatedResult("Register", tokenWebModel); } + [HttpPost] + [Route("AddAFriend")] + public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) + { + return await this._userService.AddFriend(userId, friendIdModel.Id) ? + new OkResult() : + new BadRequestResult(); + } + //Read [HttpGet] public async Task GetById(Guid id) diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj index 0be5bdc..ad23ce9 100644 --- a/src/DevHive.Web/DevHive.Web.csproj +++ b/src/DevHive.Web/DevHive.Web.csproj @@ -3,23 +3,21 @@ net5.0 - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - - - + + + + + + - - + + \ No newline at end of file diff --git a/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs new file mode 100644 index 0000000..d066f18 --- /dev/null +++ b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.Designer.cs @@ -0,0 +1,364 @@ +// +using System; +using DevHive.Data.Repositories; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Web.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20201217163729_Friends_Implemented")] + partial class Friends_Implemented + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePicture") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("ProviderKey") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Name") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs new file mode 100644 index 0000000..e20bfc1 --- /dev/null +++ b/src/DevHive.Web/Migrations/20201217163729_Friends_Implemented.cs @@ -0,0 +1,45 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Web.Migrations +{ + public partial class Friends_Implemented : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "UserId", + table: "AspNetUsers", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + table: "AspNetUsers"); + + migrationBuilder.DropIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "AspNetUsers"); + } + } +} diff --git a/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs index c1464d4..328eb9c 100644 --- a/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Web/Migrations/DevHiveContextModelSnapshot.cs @@ -135,6 +135,9 @@ namespace DevHive.Web.Migrations b.Property("TwoFactorEnabled") .HasColumnType("boolean"); + b.Property("UserId") + .HasColumnType("uuid"); + b.Property("UserName") .HasMaxLength(256) .HasColumnType("character varying(256)"); @@ -148,6 +151,8 @@ namespace DevHive.Web.Migrations .IsUnique() .HasDatabaseName("UserNameIndex"); + b.HasIndex("UserId"); + b.HasIndex("UserName") .IsUnique(); @@ -274,6 +279,13 @@ namespace DevHive.Web.Migrations b.ToTable("RoleUser"); }); + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => { b.HasOne("DevHive.Data.Models.Role", null) @@ -339,6 +351,11 @@ namespace DevHive.Web.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + }); #pragma warning restore 612, 618 } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 88ee1b7..e7bdb2a 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,7 +1,11 @@ +using System.Collections.Generic; +using DevHive.Web.Models.Identity.Role; + namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public string Role { get; set; } + public IList Role { get; set; } = new List(); + public IList Friends { get; set; } = new List(); } } -- cgit v1.2.3 From 33a1a5899a16378691cd62d9ee4644db2a02e2b7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:37:38 +0200 Subject: RoleModel moved to DevHive.Common --- src/DevHive.Common/Models/Identity/RoleModel.cs | 10 ++++++++++ src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs | 10 ---------- src/DevHive.Services/Models/Identity/User/UserServiceModel.cs | 3 ++- .../Configurations/Extensions/ConfigureAutoMapper.cs | 9 +++++++++ src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs | 10 ---------- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 3 ++- src/DevHive.Web/Startup.cs | 2 +- 7 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs delete mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Common/Models/Identity/RoleModel.cs b/src/DevHive.Common/Models/Identity/RoleModel.cs new file mode 100644 index 0000000..5db8df9 --- /dev/null +++ b/src/DevHive.Common/Models/Identity/RoleModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Common.Models.Identity +{ + public class RoleModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs deleted file mode 100644 index 3f834ef..0000000 --- a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Identity.Role -{ - public class RoleServiceModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 868e7ba..8ac71b1 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using DevHive.Common.Models.Identity; using DevHive.Data.Models; using DevHive.Services.Models.Identity.Role; @@ -6,7 +7,7 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Role { get; set; } = new List(); + public IList Role { get; set; } = new List(); public List Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs index afba39c..b6ebc63 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureAutoMapper.cs @@ -1,6 +1,7 @@ using System; using AutoMapper; using AutoMapper.Configuration; +using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; namespace DevHive.Web.Configurations.Extensions @@ -11,5 +12,13 @@ namespace DevHive.Web.Configurations.Extensions { services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); } + + public static void UseAutoMapperConfiguration(this IApplicationBuilder app) + { + var config = new MapperConfiguration(cfg => + { + cfg.AllowNullCollections = true; + }); + } } } \ No newline at end of file diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs deleted file mode 100644 index d8ae465..0000000 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Identity.Role -{ - public class RoleWebModel - { - public Guid Id { get; set; } - public string Name { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index e7bdb2a..83430ce 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,11 +1,12 @@ using System.Collections.Generic; +using DevHive.Common.Models.Identity; using DevHive.Web.Models.Identity.Role; namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList Role { get; set; } = new List(); + public IList Role { get; set; } = new List(); public IList Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index fac1b4a..66fde9e 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -50,8 +50,8 @@ namespace DevHive.Web } app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); - app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( -- cgit v1.2.3 From b24b9b1ffa528f29aa87f0e48097a35386fbb8b1 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 20:15:31 +0200 Subject: Fixed roles not mapping properly --- src/DevHive.Services/Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index de57faf..953b038 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -5,7 +5,7 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Role { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public List Friends { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 83430ce..4b523b5 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -6,7 +6,7 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList Role { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); } } -- cgit v1.2.3 From 8cc02d6195784ca2783b7ae8daa1cf9fd20902a9 Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 18 Dec 2020 08:04:47 +0200 Subject: Fixed Id showing in web model --- src/DevHive.Web/Configurations/Mapping/RoleMappings.cs | 4 ++-- src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs | 10 ++++++++++ src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 2 +- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 8c5cd85..5d33c56 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,8 +11,8 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs new file mode 100644 index 0000000..7a3ed96 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Identity.Role +{ + public class RoleWebModel + { + public string Name { get; set; } + } +} +2 \ No newline at end of file diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 4b523b5..37d6553 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -6,7 +6,7 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { - public IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); } } -- cgit v1.2.3 From 8e11fbaa79ad6fba234e8162c5b291174ed31fbb Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 12 Jan 2021 23:07:13 +0200 Subject: Fixed bugs with Lang features, User's GetById, Lang & User mappings; Added more UserService validations --- src/DevHive.Data/Repositories/UserRepository.cs | 9 ++++- .../Configurations/Mapping/LanguageMappings.cs | 5 +++ .../Models/Identity/User/UserServiceModel.cs | 6 +++- .../Models/Language/CreateLanguageServiceModel.cs | 2 +- .../Models/Language/UpdateLanguageServiceModel.cs | 7 +++- src/DevHive.Services/Services/LanguageService.cs | 7 ++-- src/DevHive.Services/Services/UserService.cs | 42 +++++++++++----------- .../Extensions/ConfigureDependencyInjection.cs | 2 -- src/DevHive.Web/Controllers/ErrorController.cs | 3 ++ src/DevHive.Web/Controllers/UserController.cs | 2 ++ .../Models/Identity/User/UserWebModel.cs | 4 +++ .../Models/Language/CreateLanguageWebModel.cs | 2 +- src/DevHive.code-workspace | 3 ++ 13 files changed, 62 insertions(+), 32 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index e3c1304..64a81ae 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -67,8 +67,10 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { return await this._context.Users - .Include(x => x.Roles) .Include(x => x.Friends) + .Include(x => x.Roles) + .Include(x => x.Langauges) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.Id == id); } @@ -218,6 +220,11 @@ namespace DevHive.Data.Repositories { return user.Friends.Count >= 1; } + + public bool DoesUserHaveThisLanguage(User user, Language language) + { + return user.Langauges.Contains(language); + } #endregion } } diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs index 0be9ca2..e483fff 100644 --- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs @@ -9,7 +9,12 @@ namespace DevHive.Services.Configurations.Mapping public LanguageMappings() { CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 953b038..8825f50 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,11 +1,15 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { public IList Roles { get; set; } = new List(); - public List Friends { get; set; } = new List(); + public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); + public IList TechnologyServiceModels { get; set; } = new List(); } } diff --git a/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs index 75e7714..9d66d3e 100644 --- a/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Services.Models.Language { - public class CreateLanguageServiceModel : LanguageServiceModel + public class CreateLanguageServiceModel { public string Name { get; set; } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index fdc309e..8536693 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -1,4 +1,9 @@ +using System; + namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : CreateLanguageServiceModel { } + public class UpdateLanguageServiceModel : LanguageServiceModel + { + public string Name { get; set; } + } } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 127bde4..ccc64fd 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -18,12 +18,13 @@ namespace DevHive.Services.Services this._languageMapper = mapper; } - public async Task CreateLanguage(CreateLanguageServiceModel languageServiceModel) + public async Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel) { - if (await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name)) + if (await this._languageRepository.DoesLanguageNameExistAsync(createLanguageServiceModel.Name)) throw new ArgumentException("Language already exists!"); - Language language = this._languageMapper.Map(languageServiceModel); + //TODO: Fix, cuz it breaks + Language language = this._languageMapper.Map(createLanguageServiceModel); bool result = await this._languageRepository.AddAsync(language); return result; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 4fb18a0..c1de741 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,6 +111,9 @@ namespace DevHive.Services.Services { Tuple tuple = await ValidateUserAndLanguage(userId, languageServiceModel); + if (this._userRepository.DoesUserHaveThisLanguage(tuple.Item1, tuple.Item2)) + throw new ArgumentException("User already has this language!"); + return await this._userRepository.AddLanguageToUserAsync(tuple.Item1, tuple.Item2); } @@ -201,6 +204,9 @@ namespace DevHive.Services.Services { Tuple tuple = await ValidateUserAndLanguage(userId, languageServiceModel); + if (!this._userRepository.DoesUserHaveThisLanguage(tuple.Item1, tuple.Item2)) + throw new ArgumentException("User does not have this language!"); + return await this._userRepository.RemoveLanguageFromUserAsync(tuple.Item1, tuple.Item2); } @@ -293,44 +299,36 @@ namespace DevHive.Services.Services private async Task> ValidateUserAndLanguage(Guid userId, LanguageServiceModel languageServiceModel) { - Task userExists = this._userRepository.DoesUserExistAsync(userId); - Task languageExists = this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); + bool userExists = await this._userRepository.DoesUserExistAsync(userId); + bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); - await Task.WhenAll(userExists, languageExists); - - if (!userExists.Result) + if (!userExists) throw new ArgumentException("User does not exist!"); - if (!languageExists.Result) + if (!languageExists) throw new ArgumentException("Language does not exist!"); - Task user = this._userRepository.GetByIdAsync(userId); - Task language = this._languageRepository.GetByIdAsync(languageServiceModel.Id); - - await Task.WhenAll(user, language); + User user = await this._userRepository.GetByIdAsync(userId); + Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id); - return new Tuple(user.Result, language.Result); + return new Tuple(user, language); } private async Task> ValidateUserAndTechnology(Guid userId, TechnologyServiceModel technologyServiceModel) { - Task userExists = this._userRepository.DoesUserExistAsync(userId); - Task technologyExists = this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id); - - await Task.WhenAll(userExists, technologyExists); + bool userExists = await this._userRepository.DoesUserExistAsync(userId); + bool technologyExists = await this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id); - if (!userExists.Result) + if (!userExists) throw new ArgumentException("User does not exist!"); - if (!technologyExists.Result) + if (!technologyExists) throw new ArgumentException("Language does not exist!"); - Task user = this._userRepository.GetByIdAsync(userId); - Task technology = this._technologyRepository.GetByIdAsync(technologyServiceModel.Id); - - await Task.WhenAll(user, technology); + User user = await this._userRepository.GetByIdAsync(userId); + Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id); - return new Tuple(user.Result, technology.Result); + return new Tuple(user, technology); } #endregion } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index cce5174..46b2591 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -20,8 +20,6 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); - - System.Console.WriteLine(services.Count); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs index 67a83fe..c3f1e55 100644 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using AutoMapper; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -29,6 +30,8 @@ namespace DevHive.Web.Controllers { case ArgumentException _: case InvalidOperationException _: + case AutoMapperMappingException _: + case AutoMapperConfigurationException _: return MessageToObject(exception.Error.Message); default: return MessageToObject(null); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index c222ba6..0960915 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -94,6 +94,7 @@ namespace DevHive.Web.Controllers #endregion #region Read + [HttpGet] public async Task GetById(Guid id, [FromHeader] string authorization) { @@ -135,6 +136,7 @@ namespace DevHive.Web.Controllers #endregion #region Delete + [HttpDelete] public async Task Delete(Guid id, [FromHeader] string authorization) { diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 37d6553..260d34c 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,6 +1,8 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Web.Models.Identity.Role; +using DevHive.Web.Models.Language; +using DevHive.Web.Models.Technology; namespace DevHive.Web.Models.Identity.User { @@ -8,5 +10,7 @@ namespace DevHive.Web.Models.Identity.User { public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); + public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs index 111beed..d261500 100644 --- a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Web.Models.Language { - public class CreateLanguageWebModel : LanguageWebModel + public class CreateLanguageWebModel { public string Name { get; set; } } diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace index 8899918..04a9a2f 100644 --- a/src/DevHive.code-workspace +++ b/src/DevHive.code-workspace @@ -57,6 +57,9 @@ "env": { "ASPNETCORE_ENVIRONMENT": "Development" }, + "launchBrowser": { + "enabled": true + } }, ], "compounds": [] -- cgit v1.2.3 From 61c51944844ed404cd4f174440d6e81b2a8591ba Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 23:06:18 +0200 Subject: Fixed sln-wide code formatting --- .../Configurations/Extensions/ConfigureDatabase.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 4 +- .../Configurations/Mapping/LanguageMappings.cs | 6 +- .../Configurations/Mapping/RoleMappings.cs | 6 +- .../Configurations/Mapping/TechnologyMappings.cs | 6 +- .../Configurations/Mapping/UserMappings.cs | 2 +- src/DevHive.Web/Controllers/ErrorController.cs | 2 +- src/DevHive.Web/Controllers/LanguageController.cs | 12 +- src/DevHive.Web/Controllers/PostController.cs | 12 +- src/DevHive.Web/Controllers/RoleController.cs | 8 +- .../Controllers/TechnologyController.cs | 12 +- src/DevHive.Web/Controllers/UserController.cs | 6 +- .../Models/Identity/User/BaseUserWebModel.cs | 4 +- .../Models/Identity/User/LoginWebModel.cs | 4 +- .../Models/Identity/User/RegisterWebModel.cs | 4 +- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Validation/GoodPasswordModelValidation.cs | 2 +- .../Models/Language/UpdateLanguageWebModel.cs | 4 +- src/DevHive.Web/Startup.cs | 142 ++++++++++----------- 20 files changed, 122 insertions(+), 122 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index b42ae05..4831435 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddIdentity() .AddEntityFrameworkStores(); - + services.Configure(options => { options.User.RequireUniqueEmail = true; @@ -41,7 +41,7 @@ namespace DevHive.Web.Configurations.Extensions services.AddAuthorization(options => { - options.AddPolicy("User", options => + options.AddPolicy("User", options => { options.RequireAuthenticatedUser(); options.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme); diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index 394490e..5998e7a 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index bae8562..3c2a4d0 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Language; namespace DevHive.Web.Configurations.Mapping { - public class LanguageMappings : Profile + public class LanguageMappings : Profile { public LanguageMappings() { @@ -16,5 +16,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 5d33c56..afa3a94 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,15 +4,15 @@ using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { CreateMap(); CreateMap(); - + CreateMap(); CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 849e47f..8523897 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Technology; namespace DevHive.Web.Configurations.Mapping { - public class TechnologyMappings : Profile + public class TechnologyMappings : Profile { public TechnologyMappings() { @@ -13,5 +13,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); } - } -} \ No newline at end of file + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 4420368..59003ea 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -18,5 +18,5 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); } - } + } } diff --git a/src/DevHive.Web/Controllers/ErrorController.cs b/src/DevHive.Web/Controllers/ErrorController.cs index c3f1e55..b187501 100644 --- a/src/DevHive.Web/Controllers/ErrorController.cs +++ b/src/DevHive.Web/Controllers/ErrorController.cs @@ -19,7 +19,7 @@ namespace DevHive.Web.Controllers IExceptionHandlerFeature exception = HttpContext.Features.Get(); - + object result = ProcessException(requestId, exception); return new BadRequestObjectResult(JsonConvert.SerializeObject(result)); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 29c1e99..486e16e 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.CreateLanguage(languageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create Language"); return new OkResult(); @@ -52,21 +52,21 @@ namespace DevHive.Web.Controllers bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Language"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._languageService.DeleteLanguage(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Language"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index 753897c..a906e47 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -30,9 +30,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreatePostWebModel createPostModel) { - CreatePostServiceModel postServiceModel = - this._postMapper.Map(createPostModel); - + CreatePostServiceModel postServiceModel = + this._postMapper.Map(createPostModel); + bool result = await this._postService.CreatePost(postServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers bool result = await this._postService.AddComment(createCommentServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Comment"); return new OkResult(); @@ -81,7 +81,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) { - UpdatePostServiceModel postServiceModel = + UpdatePostServiceModel postServiceModel = this._postMapper.Map(updatePostModel); postServiceModel.IssuerId = id; @@ -129,7 +129,7 @@ namespace DevHive.Web.Controllers { if (!await this._postService.ValidateJwtForComment(id, authorization)) return new UnauthorizedResult(); - + bool result = await this._postService.DeleteComment(id); if (!result) diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d710f5a..0a8f7a1 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -26,9 +26,9 @@ namespace DevHive.Web.Controllers [HttpPost] public async Task Create([FromBody] CreateRoleModel createRoleModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); - + RoleModel roleServiceModel = + this._roleMapper.Map(createRoleModel); + bool result = await this._roleService.CreateRole(roleServiceModel); if (!result) @@ -49,7 +49,7 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) { - RoleModel roleServiceModel = + RoleModel roleServiceModel = this._roleMapper.Map(updateRoleModel); roleServiceModel.Id = id; diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index e02ca3d..905a71d 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -29,7 +29,7 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.Create(technologyServiceModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not create the Technology"); return new OkResult(); @@ -51,21 +51,21 @@ namespace DevHive.Web.Controllers bool result = await this._technologyService.UpdateTechnology(updateTechnologyWebModel); - if(!result) + if (!result) return new BadRequestObjectResult("Could not update Technology"); return new OkResult(); } - + [HttpDelete] public async Task Delete(Guid id) { bool result = await this._technologyService.DeleteTechnology(id); - - if(!result) + + if (!result) return new BadRequestObjectResult("Could not delete Technology"); return new OkResult(); } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 0960915..26271b2 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -20,7 +20,7 @@ namespace DevHive.Web.Controllers [ApiController] [Route("/api/[controller]")] [Authorize(Roles = "User")] - public class UserController: ControllerBase + public class UserController : ControllerBase { private readonly UserService _userService; private readonly IMapper _userMapper; @@ -154,7 +154,7 @@ namespace DevHive.Web.Controllers await this._userService.RemoveFriend(userId, friendId); return new OkResult(); } - + [HttpDelete] [Route("RemoveLanguageFromUser")] public async Task RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) @@ -176,7 +176,7 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestResult(); } - + #endregion } } diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs index ff9fac5..2d99786 100644 --- a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class BaseUserWebModel + public class BaseUserWebModel { [Required] [MinLength(3)] diff --git a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs index 3bd7428..87c7416 100644 --- a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs @@ -1,6 +1,6 @@ -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class LoginWebModel + public class LoginWebModel { public string UserName { get; set; } public string Password { get; set; } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 22b178b..273c2d3 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { - public class RegisterWebModel : BaseUserWebModel + public class RegisterWebModel : BaseUserWebModel { [Required] [GoodPassword] diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index fbe02a5..91fbc64 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 260d34c..8f7995c 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -4,7 +4,7 @@ using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; -namespace DevHive.Web.Models.Identity.User +namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs index f69121a..f920c35 100644 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs @@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations; namespace DevHive.Web.Models.Identity.Validation { - public class GoodPassword : ValidationAttribute + public class GoodPassword : ValidationAttribute { public override bool IsValid(object value) { diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 2da8217..deca0fc 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,5 +2,5 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : CreateLanguageWebModel {} -} \ No newline at end of file + public class UpdateLanguageWebModel : CreateLanguageWebModel { } +} diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 96ab318..94aabe8 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,71 +1,71 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using DevHive.Web.Configurations.Extensions; -using Newtonsoft.Json; - -namespace DevHive.Web -{ - public class Startup - { - public Startup(IConfiguration configuration) - { - Configuration = configuration; - } - - public IConfiguration Configuration { get; } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddCors(); - - services.AddControllers() - .AddNewtonsoftJson(x => - { - x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; - }); - - services.DatabaseConfiguration(Configuration); - services.SwaggerConfiguration(); - services.JWTConfiguration(Configuration); - services.AutoMapperConfiguration(); - services.DependencyInjectionConfiguration(); - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.UseCors(x => x - .AllowAnyMethod() - .AllowAnyHeader() - .SetIsOriginAllowed(origin => true) // allow any origin - .AllowCredentials()); // allow credentials - - if (env.IsDevelopment()) - { - //app.UseDeveloperExceptionPage(); - app.UseExceptionHandler("/api/Error"); - app.UseSwaggerConfiguration(); - } - else - { - app.UseExceptionHandler("/api/Error"); - app.UseHsts(); - } - - app.UseDatabaseConfiguration(); - app.UseAutoMapperConfiguration(); - - app.UseEndpoints(endpoints => - { - endpoints.MapControllerRoute( - name: "default", - pattern: "api/{controller}/{action}" - ); - }); - } - } -} +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using DevHive.Web.Configurations.Extensions; +using Newtonsoft.Json; + +namespace DevHive.Web +{ + public class Startup + { + public Startup(IConfiguration configuration) + { + Configuration = configuration; + } + + public IConfiguration Configuration { get; } + + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) + { + services.AddCors(); + + services.AddControllers() + .AddNewtonsoftJson(x => + { + x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; + }); + + services.DatabaseConfiguration(Configuration); + services.SwaggerConfiguration(); + services.JWTConfiguration(Configuration); + services.AutoMapperConfiguration(); + services.DependencyInjectionConfiguration(); + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .SetIsOriginAllowed(origin => true) // allow any origin + .AllowCredentials()); // allow credentials + + if (env.IsDevelopment()) + { + //app.UseDeveloperExceptionPage(); + app.UseExceptionHandler("/api/Error"); + app.UseSwaggerConfiguration(); + } + else + { + app.UseExceptionHandler("/api/Error"); + app.UseHsts(); + } + + app.UseDatabaseConfiguration(); + app.UseAutoMapperConfiguration(); + + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute( + name: "default", + pattern: "api/{controller}/{action}" + ); + }); + } + } +} -- cgit v1.2.3 From c43a7665b9febe890eb6dac143b62c36464cc94d Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 12:24:17 +0200 Subject: Moved Lang&Tech CRUD to Update in User Layer --- .../Interfaces/ILanguageService.cs | 2 +- .../Interfaces/ITechnologyService.cs | 2 +- src/DevHive.Services/Interfaces/IUserService.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 5 +- src/DevHive.Services/Services/LanguageService.cs | 5 +- src/DevHive.Services/Services/TechnologyService.cs | 5 +- src/DevHive.Services/Services/UserService.cs | 8 +-- src/DevHive.Web/Controllers/LanguageController.cs | 3 +- .../Controllers/TechnologyController.cs | 5 +- src/DevHive.Web/Controllers/UserController.cs | 70 +--------------------- .../Models/Identity/User/FriendWebModel.cs | 7 +++ .../Models/Identity/User/UpdateUserWebModel.cs | 9 +++ .../Models/Identity/User/UserWebModel.cs | 4 +- .../Models/Language/UpdateLanguageWebModel.cs | 2 +- .../Models/Technology/UpdateTechnologyWebModel.cs | 2 +- 15 files changed, 44 insertions(+), 87 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/User/FriendWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index eb45a8d..08b3812 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetLanguageById(Guid languageId); - Task UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel); + Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); Task DeleteLanguage(Guid languageId); } diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs index 0797078..9e1e955 100644 --- a/src/DevHive.Services/Interfaces/ITechnologyService.cs +++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs @@ -10,7 +10,7 @@ namespace DevHive.Services.Interfaces Task GetTechnologyById(Guid id); - Task UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel); + Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel); Task DeleteTechnology(Guid id); } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 5ef141f..0f834e9 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -16,7 +16,7 @@ namespace DevHive.Services.Interfaces Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel); Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel); - Task GetFriendById(Guid friendId); + Task GetFriend(string username); Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 8825f50..ecf8c42 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -8,8 +8,11 @@ namespace DevHive.Services.Models.Identity.User public class UserServiceModel : BaseUserServiceModel { public IList Roles { get; set; } = new List(); + public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); - public IList TechnologyServiceModels { get; set; } = new List(); + + public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index be035c2..5b697cd 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -48,9 +48,9 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel) + public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { - bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageId); + bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); bool newLangNameExists = await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name); if (!langExists) @@ -59,7 +59,6 @@ namespace DevHive.Services.Services if (newLangNameExists) throw new ArgumentException("This name is already in our datbase!"); - languageServiceModel.Id = languageId; Language lang = this._languageMapper.Map(languageServiceModel); return await this._languageRepository.EditAsync(lang); } diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index d8b7262..c088281 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -48,15 +48,14 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel) + public async Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel) { - if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId)) + if (!await this._technologyRepository.DoesTechnologyExistAsync(updateTechnologyServiceModel.Id)) throw new ArgumentException("Technology does not exist!"); if (await this._technologyRepository.DoesTechnologyNameExistAsync(updateTechnologyServiceModel.Name)) throw new ArgumentException("Technology name already exists!"); - updateTechnologyServiceModel.Id = technologyId; Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); bool result = await this._technologyRepository.EditAsync(technology); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 37dbc7b..d9e87e0 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -160,12 +160,12 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); } - public async Task GetFriendById(Guid friendId) + public async Task GetFriend(string username) { - if (!await _userRepository.DoesUserExistAsync(friendId)) - throw new ArgumentException("User does not exist!"); + User friend = await this._userRepository.GetByUsernameAsync(username); - User friend = await this._userRepository.GetByIdAsync(friendId); + if (default(User) == friend) + throw new ArgumentException("User does not exist!"); return this._userMapper.Map(friend); } diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index 5202f16..c503a47 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -47,8 +47,9 @@ namespace DevHive.Web.Controllers public async Task Update(Guid languageId, [FromBody] UpdateLanguageWebModel updateModel) { UpdateLanguageServiceModel updatelanguageServiceModel = this._languageMapper.Map(updateModel); + updatelanguageServiceModel.Id = languageId; - bool result = await this._languageService.UpdateLanguage(languageId, updatelanguageServiceModel); + bool result = await this._languageService.UpdateLanguage(updatelanguageServiceModel); if (!result) return new BadRequestObjectResult("Could not update Language"); diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index 3be3b8a..7f2f400 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -46,9 +46,10 @@ namespace DevHive.Web.Controllers [HttpPut] public async Task Update(Guid technologyId, [FromBody] UpdateTechnologyWebModel updateModel) { - UpdateTechnologyServiceModel updateTechnologyWebModel = this._technologyMapper.Map(updateModel); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._technologyMapper.Map(updateModel); + updateTechnologyServiceModel.Id = technologyId; - bool result = await this._technologyService.UpdateTechnology(technologyId, updateTechnologyWebModel); + bool result = await this._technologyService.UpdateTechnology(updateTechnologyServiceModel); if (!result) return new BadRequestObjectResult("Could not update Technology"); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index b33c3b9..8d48705 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -57,40 +57,6 @@ namespace DevHive.Web.Controllers } #endregion - #region Create - - [HttpPost] - [Route("AddAFriend")] - public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) - { - return await this._userService.AddFriend(userId, friendIdModel.Id) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddLanguageToUser")] - public async Task AddLanguageToUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map(languageWebModel); - - return await this._userService.AddLanguageToUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpPost] - [Route("AddTechnologyToUser")] - public async Task AddTechnologyToUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map(technologyWebModel); - - return await this._userService.AddTechnologyToUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - #endregion - #region Read [HttpGet] @@ -107,9 +73,10 @@ namespace DevHive.Web.Controllers [HttpGet] [Route("GetAFriend")] - public async Task GetAFriend(Guid friendId) + [AllowAnonymous] + public async Task GetAFriend(string username) { - UserServiceModel friendServiceModel = await this._userService.GetFriendById(friendId); + UserServiceModel friendServiceModel = await this._userService.GetFriend(username); UserWebModel friend = this._userMapper.Map(friendServiceModel); return new OkObjectResult(friend); @@ -134,7 +101,6 @@ namespace DevHive.Web.Controllers #endregion #region Delete - [HttpDelete] public async Task Delete(Guid id, [FromHeader] string authorization) { @@ -144,36 +110,6 @@ namespace DevHive.Web.Controllers await this._userService.DeleteUser(id); return new OkResult(); } - - [HttpDelete] - [Route("RemoveAFriend")] - public async Task RemoveAFriend(Guid userId, Guid friendId) - { - await this._userService.RemoveFriend(userId, friendId); - return new OkResult(); - } - - [HttpDelete] - [Route("RemoveLanguageFromUser")] - public async Task RemoveLanguageFromUser(Guid userId, [FromBody] LanguageWebModel languageWebModel) - { - LanguageServiceModel languageServiceModel = this._userMapper.Map(languageWebModel); - - return await this._userService.RemoveLanguageFromUser(userId, languageServiceModel) ? - new OkResult() : - new BadRequestResult(); - } - - [HttpDelete] - [Route("RemoveTechnologyFromUser")] - public async Task RemoveTechnologyFromUser(Guid userId, [FromBody] TechnologyWebModel technologyWebModel) - { - TechnologyServiceModel technologyServiceModel = this._userMapper.Map(technologyWebModel); - - return await this._userService.RemoveTechnologyFromUser(userId, technologyServiceModel) ? - new OkResult() : - new BadRequestResult(); - } #endregion } } diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs new file mode 100644 index 0000000..681529a --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class FriendWebModel + { + public string Username { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 9e41eb6..782c558 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,5 +1,8 @@ +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using DevHive.Web.Models.Identity.Validation; +using DevHive.Web.Models.Language; +using DevHive.Web.Models.Technology; namespace DevHive.Web.Models.Identity.User { @@ -8,5 +11,11 @@ namespace DevHive.Web.Models.Identity.User [Required] [GoodPassword] public string Password { get; set; } + + public IList Friends { get; set; } + + public IList Languages { get; set; } + + public IList Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 8f7995c..3b243e7 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -9,8 +8,11 @@ namespace DevHive.Web.Models.Identity.User public class UserWebModel : BaseUserWebModel { public IList Roles { get; set; } = new List(); + public IList Friends { get; set; } = new List(); + public IList Languages { get; set; } = new List(); + public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index ed3b37c..18d945f 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : LanguageWebModel + public class UpdateLanguageWebModel { public string Name { get; set; } } diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs index 8bf48bf..4651b9e 100644 --- a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -2,7 +2,7 @@ using System; namespace DevHive.Web.Models.Technology { - public class UpdateTechnologyWebModel : TechnologyWebModel + public class UpdateTechnologyWebModel { public string Name { get; set; } } -- cgit v1.2.3 From 8560caa13b7f7d0e80ee712c77efc59a80e8935c Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 21:46:06 +0200 Subject: Added attributes to the Web models for validations --- .../Attributes/GoodPasswordModelValidation.cs | 24 +++++++++++ .../Attributes/OnlyAlphanumericsModelValidation.cs | 20 +++++++++ .../Attributes/OnlyLettersModelValidation.cs | 20 +++++++++ .../Extensions/ConfigureCustomMiddleware.cs | 2 +- src/DevHive.Web/Middleware/ExceptionMiddleware.cs | 50 ++++++++++++++++++++++ .../Models/Identity/Role/CreateRoleWebModel.cs | 7 +++ .../Models/Identity/Role/RoleWebModel.cs | 7 +++ .../Models/Identity/Role/UpdateRoleWebModel.cs | 4 ++ .../Models/Identity/User/BaseUserWebModel.cs | 7 ++- .../Models/Identity/User/FriendWebModel.cs | 11 ++++- .../Models/Identity/User/LoginWebModel.cs | 13 ++++++ .../Models/Identity/User/RegisterWebModel.cs | 4 +- .../Models/Identity/User/UpdateUserWebModel.cs | 10 ++++- .../Models/Identity/User/UserWebModel.cs | 10 +++++ .../Validation/GoodPasswordModelValidation.cs | 24 ----------- .../Validation/OnlyAlphanumericsModelValidation.cs | 20 --------- .../Validation/OnlyLettersModelValidation.cs | 20 --------- .../Models/Language/CreateLanguageWebModel.cs | 9 +++- .../Models/Language/LanguageWebModel.cs | 6 ++- .../Models/Language/ReadLanguageWebModel.cs | 7 +++ .../Models/Language/UpdateLanguageWebModel.cs | 7 ++- .../Models/Middleware/ExceptionMiddleware.cs | 50 ---------------------- .../Models/Post/Comment/CommentWebModel.cs | 13 ++++++ .../Models/Post/Post/BasePostWebModel.cs | 9 +++- .../Models/Post/Post/CreatePostWebModel.cs | 6 +-- src/DevHive.Web/Models/Post/Post/PostWebModel.cs | 20 ++++++++- .../Models/Technology/CreateTechnologyWebModel.cs | 7 +++ .../Models/Technology/TechnologyWebModel.cs | 6 ++- .../Models/Technology/UpdateTechnologyWebModel.cs | 6 +++ 29 files changed, 269 insertions(+), 130 deletions(-) create mode 100644 src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs create mode 100644 src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs create mode 100644 src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs create mode 100644 src/DevHive.Web/Middleware/ExceptionMiddleware.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs delete mode 100644 src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs new file mode 100644 index 0000000..7d6a1ea --- /dev/null +++ b/src/DevHive.Web/Attributes/GoodPasswordModelValidation.cs @@ -0,0 +1,24 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class GoodPassword : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + for (int i = 0; i < stringValue.Length; i++) + { + if (Char.IsDigit(stringValue[i])) + { + base.ErrorMessage = "Password must be atleast 5 characters long!"; + return stringValue.Length >= 5; + } + } + base.ErrorMessage = "Password must contain atleast 1 digit!"; + return false; + } + } +} diff --git a/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs new file mode 100644 index 0000000..26e0733 --- /dev/null +++ b/src/DevHive.Web/Attributes/OnlyAlphanumericsModelValidation.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class OnlyAlphanumerics : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + foreach (char ch in stringValue) + { + if (!Char.IsLetterOrDigit(ch)) + return false; + } + return true; + } + } +} diff --git a/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs new file mode 100644 index 0000000..07afee9 --- /dev/null +++ b/src/DevHive.Web/Attributes/OnlyLettersModelValidation.cs @@ -0,0 +1,20 @@ +using System; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Attributes +{ + public class OnlyLetters : ValidationAttribute + { + public override bool IsValid(object value) + { + var stringValue = (string)value; + + foreach (char ch in stringValue) + { + if (!Char.IsLetter(ch)) + return false; + } + return true; + } + } +} diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs index 24c2adf..efcb8e1 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs @@ -1,4 +1,4 @@ -using DevHive.Web.Models.Middleware; +using DevHive.Web.Middleware; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; diff --git a/src/DevHive.Web/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs new file mode 100644 index 0000000..cb6d4ca --- /dev/null +++ b/src/DevHive.Web/Middleware/ExceptionMiddleware.cs @@ -0,0 +1,50 @@ +using System; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; + +namespace DevHive.Web.Middleware +{ + public class ExceptionMiddleware + { + private readonly RequestDelegate _next; + // private readonly ILogger _logger; + + public ExceptionMiddleware(RequestDelegate next) + { + this._next = next; + // this._logger = logger; + } + // public ExceptionMiddleware(RequestDelegate next, ILogger logger) + // { + // this._logger = logger; + // this._next = next; + // } + + public async Task InvokeAsync(HttpContext httpContext) + { + try + { + await this._next(httpContext); + } + catch (Exception ex) + { + // this._logger.LogError($"Something went wrong: {ex}"); + await HandleExceptionAsync(httpContext, ex); + } + } + + private Task HandleExceptionAsync(HttpContext context, Exception exception) + { + context.Response.ContentType = "application/json"; + context.Response.StatusCode = (int)HttpStatusCode.BadRequest; + + return context.Response.WriteAsync(new + { + StatusCode = context.Response.StatusCode, + Message = exception.Message + }.ToString()); + } + } +} diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index e872428..859cdd9 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Identity.Role { public class CreateRoleWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs index 41ade23..99b0f50 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Identity.Role { public class RoleWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index aaf0270..254affc 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Identity.Role { public class UpdateRoleWebModel : RoleWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs index 2d99786..d7d8d29 100644 --- a/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/BaseUserWebModel.cs @@ -1,26 +1,31 @@ using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; namespace DevHive.Web.Models.Identity.User { public class BaseUserWebModel { + [NotNull] [Required] [MinLength(3)] [MaxLength(50)] [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string UserName { get; set; } + [NotNull] [Required] [EmailAddress] public string Email { get; set; } + [NotNull] [Required] [MinLength(3)] [MaxLength(30)] [OnlyLetters(ErrorMessage = "First name can only contain letters!")] public string FirstName { get; set; } + [NotNull] [Required] [MinLength(3)] [MaxLength(30)] diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs index 681529a..d59bff5 100644 --- a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs @@ -1,7 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + namespace DevHive.Web.Models.Identity.User { public class FriendWebModel { - public string Username { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs index 87c7416..0395274 100644 --- a/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/LoginWebModel.cs @@ -1,8 +1,21 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + namespace DevHive.Web.Models.Identity.User { public class LoginWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string UserName { get; set; } + + [NotNull] + [Required] + [GoodPassword] public string Password { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs index 04fd6bd..0fc7ec6 100644 --- a/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/RegisterWebModel.cs @@ -1,10 +1,12 @@ using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; namespace DevHive.Web.Models.Identity.User { public class RegisterWebModel : BaseUserWebModel { + [NotNull] [Required] [GoodPassword] public string Password { get; set; } diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 782c558..724930c 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using DevHive.Web.Models.Identity.Validation; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -8,14 +9,21 @@ namespace DevHive.Web.Models.Identity.User { public class UpdateUserWebModel : BaseUserWebModel { + [NotNull] [Required] [GoodPassword] public string Password { get; set; } + [NotNull] + [Required] public IList Friends { get; set; } + [NotNull] + [Required] public IList Languages { get; set; } + [NotNull] + [Required] public IList Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 3b243e7..88f199d 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,4 +1,6 @@ using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -7,12 +9,20 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { + [NotNull] + [Required] public IList Roles { get; set; } = new List(); + [NotNull] + [Required] public IList Friends { get; set; } = new List(); + [NotNull] + [Required] public IList Languages { get; set; } = new List(); + [NotNull] + [Required] public IList Technologies { get; set; } = new List(); } } diff --git a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs deleted file mode 100644 index f920c35..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/GoodPasswordModelValidation.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class GoodPassword : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - for (int i = 0; i < stringValue.Length; i++) - { - if (Char.IsDigit(stringValue[i])) - { - base.ErrorMessage = "Password must be atleast 5 characters long!"; - return stringValue.Length >= 5; - } - } - base.ErrorMessage = "Password must contain atleast 1 digit!"; - return false; - } - } -} diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs deleted file mode 100644 index 5c8c66c..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyAlphanumericsModelValidation.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class OnlyAlphanumerics : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - foreach (char ch in stringValue) - { - if (!Char.IsLetterOrDigit(ch)) - return false; - } - return true; - } - } -} diff --git a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs b/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs deleted file mode 100644 index 29a995a..0000000 --- a/src/DevHive.Web/Models/Identity/Validation/OnlyLettersModelValidation.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.ComponentModel.DataAnnotations; - -namespace DevHive.Web.Models.Identity.Validation -{ - public class OnlyLetters : ValidationAttribute - { - public override bool IsValid(object value) - { - var stringValue = (string)value; - - foreach (char ch in stringValue) - { - if (!Char.IsLetter(ch)) - return false; - } - return true; - } - } -} diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs index d261500..a739e7f 100644 --- a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -1,9 +1,14 @@ -using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class CreateLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Language/LanguageWebModel.cs b/src/DevHive.Web/Models/Language/LanguageWebModel.cs index 455b559..7515e70 100644 --- a/src/DevHive.Web/Models/Language/LanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/LanguageWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class LanguageWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs index f1e0ecc..ab4a089 100644 --- a/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/ReadLanguageWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Language { public class ReadLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 18d945f..128d534 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -1,9 +1,14 @@ -using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Language { public class UpdateLanguageWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs b/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs deleted file mode 100644 index c57452e..0000000 --- a/src/DevHive.Web/Models/Middleware/ExceptionMiddleware.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Net; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Logging; - -namespace DevHive.Web.Models.Middleware -{ - public class ExceptionMiddleware - { - private readonly RequestDelegate _next; - // private readonly ILogger _logger; - - public ExceptionMiddleware(RequestDelegate next) - { - this._next = next; - // this._logger = logger; - } - // public ExceptionMiddleware(RequestDelegate next, ILogger logger) - // { - // this._logger = logger; - // this._next = next; - // } - - public async Task InvokeAsync(HttpContext httpContext) - { - try - { - await this._next(httpContext); - } - catch (Exception ex) - { - // this._logger.LogError($"Something went wrong: {ex}"); - await HandleExceptionAsync(httpContext, ex); - } - } - - private Task HandleExceptionAsync(HttpContext context, Exception exception) - { - context.Response.ContentType = "application/json"; - context.Response.StatusCode = (int)HttpStatusCode.BadRequest; - - return context.Response.WriteAsync(new - { - StatusCode = context.Response.StatusCode, - Message = exception.Message - }.ToString()); - } - } -} diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs index d66e5c9..590851d 100644 --- a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -1,12 +1,25 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Post.Comment { public class CommentWebModel { + [NotNull] + [Required] public Guid IssuerId { get; set; } + + [NotNull] + [Required] public Guid PostId { get; set; } + + [NotNull] + [Required] public string Message { get; set; } + + [NotNull] + [Required] public DateTime TimeCreated { get; set; } } } diff --git a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs index caa9925..35ddd34 100644 --- a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs @@ -1,10 +1,17 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Post.Post { public class BasePostWebModel { + [NotNull] + [Required] public Guid IssuerId { get; set; } + + [NotNull] + [Required] public string Message { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs index 7558b2c..389ff9e 100644 --- a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -2,7 +2,5 @@ using System; namespace DevHive.Web.Models.Post.Post { - public class CreatePostWebModel : BasePostWebModel - { - } -} \ No newline at end of file + public class CreatePostWebModel : BasePostWebModel { } +} diff --git a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs index fa18c3a..fe35cee 100644 --- a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs +++ b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs @@ -1,3 +1,6 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; using DevHive.Web.Models.Post.Comment; namespace DevHive.Web.Models.Post.Post @@ -6,16 +9,31 @@ namespace DevHive.Web.Models.Post.Post { //public string Picture { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string IssuerFirstName { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string IssuerLastName { get; set; } + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] public string IssuerUsername { get; set; } + [NotNull] + [Required] public string Message { get; set; } //public Files[] Files { get; set; } public CommentWebModel[] Comments { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs index 13029ee..ec9ec15 100644 --- a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs @@ -1,7 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + namespace DevHive.Web.Models.Technology { public class CreateTechnologyWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs index 05f7af8..6e8273b 100644 --- a/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/TechnologyWebModel.cs @@ -1,9 +1,13 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Technology { public class TechnologyWebModel { + [NotNull] + [Required] public Guid Id { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs index 4651b9e..3e9fe2a 100644 --- a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -1,9 +1,15 @@ using System; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Technology { public class UpdateTechnologyWebModel { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] public string Name { get; set; } } } -- cgit v1.2.3 From aa4f7bdd9a2df09fc47e82c2b85fb7647203ba8d Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 23:01:33 +0200 Subject: Config ExceptionMiddleware; Config Mapper; Fixed User editing; Implmeneted Friend add trough HttpPatch --- src/DevHive.Data/Models/User.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 12 +++++----- .../Configurations/Mapping/UserMappings.cs | 2 ++ .../Models/Identity/User/FriendServiceModel.cs | 7 ++++++ .../Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Services/Services/UserService.cs | 26 +++++++++++++++++++--- .../Extensions/ConfigureCustomMiddleware.cs | 16 ------------- .../ConfigureExceptionHandlerMiddleware.cs | 16 +++++++++++++ .../Configurations/Mapping/UserMappings.cs | 3 +++ .../Models/Identity/User/UserWebModel.cs | 2 +- src/DevHive.Web/Startup.cs | 4 ++-- 11 files changed, 63 insertions(+), 28 deletions(-) create mode 100644 src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs delete mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs create mode 100644 src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 31e36ac..cf779f5 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -18,6 +18,7 @@ namespace DevHive.Data.Models /// /// Languages that the user uses or is familiar with /// + // [Unique] public IList Languages { get; set; } /// diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 81c974c..2ca8099 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -109,12 +109,14 @@ namespace DevHive.Data.Repositories public async Task EditAsync(User newEntity) { - User user = await this.GetByIdAsync(newEntity.Id); + // User user = await this.GetByIdAsync(newEntity.Id); - this._context - .Entry(user) - .CurrentValues - .SetValues(newEntity); + // this._context + // .Entry(user) + // .CurrentValues + // .SetValues(newEntity); + + this._context.Update(newEntity); return await this.SaveChangesAsync(this._context); } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index d57c6ba..541e16e 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs new file mode 100644 index 0000000..63d57f7 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class FriendServiceModel + { + public string UserName { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index aa77a0c..913b5c0 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -9,7 +9,7 @@ namespace DevHive.Services.Models.Identity.User { public IList Roles { get; set; } = new List(); - public IList Friends { get; set; } = new List(); + public IList Friends { get; set; } = new List(); public IList Languages { get; set; } = new List(); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index a8b9ef9..ee4b24d 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,6 +15,7 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using Microsoft.AspNetCore.JsonPatch; using System.Linq; +using Newtonsoft.Json; namespace DevHive.Services.Services { @@ -171,18 +172,37 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - var password = jsonPatch.Operations + object password = jsonPatch.Operations .Where(x => x.path == "/password") .Select(x => x.value) .FirstOrDefault(); + IEnumerable friends = jsonPatch.Operations + .Where(x => x.path == "/friends") + .Select(x => x.value); + if(password != null) { string passwordHash = this.GeneratePasswordHash(password.ToString()); user.PasswordHash = passwordHash; } - else - jsonPatch.ApplyTo(user); + + if (friends != null) + { + foreach (object friendObj in friends) + { + FriendServiceModel friendServiceModel = + JsonConvert.DeserializeObject(friendObj.ToString()); + + User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName) + ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!"); + + user.Friends.Add(amigo); + } + } + + //Remove password and friends peace from the request patch before applying the rest + // jsonPatch.ApplyTo(user); bool success = await this._userRepository.EditAsync(user); if (success) diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs deleted file mode 100644 index efcb8e1..0000000 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureCustomMiddleware.cs +++ /dev/null @@ -1,16 +0,0 @@ -using DevHive.Web.Middleware; -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; - -namespace DevHive.Web.Configurations.Extensions -{ - public static class ConfigureCustomMiddleware - { - public static void CustomMiddlewareConfiguration(this IServiceCollection services) { } - - public static void UseCustomMiddlewareConfiguration(this IApplicationBuilder app) - { - app.UseMiddleware(); - } - } -} diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs new file mode 100644 index 0000000..286727f --- /dev/null +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureExceptionHandlerMiddleware.cs @@ -0,0 +1,16 @@ +using DevHive.Web.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; + +namespace DevHive.Web.Configurations.Extensions +{ + public static class ConfigureExceptionHandlerMiddleware + { + public static void ExceptionHandlerMiddlewareConfiguration(this IServiceCollection services) { } + + public static void UseExceptionHandlerMiddlewareConfiguration(this IApplicationBuilder app) + { + app.UseMiddleware(); + } + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index beb9607..5faf4b5 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,6 +20,9 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); + CreateMap(); + CreateMap(); + CreateMap() .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); CreateMap(); diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 88f199d..1d2d17b 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -15,7 +15,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public IList Friends { get; set; } = new List(); + public IList Friends { get; set; } = new List(); [NotNull] [Required] diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 8fa346a..92d4359 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -33,7 +33,7 @@ namespace DevHive.Web services.JWTConfiguration(Configuration); services.AutoMapperConfiguration(); services.DependencyInjectionConfiguration(); - services.CustomMiddlewareConfiguration(); + services.ExceptionHandlerMiddlewareConfiguration(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -53,11 +53,11 @@ namespace DevHive.Web else { app.UseHsts(); + app.UseExceptionHandlerMiddlewareConfiguration(); } app.UseDatabaseConfiguration(); app.UseAutoMapperConfiguration(); - app.UseCustomMiddlewareConfiguration(); app.UseEndpoints(endpoints => { -- cgit v1.2.3 From 8179af787a7bf375753a178b89111a91d84a8bb8 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 20 Jan 2021 18:07:58 +0200 Subject: Changed List to HashSet for "no duplicates" scenario --- src/DevHive.Data/Interfaces/Models/ILanguage.cs | 2 +- src/DevHive.Data/Interfaces/Models/IRole.cs | 2 +- src/DevHive.Data/Interfaces/Models/ITechnology.cs | 2 +- src/DevHive.Data/Interfaces/Models/IUser.cs | 8 ++++---- .../Interfaces/Repositories/IUserRepository.cs | 4 ++-- src/DevHive.Data/Models/Language.cs | 2 +- src/DevHive.Data/Models/Role.cs | 2 +- src/DevHive.Data/Models/Technology.cs | 2 +- src/DevHive.Data/Models/User.cs | 8 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 4 ++-- .../Models/Identity/User/RegisterServiceModel.cs | 4 ++-- .../Models/Identity/User/UpdateUserServiceModel.cs | 8 ++++---- .../Models/Identity/User/UserServiceModel.cs | 8 ++++---- src/DevHive.Services/Services/PostService.cs | 5 +++-- src/DevHive.Services/Services/UserService.cs | 12 ++++++------ .../DevHive.Data.Tests/UserRepositoryTests.cs | 18 +++++++++--------- .../Models/Identity/User/UpdateUserWebModel.cs | 6 +++--- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 8 ++++---- 18 files changed, 53 insertions(+), 52 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Data/Interfaces/Models/ILanguage.cs b/src/DevHive.Data/Interfaces/Models/ILanguage.cs index 9549777..b77d5ae 100644 --- a/src/DevHive.Data/Interfaces/Models/ILanguage.cs +++ b/src/DevHive.Data/Interfaces/Models/ILanguage.cs @@ -6,7 +6,7 @@ namespace DevHive.Data.Interfaces.Models public interface ILanguage : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IRole.cs b/src/DevHive.Data/Interfaces/Models/IRole.cs index 0623f07..c8b7068 100644 --- a/src/DevHive.Data/Interfaces/Models/IRole.cs +++ b/src/DevHive.Data/Interfaces/Models/IRole.cs @@ -5,6 +5,6 @@ namespace DevHive.Data.Interfaces.Models { public interface IRole { - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/ITechnology.cs b/src/DevHive.Data/Interfaces/Models/ITechnology.cs index 159a21a..153f75f 100644 --- a/src/DevHive.Data/Interfaces/Models/ITechnology.cs +++ b/src/DevHive.Data/Interfaces/Models/ITechnology.cs @@ -6,6 +6,6 @@ namespace DevHive.Data.Interfaces.Models public interface ITechnology : IModel { string Name { get; set; } - List Users { get; set; } + HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index ef8c927..08ce385 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,9 +8,9 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Languages { get; set; } - IList Technologies { get; set; } - IList Roles { get; set; } - IList Friends { get; set; } + HashSet Languages { get; set; } + HashSet Technologies { get; set; } + HashSet Roles { get; set; } + HashSet Friends { get; set; } } } diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index eca6adb..456eb94 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -14,8 +14,8 @@ namespace DevHive.Data.Interfaces.Repositories Task GetByUsernameAsync(string username); Language GetUserLanguage(User user, Language language); - IList GetUserLanguages(User user); - IList GetUserTechnologies(User user); + HashSet GetUserLanguages(User user); + HashSet GetUserTechnologies(User user); Technology GetUserTechnology(User user, Technology technology); IEnumerable QueryAll(); diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index 0db8cb6..f2b2786 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index e63f007..e0855aa 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -12,6 +12,6 @@ namespace DevHive.Data.Models public const string DefaultRole = "User"; public const string AdminRole = "Admin"; - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 9096cbe..a0728d5 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -8,6 +8,6 @@ namespace DevHive.Data.Models { public Guid Id { get; set; } public string Name { get; set; } - public List Users { get; set; } + public HashSet Users { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index cf779f5..2ac7adf 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -19,15 +19,15 @@ namespace DevHive.Data.Models /// Languages that the user uses or is familiar with /// // [Unique] - public IList Languages { get; set; } + public HashSet Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 2ca8099..492d46b 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -82,7 +82,7 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.UserName == username); } - public IList GetUserLanguages(User user) + public HashSet GetUserLanguages(User user) { return user.Languages; } @@ -93,7 +93,7 @@ namespace DevHive.Data.Repositories .FirstOrDefault(x => x.Id == language.Id); } - public IList GetUserTechnologies(User user) + public HashSet GetUserTechnologies(User user) { return user.Technologies; } diff --git a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 74f66b4..3171ea6 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -6,8 +6,8 @@ namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel { - public IList Languages { get; set; } - public IList Technologies { get; set; } + public HashSet Languages { get; set; } + public HashSet Technologies { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 5b5a178..87af43a 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -7,13 +7,13 @@ namespace DevHive.Services.Models.Identity.User { public Guid Id { get; set; } - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 913b5c0..5fcd494 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index f2f60d1..9503b8a 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -9,6 +9,7 @@ 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 { @@ -131,8 +132,8 @@ namespace DevHive.Services.Services { var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; - //List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); + string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First(); + //HashSet jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByUsernameAsync(jwtUserName) ?? throw new ArgumentException("User does not exist!"); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index ee4b24d..51c4432 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -75,7 +75,7 @@ namespace DevHive.Services.Services // Set the default role to the user Role defaultRole = await this._roleRepository.GetByNameAsync(Role.DefaultRole); - user.Roles = new List() { defaultRole }; + user.Roles = new HashSet() { defaultRole }; await this._userRepository.AddAsync(user); @@ -144,12 +144,12 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - List languages = new(); + HashSet languages = new(); foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? throw new ArgumentException("Invalid language name!")); - List technologies = new(); + HashSet technologies = new(); foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? throw new ArgumentException("Invalid technology name!")); @@ -257,7 +257,7 @@ namespace DevHive.Services.Services // There is authorization name in the beginning, i.e. "Bearer eyJh..." var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7)); - Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims)[0]); + Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims).First()); List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); User user = await this._userRepository.GetByIdAsync(jwtUserID) @@ -326,11 +326,11 @@ namespace DevHive.Services.Services } } - private string WriteJWTSecurityToken(Guid userId, IList roles) + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); - List claims = new() + HashSet claims = new() { new Claim("ID", $"{userId}"), }; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index b0a5b93..be116b0 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -165,10 +165,10 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Languages; + HashSet dummyUserLanguages = dummyUser.Languages; //Act - IList languages = this._userRepository.GetUserLanguages(dummyUser); + HashSet languages = this._userRepository.GetUserLanguages(dummyUser); //Assert Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -185,7 +185,7 @@ namespace DevHive.Data.Tests // Language dummyLang = await this._languageRepository.GetByNameAsync("csharp"); // //Act - // IList languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); + // HashSet languages = this._userRepository.GetUserLanguage(dummyUser, dummyLang); // //Assert // Assert.AreEqual(dummyUserLanguages, languages, "Method doesn't query languages properly"); @@ -195,7 +195,7 @@ namespace DevHive.Data.Tests #region HelperMethods private User CreateDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -204,7 +204,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -213,7 +213,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { @@ -237,7 +237,7 @@ namespace DevHive.Data.Tests private User CreateAnotherDummyUser() { - List languages = new() + HashSet languages = new() { new Language() { @@ -246,7 +246,7 @@ namespace DevHive.Data.Tests }, }; - List technologies = new() + HashSet technologies = new() { new Technology() { @@ -255,7 +255,7 @@ namespace DevHive.Data.Tests }, }; - List roles = new() + HashSet roles = new() { new Role() { diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 724930c..3c38ab6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -16,14 +16,14 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public IList Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] - public IList Languages { get; set; } + public HashSet Languages { get; set; } [NotNull] [Required] - public IList Technologies { get; set; } + public HashSet Technologies { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 1d2d17b..5b80ba3 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -11,18 +11,18 @@ namespace DevHive.Web.Models.Identity.User { [NotNull] [Required] - public IList Roles { get; set; } = new List(); + public HashSet Roles { get; set; } = new HashSet(); [NotNull] [Required] - public IList Friends { get; set; } = new List(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] - public IList Languages { get; set; } = new List(); + public HashSet Languages { get; set; } = new HashSet(); [NotNull] [Required] - public IList Technologies { get; set; } = new List(); + public HashSet Technologies { get; set; } = new HashSet(); } } -- cgit v1.2.3 From df52b1068e16adc50ffd365e2e8b8ea19b59fac3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 11:44:41 +0200 Subject: UserUpdate does not allow updating roles if not admin; HTTP Put now works properly; UserUpdate validates properly --- .../Configurations/Mapping/RoleMapings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 5 +- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../Identity/User/UpdateFriendServiceModel.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 8 +- .../Models/Language/UpdateLanguageServiceModel.cs | 4 +- .../Technology/UpdateTechnologyServiceModel.cs | 4 +- src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 142 +++++++++++---------- .../Configurations/Mapping/RoleMappings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 14 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- .../Models/Identity/Role/UpdateRoleWebModel.cs | 6 +- .../Models/Identity/User/FriendWebModel.cs | 16 --- .../Models/Identity/User/UpdateUserWebModel.cs | 2 +- .../Models/Identity/User/UserWebModel.cs | 2 +- .../Models/Identity/User/UsernameWebModel.cs | 16 +++ 17 files changed, 125 insertions(+), 112 deletions(-) delete mode 100644 src/DevHive.Web/Models/Identity/User/FriendWebModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 23bd46f..e61a107 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,10 +9,10 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 6797ce1..096af38 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,11 +11,10 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); CreateMap() .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); - CreateMap(); - CreateMap() - .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name)); + CreateMap(); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index d3a45e5..d47728c 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -8,7 +8,7 @@ namespace DevHive.Services.Interfaces { Task CreateRole(CreateRoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); Task UpdateRole(UpdateRoleServiceModel roleServiceModel); diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs index 83fcc34..b0efe10 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -5,6 +5,6 @@ namespace DevHive.Services.Models.Identity.User public class UpdateFriendServiceModel { public Guid Id { get; set; } - public string Name { get; set; } + public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 3e41057..7da54b8 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,12 +7,12 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new(); } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index 8536693..84b7f27 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : LanguageServiceModel + public class UpdateLanguageServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs index a18e286..f4c7921 100644 --- a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs @@ -2,8 +2,10 @@ using System; namespace DevHive.Services.Models.Technology { - public class UpdateTechnologyServiceModel : TechnologyServiceModel + public class UpdateTechnologyServiceModel { + public Guid Id { get; set; } + public string Name { get; set; } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 9f7a5ac..a8b8e17 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -38,12 +38,12 @@ namespace DevHive.Services.Services } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); - return this._roleMapper.Map(role); + return this._roleMapper.Map(role); } public async Task UpdateRole(UpdateRoleServiceModel updateRoleServiceModel) diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 1beb07f..960630e 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,61 +111,9 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - /* Roles */ - int roleCount = updateUserServiceModel.Roles.Count; - for (int i = 0; i < roleCount; i++) - { - Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid role name!"); - - UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - - updateUserServiceModel.Roles.Add(updateRoleServiceModel); - } - - /* Languages */ - int langCount = updateUserServiceModel.Languages.Count; - for (int i = 0; i < langCount; i++) - { - Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid language name!"); - - UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - - updateUserServiceModel.Languages.Add(updateLanguageServiceModel); - } - //Clean the already replaced languages - updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - - /* Technologies */ - int techCount = updateUserServiceModel.Technologies.Count; - for (int i = 0; i < techCount; i++) - { - Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid technology name!"); - - UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - - updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); - } - //Clean the already replaced technologies - updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); - - /* Friends */ - HashSet friends = new(); - int friendsCount = updateUserServiceModel.Friends.Count; - for (int i = 0; i < friendsCount; i++) - { - User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid friend's username!"); - - friends.Add(friend); - } - //Clean the already replaced technologies - updateUserServiceModel.Friends.RemoveWhere(x => x.Id == Guid.Empty); + updateUserServiceModel = await this.PopulateUpdateModelWithIds(updateUserServiceModel); User user = this._userMapper.Map(updateUserServiceModel); - user.Friends = friends; bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); @@ -249,30 +197,49 @@ namespace DevHive.Services.Services private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { + //Do NOT allow a user to change his roles, unless he is an Admin + bool isAdmin = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)) + .Roles.Any(r => r.Name == Role.AdminRole); + + if (isAdmin) + { + // Roles + foreach (var role in updateUserServiceModel.Roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + //Preserve original user roles + else + { + HashSet roles = (await this._userRepository.GetByIdAsync(updateUserServiceModel.Id)).Roles; + + foreach (var role in roles) + { + Role returnedRole = await this._roleRepository.GetByNameAsync(role.Name) ?? + throw new ArgumentException($"Role {role.Name} does not exist!"); + } + } + // Friends foreach (var friend in updateUserServiceModel.Friends) { - User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); - - if (returnedFriend == null) - throw new ArgumentException($"User {friend.Name} does not exist!"); + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.UserName) ?? + throw new ArgumentException($"User {friend.UserName} does not exist!"); } // Languages foreach (var language in updateUserServiceModel.Languages) { - Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - - if (returnedLanguage == null) + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name) ?? throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology foreach (var technology in updateUserServiceModel.Technologies) { - Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - - if (returnedTechnology == null) + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name) ?? throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } @@ -306,12 +273,13 @@ namespace DevHive.Services.Services } #endregion + #region Misc public async Task SuperSecretPromotionToAdmin(Guid userId) { User user = await this._userRepository.GetByIdAsync(userId) ?? throw new ArgumentException("User does not exist! Can't promote shit in this country..."); - if(!await this._roleRepository.DoesNameExist("Admin")) + if (!await this._roleRepository.DoesNameExist("Admin")) { Role adminRole = new() { @@ -329,5 +297,51 @@ namespace DevHive.Services.Services return admin.Id; } + + private async Task PopulateUpdateModelWithIds(UpdateUserServiceModel updateUserServiceModel) + { + /* Roles */ + int roleCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < roleCount; i++) + { + Role role = await this._roleRepository.GetByNameAsync(updateUserServiceModel.Roles.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid role name!"); + + updateUserServiceModel.Roles.ElementAt(i).Id = role.Id; + } + + /* Languages */ + int langCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < langCount; i++) + { + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); + + updateUserServiceModel.Languages.ElementAt(i).Id = language.Id; + } + + /* Technologies */ + int techCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < techCount; i++) + { + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); + + updateUserServiceModel.Technologies.ElementAt(i).Id = technology.Id; + } + + /* Friends */ + int friendsCount = updateUserServiceModel.Friends.Count; + for (int i = 0; i < friendsCount; i++) + { + User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? + throw new ArgumentException("Invalid friend's username!"); + + updateUserServiceModel.Friends.ElementAt(i).Id = friend.Id; + } + + return updateUserServiceModel; + } + #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2f01f77..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -11,11 +11,11 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap() .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap(); + CreateMap(); CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index e80a69a..1b26cc9 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -2,8 +2,6 @@ using AutoMapper; using DevHive.Services.Models.Identity.User; using DevHive.Web.Models.Identity.User; using DevHive.Common.Models.Identity; -using DevHive.Web.Models.Language; -using DevHive.Web.Models.Technology; namespace DevHive.Web.Configurations.Mapping { @@ -21,16 +19,12 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); //Update - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(src => src.Id, dest => dest.Ignore()) - .ForMember(src => src.Name, dest => dest.MapFrom(p => p.UserName)); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index d8bb60c..c68a32b 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -40,7 +40,7 @@ namespace DevHive.Web.Controllers [Authorize(Policy = "User")] public async Task GetById(Guid id) { - ReadRoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 254affc..3870481 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -4,10 +4,12 @@ using System.Diagnostics.CodeAnalysis; namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel : RoleWebModel + public class UpdateRoleWebModel { [NotNull] [Required] - public Guid Id { get; set; } + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } } } diff --git a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs b/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs deleted file mode 100644 index d59bff5..0000000 --- a/src/DevHive.Web/Models/Identity/User/FriendWebModel.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.ComponentModel.DataAnnotations; -using System.Diagnostics.CodeAnalysis; -using DevHive.Web.Attributes; - -namespace DevHive.Web.Models.Identity.User -{ - public class FriendWebModel - { - [NotNull] - [Required] - [MinLength(3)] - [MaxLength(50)] - [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] - public string UserName { get; set; } - } -} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 30c66fb..62901f6 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -17,7 +17,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public HashSet Friends { get; set; } + public HashSet Friends { get; set; } [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 5b80ba3..4097901 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -15,7 +15,7 @@ namespace DevHive.Web.Models.Identity.User [NotNull] [Required] - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); [NotNull] [Required] diff --git a/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs new file mode 100644 index 0000000..a20c1bf --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UsernameWebModel.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; +using DevHive.Web.Attributes; + +namespace DevHive.Web.Models.Identity.User +{ + public class UsernameWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + [OnlyAlphanumerics(ErrorMessage = "Username can only contain letters and digits!")] + public string UserName { get; set; } + } +} -- cgit v1.2.3 From 6f7c7adced972944f5648b3714baa053037a4160 Mon Sep 17 00:00:00 2001 From: transtrike Date: Mon, 1 Feb 2021 18:28:55 +0200 Subject: GetByUsername & GetById return post Ids; Read POst returns URLs --- src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs | 2 ++ src/DevHive.Data/Repositories/PostRepository.cs | 13 ++++++++++++- src/DevHive.Data/Repositories/UserRepository.cs | 2 ++ src/DevHive.Services/Configurations/Mapping/PostMappings.cs | 4 ++++ .../Models/Identity/User/UserServiceModel.cs | 3 +++ src/DevHive.Services/Services/PostService.cs | 4 +++- src/DevHive.Web/Models/Identity/User/UserWebModel.cs | 11 +++++++---- 7 files changed, 33 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs index 5022df5..9f7cf85 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs @@ -8,6 +8,8 @@ namespace DevHive.Data.Interfaces.Repositories { public interface IPostRepository : IRepository { + Task AddNewPostToCreator(Guid userId, Post post); + Task GetPostByCreatorAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated); Task> GetFileUrls(Guid postId); diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 31eb7d0..623a8f8 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using DevHive.Data.Interfaces.Models; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -11,11 +12,21 @@ namespace DevHive.Data.Repositories public class PostRepository : BaseRepository, IPostRepository { private readonly DevHiveContext _context; + private readonly IUserRepository _userRepository; - public PostRepository(DevHiveContext context) + public PostRepository(DevHiveContext context, IUserRepository userRepository) : base(context) { this._context = context; + this._userRepository = userRepository; + } + + public async Task AddNewPostToCreator(Guid userId, Post post) + { + User user = await this._userRepository.GetByIdAsync(userId); + user.Posts.Add(post); + + return await this.SaveChangesAsync(); } #region Read diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 4bf919e..6f33570 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -38,6 +38,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Roles) .Include(x => x.Languages) .Include(x => x.Technologies) + .Include(x => x.Posts) .FirstOrDefaultAsync(x => x.Id == id); } @@ -48,6 +49,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Roles) .Include(x => x.Languages) .Include(x => x.Technologies) + .Include(x => x.Posts) .FirstOrDefaultAsync(x => x.UserName == username); } #endregion diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index d36dbdd..13c9736 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Post; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -20,6 +21,9 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.CreatorFirstName, src => src.MapFrom(p => p.Creator.FirstName)) .ForMember(dest => dest.CreatorLastName, src => src.MapFrom(p => p.Creator.LastName)) .ForMember(dest => dest.CreatorUsername, src => src.MapFrom(p => p.Creator.UserName)); + + CreateMap() + .ForMember(dest => dest.Id, src => src.MapFrom(x => x.Id)); } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 7da54b8..5bf58ec 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using DevHive.Common.Models.Misc; using DevHive.Services.Models.Identity.Role; using DevHive.Services.Models.Language; using DevHive.Services.Models.Technology; @@ -14,5 +15,7 @@ namespace DevHive.Services.Models.Identity.User public HashSet Languages { get; set; } = new(); public HashSet Technologies { get; set; } = new(); + + public List Posts { get; set; } = new(); } } diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 8a37acd..3206479 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -9,6 +9,7 @@ using System.Security.Claims; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; +using Microsoft.CodeAnalysis.CSharp; namespace DevHive.Services.Services { @@ -49,6 +50,8 @@ namespace DevHive.Services.Services Post newPost = await this._postRepository .GetPostByCreatorAndTimeCreatedAsync(post.Creator.Id, post.TimeCreated); + await this._postRepository.AddNewPostToCreator(createPostServiceModel.CreatorId, newPost); + return newPost.Id; } else @@ -69,7 +72,6 @@ namespace DevHive.Services.Services readPostServiceModel.CreatorFirstName = user.FirstName; readPostServiceModel.CreatorLastName = user.LastName; readPostServiceModel.CreatorUsername = user.UserName; - // readPostServiceModel.Files = await this._cloudService.GetFilesFromCloud(post.FileUrls); return readPostServiceModel; } diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 4097901..1a5484a 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; +using DevHive.Common.Models.Misc; using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -11,18 +12,20 @@ namespace DevHive.Web.Models.Identity.User { [NotNull] [Required] - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new(); [NotNull] [Required] - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new(); [NotNull] [Required] - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new(); [NotNull] [Required] - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new(); + + public List Posts { get; set; } = new(); } } -- cgit v1.2.3 From 01ad75fa5a871a0c9f8cd0c5291312286ae4d52d Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 3 Feb 2021 10:22:37 +0200 Subject: Implemented profile picture table functionality; added models and interfaces for profile picture; added ability for user layers to update the profile picture; added migrations; updated mappings --- src/DevHive.Data/DevHiveContext.cs | 5 + .../Interfaces/Models/IProfilePicture.cs | 13 + src/DevHive.Data/Interfaces/Models/IUser.cs | 2 +- .../Interfaces/Repositories/IUserRepository.cs | 1 + .../20210203071720_ProfilePicture.Designer.cs | 633 +++++++++++++++++++++ .../Migrations/20210203071720_ProfilePicture.cs | 52 ++ .../Migrations/DevHiveContextModelSnapshot.cs | 36 +- src/DevHive.Data/Models/ProfilePicture.cs | 14 + src/DevHive.Data/Models/User.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 10 + .../Configurations/Mapping/UserMappings.cs | 6 +- src/DevHive.Services/Interfaces/IUserService.cs | 1 + .../Identity/User/ProfilePictureServiceModel.cs | 7 + .../User/UpdateProfilePictureServiceModel.cs | 12 + .../Models/Identity/User/UpdateUserServiceModel.cs | 2 + .../Models/Identity/User/UserServiceModel.cs | 2 + src/DevHive.Services/Services/UserService.cs | 29 +- .../Configurations/Mapping/UserMappings.cs | 3 + src/DevHive.Web/Controllers/UserController.cs | 17 + .../Models/Identity/User/ProfilePictureWebModel.cs | 7 + .../Identity/User/UpdateProfilePictureWebModel.cs | 9 + .../Models/Identity/User/UserWebModel.cs | 2 + 22 files changed, 857 insertions(+), 8 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/Models/IProfilePicture.cs create mode 100644 src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs create mode 100644 src/DevHive.Data/Models/ProfilePicture.cs create mode 100644 src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs create mode 100644 src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs (limited to 'src/DevHive.Web/Models/Identity/User/UserWebModel.cs') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 417de7f..b3ebd73 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -27,6 +27,11 @@ namespace DevHive.Data .HasIndex(x => x.UserName) .IsUnique(); + builder.Entity() + .HasOne(x => x.ProfilePicture) + .WithOne(x => x.User) + .HasForeignKey(x => x.UserId); + /* Roles */ builder.Entity() .HasMany(x => x.Roles) diff --git a/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs b/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs new file mode 100644 index 0000000..c3fcbea --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IProfilePicture.cs @@ -0,0 +1,13 @@ +using System; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IProfilePicture : IModel + { + Guid UserId { get; set; } + User User { get; set; } + + string PictureURL { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index eb262fd..fcd741c 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -10,7 +10,7 @@ namespace DevHive.Data.Interfaces.Models string LastName { get; set; } - string ProfilePictureUrl { get; set; } + ProfilePicture ProfilePicture { get; set; } HashSet Languages { get; set; } diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index 4346e9c..5b6ab9e 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -11,6 +11,7 @@ namespace DevHive.Data.Interfaces.Repositories //Read Task GetByUsernameAsync(string username); IEnumerable QueryAll(); + Task UpdateProfilePicture(Guid userId, string pictureUrl); //Validations Task DoesEmailExistAsync(string email); diff --git a/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs new file mode 100644 index 0000000..b8dbd8e --- /dev/null +++ b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.Designer.cs @@ -0,0 +1,633 @@ +// +using System; +using System.Collections.Generic; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210203071720_ProfilePicture")] + partial class ProfilePicture + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.HasIndex("PostId"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CreatorId") + .HasColumnType("uuid"); + + b.Property>("FileUrls") + .HasColumnType("text[]"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.HasIndex("CreatorId"); + + b.ToTable("Posts"); + }); + + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("PictureURL") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("ProfilePicture"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Rating", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.ToTable("Rating"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("PostId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "PostId"); + + b.HasIndex("PostId"); + + b.ToTable("RatedPosts"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriend", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("FriendId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "FriendId"); + + b.HasIndex("FriendId"); + + b.ToTable("UserFriends"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Rate") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("UserRates"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.Property("LanguagesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("LanguagesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("LanguageUser"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.Property("TechnologiesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("TechnologiesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("TechnologyUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Comments") + .HasForeignKey("CreatorId"); + + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany("Comments") + .HasForeignKey("PostId"); + + b.Navigation("Creator"); + + b.Navigation("Post"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.HasOne("DevHive.Data.Models.User", "Creator") + .WithMany("Posts") + .HasForeignKey("CreatorId"); + + b.Navigation("Creator"); + }); + + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithOne("ProfilePicture") + .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => + { + b.HasOne("DevHive.Data.Models.Post", "Post") + .WithMany() + .HasForeignKey("PostId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Post"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserFriend", b => + { + b.HasOne("DevHive.Data.Models.User", "Friend") + .WithMany("FriendsOf") + .HasForeignKey("FriendId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany("MyFriends") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Friend"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("DevHive.Data.RelationModels.UserRate", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithMany() + .HasForeignKey("UserId"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("LanguageUser", b => + { + b.HasOne("DevHive.Data.Models.Language", null) + .WithMany() + .HasForeignKey("LanguagesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("TechnologyUser", b => + { + b.HasOne("DevHive.Data.Models.Technology", null) + .WithMany() + .HasForeignKey("TechnologiesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.Post", b => + { + b.Navigation("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Comments"); + + b.Navigation("FriendsOf"); + + b.Navigation("MyFriends"); + + b.Navigation("Posts"); + + b.Navigation("ProfilePicture"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs new file mode 100644 index 0000000..1b0c2c6 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210203071720_ProfilePicture.cs @@ -0,0 +1,52 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class ProfilePicture : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers"); + + migrationBuilder.CreateTable( + name: "ProfilePicture", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + UserId = table.Column(type: "uuid", nullable: false), + PictureURL = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_ProfilePicture", x => x.Id); + table.ForeignKey( + name: "FK_ProfilePicture_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ProfilePicture_UserId", + table: "ProfilePicture", + column: "UserId", + unique: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ProfilePicture"); + + migrationBuilder.AddColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers", + type: "text", + nullable: true); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index 96cabad..0450670 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -86,6 +86,26 @@ namespace DevHive.Data.Migrations b.ToTable("Posts"); }); + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("PictureURL") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId") + .IsUnique(); + + b.ToTable("ProfilePicture"); + }); + modelBuilder.Entity("DevHive.Data.Models.Rating", b => { b.Property("Id") @@ -190,9 +210,6 @@ namespace DevHive.Data.Migrations b.Property("PhoneNumberConfirmed") .HasColumnType("boolean"); - b.Property("ProfilePictureUrl") - .HasColumnType("text"); - b.Property("SecurityStamp") .HasColumnType("text"); @@ -437,6 +454,17 @@ namespace DevHive.Data.Migrations b.Navigation("Creator"); }); + modelBuilder.Entity("DevHive.Data.Models.ProfilePicture", b => + { + b.HasOne("DevHive.Data.Models.User", "User") + .WithOne("ProfilePicture") + .HasForeignKey("DevHive.Data.Models.ProfilePicture", "UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("User"); + }); + modelBuilder.Entity("DevHive.Data.RelationModels.RatedPost", b => { b.HasOne("DevHive.Data.Models.Post", "Post") @@ -594,6 +622,8 @@ namespace DevHive.Data.Migrations b.Navigation("MyFriends"); b.Navigation("Posts"); + + b.Navigation("ProfilePicture"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Models/ProfilePicture.cs b/src/DevHive.Data/Models/ProfilePicture.cs new file mode 100644 index 0000000..e2ef04b --- /dev/null +++ b/src/DevHive.Data/Models/ProfilePicture.cs @@ -0,0 +1,14 @@ +using System; + +namespace DevHive.Data.Models +{ + public class ProfilePicture + { + public Guid Id { get; set; } + + public Guid UserId { get; set; } + public User User { get; set; } + + public string PictureURL { get; set; } + } +} diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 1c365e4..d73f989 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -14,7 +14,7 @@ namespace DevHive.Data.Models public string LastName { get; set; } - public string ProfilePictureUrl { get; set; } + public ProfilePicture ProfilePicture { get; set; } public HashSet Languages { get; set; } = new(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index b46198c..466b123 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -38,6 +38,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Languages) .Include(x => x.Technologies) .Include(x => x.Posts) + .Include(x => x.ProfilePicture) .FirstOrDefaultAsync(x => x.Id == id); } @@ -48,6 +49,7 @@ namespace DevHive.Data.Repositories .Include(x => x.Languages) .Include(x => x.Technologies) .Include(x => x.Posts) + .Include(x => x.ProfilePicture) .FirstOrDefaultAsync(x => x.UserName == username); } #endregion @@ -93,6 +95,14 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(); } + + public async Task UpdateProfilePicture(Guid userId, string pictureUrl) { + User user = await this.GetByIdAsync(userId); + + user.ProfilePicture.PictureURL = pictureUrl; + + return await this.SaveChangesAsync(); + } #endregion #region Validations diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 5223d84..6922cd7 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -22,10 +22,12 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap() - .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.MyFriends)); + .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.MyFriends)) + .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); // .ForMember(dest => dest.Friends, src => src.MapFrom(p => p.Friends)); CreateMap() - .ForMember(x => x.Password, opt => opt.Ignore()); + .ForMember(x => x.Password, opt => opt.Ignore()) + .ForMember(dest => dest.ProfilePictureURL, src => src.MapFrom(p => p.ProfilePicture.PictureURL)); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index b701e4a..9e2b4e3 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -14,6 +14,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); + Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel); Task DeleteUser(Guid id); diff --git a/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs b/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs new file mode 100644 index 0000000..ad81057 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/ProfilePictureServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class ProfilePictureServiceModel + { + public string ProfilePictureURL { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs new file mode 100644 index 0000000..8563953 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateProfilePictureServiceModel.cs @@ -0,0 +1,12 @@ +using System; +using Microsoft.AspNetCore.Http; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateProfilePictureServiceModel + { + public Guid UserId { get; set; } + + public IFormFile Picture { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 5b197e4..b4e4400 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -12,6 +12,8 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } + public string ProfilePictureURL { get; set; } + public HashSet Roles { get; set; } = new HashSet(); public HashSet Friends { get; set; } = new HashSet(); diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 5bf58ec..ac7bba2 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -8,6 +8,8 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { + public string ProfilePictureURL { get; set; } + public HashSet Roles { get; set; } = new(); public HashSet Friends { get; set; } = new(); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 09b56c1..d8c4ee5 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,6 +15,7 @@ using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; using DevHive.Data.RelationModels; +using Microsoft.AspNetCore.Http; namespace DevHive.Services.Services { @@ -26,13 +27,15 @@ namespace DevHive.Services.Services private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; + private readonly ICloudService _cloudService; public UserService(IUserRepository userRepository, ILanguageRepository languageRepository, IRoleRepository roleRepository, ITechnologyRepository technologyRepository, IMapper mapper, - JWTOptions jwtOptions) + JWTOptions jwtOptions, + ICloudService cloudService) { this._userRepository = userRepository; this._roleRepository = roleRepository; @@ -40,6 +43,7 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; this._languageRepository = languageRepository; this._technologyRepository = technologyRepository; + this._cloudService = cloudService; } #region Authentication @@ -66,6 +70,7 @@ namespace DevHive.Services.Services User user = this._userMapper.Map(registerModel); user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); + user.ProfilePicture = new ProfilePicture() { PictureURL = String.Empty }; // Make sure the default role exists //TODO: Move when project starts @@ -119,6 +124,28 @@ namespace DevHive.Services.Services return this._userMapper.Map( await this._userRepository.GetByIdAsync(user.Id)); } + + public async Task UpdateProfilePicture(UpdateProfilePictureServiceModel updateProfilePictureServiceModel) + { + User user = await this._userRepository.GetByIdAsync(updateProfilePictureServiceModel.UserId); + + if (!String.IsNullOrEmpty(user.ProfilePicture.PictureURL)) + { + bool success = await _cloudService.RemoveFilesFromCloud(new List { user.ProfilePicture.PictureURL }); + if (!success) + throw new InvalidCastException("Could not delete old profile picture!"); + } + + string fileUrl = (await this._cloudService.UploadFilesToCloud(new List { updateProfilePictureServiceModel.Picture }))[0] ?? + throw new ArgumentNullException("Unable to upload profile picture to cloud"); + + bool successful = await this._userRepository.UpdateProfilePicture(updateProfilePictureServiceModel.UserId, fileUrl); + + if (!successful) + throw new InvalidOperationException("Unable to change profile picture!"); + + return new ProfilePictureServiceModel() { ProfilePictureURL = fileUrl }; + } #endregion #region Delete diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 1b26cc9..f58e7ca 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -23,6 +23,9 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index fdf317c..109bbaa 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -94,6 +94,23 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } + + [HttpPut] + [Route("ProfilePicture")] + [Authorize(Roles = "User,Admin")] + public async Task UpdateProfilePicture(Guid userId, [FromForm] UpdateProfilePictureWebModel updateProfilePictureWebModel, [FromHeader] string authorization) + { + if (!await this._userService.ValidJWT(userId, authorization)) + return new UnauthorizedResult(); + + UpdateProfilePictureServiceModel updateProfilePictureServiceModel = this._userMapper.Map(updateProfilePictureWebModel); + updateProfilePictureServiceModel.UserId = userId; + + ProfilePictureServiceModel profilePictureServiceModel = await this._userService.UpdateProfilePicture(updateProfilePictureServiceModel); + ProfilePictureWebModel profilePictureWebModel = this._userMapper.Map(profilePictureServiceModel); + + return new AcceptedResult("UpdateProfilePicture", profilePictureWebModel); + } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs b/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs new file mode 100644 index 0000000..9fb1516 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/ProfilePictureWebModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Identity.User +{ + public class ProfilePictureWebModel + { + public string ProfilePictureURL { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs new file mode 100644 index 0000000..6efe968 --- /dev/null +++ b/src/DevHive.Web/Models/Identity/User/UpdateProfilePictureWebModel.cs @@ -0,0 +1,9 @@ +using Microsoft.AspNetCore.Http; + +namespace DevHive.Web.Models.Identity.User +{ + public class UpdateProfilePictureWebModel + { + public IFormFile Picture { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs index 1a5484a..7ab8cca 100644 --- a/src/DevHive.Web/Models/Identity/User/UserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UserWebModel.cs @@ -10,6 +10,8 @@ namespace DevHive.Web.Models.Identity.User { public class UserWebModel : BaseUserWebModel { + public string ProfilePictureURL { get; set; } + [NotNull] [Required] public HashSet Roles { get; set; } = new(); -- cgit v1.2.3