From 1f84b7d7da1464fab8178188f97164e4718527ed Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 15 Dec 2020 11:02:54 +0200 Subject: Updated role controller and role service to use custom models and implemented role mapping --- src/DevHive.Web/Configurations/Mapping/RoleMappings.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/DevHive.Web/Configurations/Mapping/RoleMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs new file mode 100644 index 0000000..9ccb7d7 --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -0,0 +1,15 @@ +using AutoMapper; +using DevHive.Web.Models.Identity.Role; +using DevHive.Services.Models.Identity.Role; + +namespace DevHive.Web.Configurations.Mapping +{ + public class RoleMappings : Profile + { + public RoleMappings() + { + CreateMap(); + CreateMap(); + } + } +} -- cgit v1.2.3 From fadb0fd1b7a13d1c6210e11f2db2add7c8fd45a9 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 17 Dec 2020 11:59:52 +0200 Subject: Reworked RoleController and RoleService to be consistent with the app structure --- .../Configurations/Mapping/RoleMapings.cs | 1 + src/DevHive.Services/Services/RoleService.cs | 39 +++++++++------------- .../Configurations/Mapping/RoleMappings.cs | 2 ++ src/DevHive.Web/Controllers/RoleController.cs | 35 ++++++++++++++----- .../Models/Identity/Role/RoleWebModel.cs | 10 ++++++ .../Models/Identity/Role/UpdateRoleWebModel.cs | 6 +--- 6 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 0e06523..25b314a 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,7 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c04bf31..d5c1848 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -4,7 +4,6 @@ using AutoMapper; using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.Role; -using Microsoft.AspNetCore.Mvc; namespace DevHive.Services.Services { @@ -19,51 +18,43 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role already exists!"); + throw new ArgumentException("Role already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.AddAsync(role); - - return new CreatedResult("CreateRole", role); + return await this._roleRepository.AddAsync(role); } - public async Task GetRoleById(Guid id) + public async Task GetRoleById(Guid id) { - Role role = await this._roleRepository.GetByIdAsync(id); - - if (role == null) - return new NotFoundObjectResult("Role does not exist!"); + Role role = await this._roleRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Role does not exist!"); - return new ObjectResult(role); + return this._roleMapper.Map(role); } - public async Task UpdateRole(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); - if (!await this._roleRepository.DoesNameExist(roleServiceModel.Name)) - return new BadRequestObjectResult("Role name already exists!"); + if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) + throw new ArgumentException("Role name already exists!"); Role role = this._roleMapper.Map(roleServiceModel); - await this._roleRepository.EditAsync(role); - - return new AcceptedResult("UpdateRole", role); + return await this._roleRepository.EditAsync(role); } - public async Task DeleteRole(Guid id) + public async Task DeleteRole(Guid id) { if (!await this._roleRepository.DoesRoleExist(id)) - return new NotFoundObjectResult("Role does not exist!"); + throw new ArgumentException("Role does not exist!"); Role role = await this._roleRepository.GetByIdAsync(id); - await this._roleRepository.DeleteAsync(role); - - return new OkResult(); + return await this._roleRepository.DeleteAsync(role); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 9ccb7d7..2743df3 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -10,6 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 610d370..1664f4f 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -25,33 +25,52 @@ namespace DevHive.Web.Controllers } [HttpPost] - public Task Create(CreateRoleWebModel createRoleWebModel) + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { RoleServiceModel roleServiceModel = this._roleMapper.Map(createRoleWebModel); - return this._roleService.CreateRole(roleServiceModel); + bool result = await this._roleService.CreateRole(roleServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not create role!"); + + return new OkResult(); } [HttpGet] - public Task GetById(Guid id) + public async Task GetById(Guid id) { - return this._roleService.GetRoleById(id); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + + return new OkObjectResult(roleWebModel); } [HttpPut] - public Task Update(UpdateRoleWebModel updateRoleWebModel) + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { RoleServiceModel roleServiceModel = this._roleMapper.Map(updateRoleWebModel); + roleServiceModel.Id = id; + + bool result = await this._roleService.UpdateRole(roleServiceModel); - return this._roleService.UpdateRole(roleServiceModel); + if (!result) + return new BadRequestObjectResult("Could not update role!"); + + return new OkResult(); } [HttpDelete] - public Task Delete(Guid id) + public async Task Delete(Guid id) { - return this._roleService.DeleteRole(id); + bool result = await this._roleService.DeleteRole(id); + + if (!result) + return new BadRequestObjectResult("Could nor delete role!"); + + return new OkResult(); } } } 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..d8ae465 --- /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 Guid Id { get; set; } + 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 b970dd5..213ec55 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,10 +1,6 @@ -using System; - namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel + public class UpdateRoleWebModel : CreateRoleWebModel { - public Guid Id { get; set; } - public string NewName { 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/Configurations/Mapping/RoleMappings.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 875857f118d0364a94eb88bf03673e1d0165c23c Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 17 Dec 2020 19:42:45 +0200 Subject: Fixed namespaces --- .../Configurations/Mapping/RoleMapings.cs | 6 +++--- .../Models/Identity/User/UserServiceModel.cs | 2 -- src/DevHive.Services/Services/RoleService.cs | 10 +++++----- .../Configurations/Mapping/RoleMappings.cs | 10 +++++----- src/DevHive.Web/Controllers/RoleController.cs | 21 ++++++++++----------- .../Models/Identity/Role/CreateRoleWebModel.cs | 2 +- .../Models/Identity/Role/UpdateRoleWebModel.cs | 2 +- 7 files changed, 25 insertions(+), 28 deletions(-) (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 25b314a..db4473e 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,6 +1,6 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Identity.Role; +using DevHive.Common.Models.Identity; namespace DevHive.Services.Configurations.Mapping { @@ -8,8 +8,8 @@ namespace DevHive.Services.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 8ac71b1..de57faf 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Models.Identity.User { diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index d5c1848..c0b9062 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,9 +1,9 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Common.Models.Identity; using DevHive.Data.Models; using DevHive.Data.Repositories; -using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -18,7 +18,7 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleServiceModel roleServiceModel) + public async Task CreateRole(RoleModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); @@ -28,15 +28,15 @@ namespace DevHive.Services.Services return await this._roleRepository.AddAsync(role); } - 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(RoleServiceModel roleServiceModel) + public async Task UpdateRole(RoleModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 4bc25c4..8c5cd85 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,6 +1,6 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Services.Models.Identity.Role; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { @@ -8,11 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 1664f4f..fc05481 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -4,13 +4,12 @@ using DevHive.Services.Services; using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; -using DevHive.Services.Models.Identity.Role; using System; -using Microsoft.AspNetCore.Authorization; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController @@ -25,10 +24,10 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) + public async Task Create([FromBody] CreateRoleModel createRoleModel) { - RoleServiceModel roleServiceModel = - this._roleMapper.Map(createRoleWebModel); + RoleModel roleServiceModel = + this._roleMapper.Map(createRoleModel); bool result = await this._roleService.CreateRole(roleServiceModel); @@ -41,17 +40,17 @@ namespace DevHive.Web.Controllers [HttpGet] public async Task GetById(Guid id) { - RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); + RoleModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) + public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) { - RoleServiceModel roleServiceModel = - this._roleMapper.Map(updateRoleWebModel); + RoleModel roleServiceModel = + this._roleMapper.Map(updateRoleModel); roleServiceModel.Id = id; bool result = await this._roleService.UpdateRole(roleServiceModel); diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index e872428..becb3c9 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class CreateRoleWebModel + public class CreateRoleModel { 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 213ec55..1eaad57 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleWebModel : CreateRoleWebModel + public class UpdateRoleModel : CreateRoleModel { } } -- 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/Configurations/Mapping/RoleMappings.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 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/Configurations/Mapping/RoleMappings.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 8d1d0b40d56f90248f948e474330258bf57cf0b6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 17 Jan 2021 14:45:48 +0200 Subject: Fixed role implementation by bringing back and improving all role models --- src/DevHive.Common/Models/Identity/RoleModel.cs | 10 -------- .../Configurations/Mapping/RoleMapings.cs | 8 +++---- src/DevHive.Services/Interfaces/IRoleService.cs | 10 ++++---- .../Models/Identity/Role/RoleServiceModel.cs | 10 ++++++++ .../Models/Identity/Role/UpdateRoleServiceModel.cs | 7 ++++++ .../Models/Identity/User/UserServiceModel.cs | 6 ++--- src/DevHive.Services/Services/RoleService.cs | 24 +++++++++++++------ .../Configurations/Mapping/RoleMappings.cs | 12 +++++----- src/DevHive.Web/Controllers/RoleController.cs | 28 +++++++++++----------- .../Models/Identity/Role/CreateRoleWebModel.cs | 2 +- .../Models/Identity/Role/RoleWebModel.cs | 2 -- .../Models/Identity/Role/UpdateRoleWebModel.cs | 2 +- 12 files changed, 68 insertions(+), 53 deletions(-) delete mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Common/Models/Identity/RoleModel.cs b/src/DevHive.Common/Models/Identity/RoleModel.cs deleted file mode 100644 index 5db8df9..0000000 --- a/src/DevHive.Common/Models/Identity/RoleModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -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/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 65b0b5a..4ddd253 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -1,15 +1,15 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index 2c7195c..a7a0e47 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -1,16 +1,16 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Interfaces { - public interface IRoleService + public interface IRoleService { - Task CreateRole(RoleModel roleServiceModel); + Task CreateRole(RoleServiceModel roleServiceModel); - Task GetRoleById(Guid id); + Task GetRoleById(Guid id); - Task UpdateRole(RoleModel roleServiceModel); + Task UpdateRole(RoleServiceModel roleServiceModel); Task DeleteRole(Guid id); } diff --git a/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs new file mode 100644 index 0000000..3f834ef --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs @@ -0,0 +1,10 @@ +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/Role/UpdateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs new file mode 100644 index 0000000..be71771 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.Role +{ + public class UpdateRoleServiceModel + { + 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 ecf8c42..aa77a0c 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -1,13 +1,13 @@ using System.Collections.Generic; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; using DevHive.Services.Models.Language; using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { - public class UserServiceModel : BaseUserServiceModel + public class UserServiceModel : BaseUserServiceModel { - public IList Roles { get; set; } = new List(); + public IList Roles { get; set; } = new List(); public IList Friends { get; set; } = new List(); diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index c38ac74..0945624 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -1,14 +1,15 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Common.Models.Identity; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { - public class RoleService : IRoleService + public class RoleService : IRoleService { private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; @@ -19,25 +20,34 @@ namespace DevHive.Services.Services this._roleMapper = mapper; } - public async Task CreateRole(RoleModel roleServiceModel) + public async Task CreateRole(RoleServiceModel roleServiceModel) { if (await this._roleRepository.DoesNameExist(roleServiceModel.Name)) throw new ArgumentException("Role already exists!"); + Role role = this._roleMapper.Map(roleServiceModel); + bool success = await this._roleRepository.AddAsync(role); + + if(success) + { + Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); + return newRole.Id; + } + else + return Guid.Empty; - return await this._roleRepository.AddAsync(role); } - 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(RoleModel roleServiceModel) + public async Task UpdateRole(RoleServiceModel roleServiceModel) { if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id)) throw new ArgumentException("Role does not exist!"); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index afa3a94..bce7c07 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -1,18 +1,18 @@ using AutoMapper; using DevHive.Web.Models.Identity.Role; -using DevHive.Common.Models.Identity; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 8ea2711..5b3dca5 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -3,12 +3,12 @@ using Microsoft.AspNetCore.Mvc; using DevHive.Web.Models.Identity.Role; using AutoMapper; using System; -using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Controllers { - [ApiController] + [ApiController] [Route("/api/[controller]")] //[Authorize(Roles = "Admin")] public class RoleController @@ -23,33 +23,33 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] CreateRoleModel createRoleModel) + public async Task Create([FromBody] CreateRoleWebModel createRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(createRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(createRoleWebModel); - bool result = await this._roleService.CreateRole(roleServiceModel); + Guid id = await this._roleService.CreateRole(roleServiceModel); - if (!result) - return new BadRequestObjectResult("Could not create role!"); + return id == Guid.Empty ? + new BadRequestObjectResult($"Could not create role {createRoleWebModel.Name}") : + new OkObjectResult(new { Id = id }); - return new OkResult(); } [HttpGet] public async Task GetById(Guid id) { - RoleModel roleServiceModel = await this._roleService.GetRoleById(id); - RoleModel roleWebModel = this._roleMapper.Map(roleServiceModel); + RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); } [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateRoleModel updateRoleModel) + public async Task Update(Guid id, [FromBody] UpdateRoleWebModel updateRoleWebModel) { - RoleModel roleServiceModel = - this._roleMapper.Map(updateRoleModel); + RoleServiceModel roleServiceModel = + this._roleMapper.Map(updateRoleWebModel); roleServiceModel.Id = id; bool result = await this._roleService.UpdateRole(roleServiceModel); diff --git a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs index becb3c9..e872428 100644 --- a/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/CreateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class CreateRoleModel + public class CreateRoleWebModel { 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 9e97ffc..41ade23 100644 --- a/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/RoleWebModel.cs @@ -1,5 +1,3 @@ -using System; - namespace DevHive.Web.Models.Identity.Role { public class RoleWebModel diff --git a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs index 1eaad57..213ec55 100644 --- a/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs +++ b/src/DevHive.Web/Models/Identity/Role/UpdateRoleWebModel.cs @@ -1,6 +1,6 @@ namespace DevHive.Web.Models.Identity.Role { - public class UpdateRoleModel : CreateRoleModel + public class UpdateRoleWebModel : CreateRoleWebModel { } } -- cgit v1.2.3 From f0398cf1b7e6477bbd184e7509a1030054fc1926 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 16:26:32 +0200 Subject: Fix lang naming --- src/DevHive.Data/Interfaces/Models/IUser.cs | 2 +- ...112111416_User_Implements_Languages.Designer.cs | 4 ++-- .../Migrations/DevHiveContextModelSnapshot.cs | 4 ++-- src/DevHive.Data/Models/User.cs | 4 ++-- src/DevHive.Data/Repositories/UserRepository.cs | 16 +++++++-------- .../Mapping/UserCollectionMappings.cs | 21 ++++++++++++++++++++ .../Configurations/Mapping/UserMappings.cs | 3 --- src/DevHive.Services/Services/UserService.cs | 23 ++++++++++++++-------- .../DevHive.Data.Tests/UserRepositoryTests.cs | 8 ++++---- .../Configurations/Mapping/RoleMappings.cs | 2 +- 10 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index 0a770f0..ef8c927 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,7 +8,7 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Langauges { get; set; } + IList Languages { get; set; } IList Technologies { get; set; } IList Roles { get; set; } IList Friends { get; set; } diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs index 0f1aa80..1605b5b 100644 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs @@ -307,7 +307,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -395,7 +395,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index cc6d24d..7197c81 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -305,7 +305,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -393,7 +393,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 944bf6a..31e36ac 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -18,12 +18,12 @@ namespace DevHive.Data.Models /// /// Languages that the user uses or is familiar with /// - public IList Langauges { get; set; } + public IList Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } + public IList Technologies { get; set; } = new List(); public IList Roles { get; set; } = new List(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 17ca93b..6d4a0bf 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -40,7 +40,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Add(language); + user.Languages.Add(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -70,7 +70,7 @@ namespace DevHive.Data.Repositories return await this._context.Users .Include(x => x.Friends) .Include(x => x.Roles) - .Include(x => x.Langauges) + .Include(x => x.Languages) .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.Id == id); } @@ -84,12 +84,12 @@ namespace DevHive.Data.Repositories public IList GetUserLanguages(User user) { - return user.Langauges; + return user.Languages; } public Language GetUserLanguage(User user, Language language) { - return user.Langauges + return user.Languages .FirstOrDefault(x => x.Id == language.Id); } @@ -123,8 +123,8 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(oldLang); - user.Langauges.Add(newLang); + user.Languages.Remove(oldLang); + user.Languages.Add(newLang); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -162,7 +162,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(language); + user.Languages.Remove(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -224,7 +224,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisLanguage(User user, Language language) { - return user.Langauges.Contains(language); + return user.Languages.Contains(language); } public bool DoesUserHaveThisTechnology(User user, Technology technology) diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs new file mode 100644 index 0000000..ee505a2 --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Identity.User; + +namespace DevHive.Services.Configurations.Mapping +{ + public class UserCollectionMappings : Profile + { + public UserCollectionMappings() + { + CreateMap() + .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + } + } +} diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 97355d6..d57c6ba 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,9 +11,6 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap() - .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); - CreateMap(); } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 3dd030a..b549b1c 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -131,21 +131,28 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateUser(UpdateUserServiceModel updateModel) + public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - if (!await this._userRepository.DoesUserExistAsync(updateModel.Id)) + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) throw new ArgumentException("User does not exist!"); - if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) throw new ArgumentException("Username already exists!"); - await this.ValidateUserCollections(updateModel); + await this.ValidateUserCollections(updateUserServiceModel); - User user = this._userMapper.Map(updateModel); - bool result = await this._userRepository.EditAsync(user); + //Query proper lang, tech and role and insert the full class in updateUserServiceModel + List properLanguages = new(); + foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) + properLanguages.Add(await this._languageRepository.GetByNameAsync(lang.Name)); - if (!result) + User user = this._userMapper.Map(updateUserServiceModel); + user.Languages = properLanguages; + + bool success = await this._userRepository.EditAsync(user); + + if (!success) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index 46f8aaf..b0a5b93 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -86,7 +86,7 @@ namespace DevHive.Data.Tests //Assert Assert.True(result, "The language isn't inserted properly to the database"); - Assert.True(dummyUser.Langauges.Contains(language), "The language doesn't get added properly to the user"); + Assert.True(dummyUser.Languages.Contains(language), "The language doesn't get added properly to the user"); } [Test] @@ -165,7 +165,7 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Langauges; + IList dummyUserLanguages = dummyUser.Languages; //Act IList languages = this._userRepository.GetUserLanguages(dummyUser); @@ -229,7 +229,7 @@ namespace DevHive.Data.Tests FirstName = "Spas", LastName = "Spasov", Email = "abv@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; @@ -271,7 +271,7 @@ namespace DevHive.Data.Tests FirstName = "Alex", LastName = "Spiridonov", Email = "a_spiridonov@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index bce7c07..66ae8e3 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index b5541f9..d6c8511 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -12,7 +12,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 079ec3e..0103ccf 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -11,8 +11,10 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs deleted file mode 100644 index 7a773e8..0000000 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ /dev/null @@ -1,22 +0,0 @@ -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Services.Models.Identity.User; - -namespace DevHive.Services.Configurations.Mapping -{ - public class UserCollectionMappings : Profile - { - public UserCollectionMappings() - { - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 88be0c8..923e9bb 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,7 +18,6 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs new file mode 100644 index 0000000..53bea9e --- /dev/null +++ b/src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Services.Models.Identity.Role +{ + public class CreateRoleServiceModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs index 63d57f7..a784f5c 100644 --- a/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs @@ -1,7 +1,10 @@ +using System; + namespace DevHive.Services.Models.Identity.User { public class FriendServiceModel { + public Guid Id { get; set; } public string UserName { get; set; } } } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs new file mode 100644 index 0000000..83fcc34 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateFriendServiceModel + { + public Guid Id { get; set; } + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class UpdateUserCollectionServiceModel - { - public string Name { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 835bf54..9277e3e 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using DevHive.Services.Models.Identity.Role; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { @@ -9,13 +12,13 @@ namespace DevHive.Services.Models.Identity.User public string Password { get; set; } - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index dca00fa..a57fd23 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -14,8 +14,9 @@ using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; using System.Linq; using DevHive.Common.Models.Misc; -using System.Reflection; -using Microsoft.CodeAnalysis.CSharp.Syntax; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; +using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Services { @@ -135,69 +136,57 @@ namespace DevHive.Services.Services public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - //Method: ValidateUserOnUpdate - if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) - throw new ArgumentException("User does not exist!"); - - if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) - throw new ArgumentException("Username already exists!"); + await this.ValidateUserOnUpdate(updateUserServiceModel); await this.ValidateUserCollections(updateUserServiceModel); - //Method: Insert collections to user - HashSet languages = new(); - foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) - languages.Add(await this._languageRepository.GetByNameAsync(lang.Name) ?? - throw new ArgumentException("Invalid language name!")); + //Preserve 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!"); - HashSet technologies = new(); - foreach (UpdateUserCollectionServiceModel tech in updateUserServiceModel.Technologies) - technologies.Add(await this._technologyRepository.GetByNameAsync(tech.Name) ?? - throw new ArgumentException("Invalid technology name!")); + UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map(role); - User user = this._userMapper.Map(updateUserServiceModel); + updateUserServiceModel.Roles.Add(updateRoleServiceModel); + } - user.Languages = languages; - user.Technologies = technologies; + 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!"); - bool successful = await this._userRepository.EditAsync(user); + UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List patchList) - { - User user = await this._userRepository.GetByIdAsync(id) ?? - throw new ArgumentException("User does not exist!"); + 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 updateUserServiceModel = this._userMapper.Map(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(technology); - foreach (Patch patch in patchList) - { - bool successful = patch.Action switch - { - "replace" => ReplacePatch(updateUserServiceModel, patch), - "add" => AddPatch(updateUserServiceModel, patch), - "remove" => RemovePatch(updateUserServiceModel, patch), - _ => throw new ArgumentException("Invalid patch operation!"), - }; - - if (!successful) - throw new ArgumentException("A problem occurred while applying patch"); + updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel); } - bool success = await this._userRepository.EditAsync(user); - if (success) - { - user = await this._userRepository.GetByIdAsync(id); - return this._userMapper.Map(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(user); } #endregion @@ -282,10 +271,20 @@ namespace DevHive.Services.Services return toReturn; } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) + throw new ArgumentException("User does not exist!"); + + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) + throw new ArgumentException("Username already exists!"); + } + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) { // Friends - foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + foreach (var friend in updateUserServiceModel.Friends) { User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); @@ -294,29 +293,24 @@ namespace DevHive.Services.Services } // Languages - foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + foreach (var language in updateUserServiceModel.Languages) { Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); - if (default(Language) == returnedLanguage) + if (returnedLanguage == null) throw new ArgumentException($"Language {language.Name} does not exist!"); } // Technology - foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + foreach (var technology in updateUserServiceModel.Technologies) { Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); - if (default(Technology) == returnedTechnology) + if (returnedTechnology == null) throw new ArgumentException($"Technology {technology.Name} does not exist!"); } } - private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) - { - throw new NotImplementedException(); - } - private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -344,25 +338,6 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private bool AddPatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - // Type type = typeof(UpdateUserServiceModel); - // PropertyInfo property = type.GetProperty(patch.Name); - - // property.SetValue(updateUserServiceModel, patch.Value); - throw new NotImplementedException(); - } - - private bool RemovePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } - - private bool ReplacePatch(UpdateUserServiceModel updateUserServiceModel, Patch patch) - { - throw new NotImplementedException(); - } #endregion } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From d2bc08c0dcd6f0dc0822333bbb00c9fc851f49cb Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 10:55:25 +0200 Subject: Brief testing of GetPost --- .../Interfaces/Repositories/IFeedRepository.cs | 12 +++++ src/DevHive.Data/Repositories/FeedRepository.cs | 35 ++++++++++++++ .../Configurations/Mapping/FeedMappings.cs | 11 +++++ .../Configurations/Mapping/PostMappings.cs | 1 + .../Configurations/Mapping/RoleMapings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 2 + src/DevHive.Services/Interfaces/IFeedService.cs | 10 ++++ src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- src/DevHive.Services/Interfaces/IUserService.cs | 2 + .../Models/Feed/GetPageServiceModel.cs | 15 ++++++ .../Models/Feed/ReadPageServiceModel.cs | 10 ++++ .../Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Services/Services/FeedService.cs | 47 +++++++++++++++++++ src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 53 +++++++++++++++++++--- .../Extensions/ConfigureDependencyInjection.cs | 2 + .../Configurations/Mapping/FeedMappings.cs | 18 ++++++++ .../Configurations/Mapping/RoleMappings.cs | 4 +- .../Configurations/Mapping/UserMappings.cs | 3 ++ src/DevHive.Web/Controllers/FeedController.cs | 36 +++++++++++++++ src/DevHive.Web/Controllers/PostController.cs | 2 +- src/DevHive.Web/Controllers/RoleController.cs | 2 +- src/DevHive.Web/Controllers/UserController.cs | 15 +++++- src/DevHive.Web/Models/Feed/GetPageWebModel.cs | 19 ++++++++ src/DevHive.Web/Models/Feed/ReadPageWebModel.cs | 10 ++++ 25 files changed, 303 insertions(+), 18 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs create mode 100644 src/DevHive.Data/Repositories/FeedRepository.cs create mode 100644 src/DevHive.Services/Configurations/Mapping/FeedMappings.cs create mode 100644 src/DevHive.Services/Interfaces/IFeedService.cs create mode 100644 src/DevHive.Services/Models/Feed/GetPageServiceModel.cs create mode 100644 src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs create mode 100644 src/DevHive.Services/Services/FeedService.cs create mode 100644 src/DevHive.Web/Configurations/Mapping/FeedMappings.cs create mode 100644 src/DevHive.Web/Controllers/FeedController.cs create mode 100644 src/DevHive.Web/Models/Feed/GetPageWebModel.cs create mode 100644 src/DevHive.Web/Models/Feed/ReadPageWebModel.cs (limited to 'src/DevHive.Web/Configurations/Mapping/RoleMappings.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs new file mode 100644 index 0000000..e9fd48a --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IFeedRepository.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface IFeedRepository + { + Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize); + } +} diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs new file mode 100644 index 0000000..8bf1f9a --- /dev/null +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper.Internal; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class FeedRepository : IFeedRepository + { + private readonly DevHiveContext _context; + + public FeedRepository(DevHiveContext context) + { + this._context = context; + } + public async Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize) + { + List friendsIds = friendsList.Select(f => f.Id).ToList(); + + List posts = await this._context.Posts + .Where(post => post.TimeCreated < firstRequestIssued) + .Where(p => friendsIds.Contains(p.CreatorId)) + .OrderByDescending(x => x.TimeCreated) + .Skip((pageNumber - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + + return posts; + } + } +} diff --git a/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs b/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs new file mode 100644 index 0000000..952e480 --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/FeedMappings.cs @@ -0,0 +1,11 @@ +using AutoMapper; + +namespace DevHive.Services.Configurations.Mapping +{ + public class FeedMappings : Profile + { + public FeedMappings() + { + } + } +} diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs index cea7b1c..d8dcc84 100644 --- a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -20,6 +20,7 @@ namespace DevHive.Services.Configurations.Mapping .ForMember(dest => dest.CreatorFirstName, src => src.Ignore()) .ForMember(dest => dest.CreatorLastName, src => src.Ignore()) .ForMember(dest => dest.CreatorUsername, src => src.Ignore()); + //TODO: Map those here /\ } } } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index e61a107..23bd46f 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 5d9e41c..6797ce1 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -14,6 +14,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap() .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); CreateMap(); + CreateMap() + .ForMember(dest => dest.UserName, src => src.MapFrom(p => p.Name)); CreateMap(); CreateMap() diff --git a/src/DevHive.Services/Interfaces/IFeedService.cs b/src/DevHive.Services/Interfaces/IFeedService.cs new file mode 100644 index 0000000..1edba5a --- /dev/null +++ b/src/DevHive.Services/Interfaces/IFeedService.cs @@ -0,0 +1,10 @@ +using System.Threading.Tasks; +using DevHive.Services.Models; + +namespace DevHive.Services.Interfaces +{ + public interface IFeedService + { + Task GetPage(GetPageServiceModel getPageServiceModel); + } +} diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index d47728c..d3a45e5 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/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 51e3cf9..9372517 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -18,5 +18,7 @@ namespace DevHive.Services.Interfaces Task DeleteUser(Guid id); Task ValidJWT(Guid id, string rawTokenData); + + Task SuperSecretPromotionToAdmin(Guid userId); } } diff --git a/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs b/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs new file mode 100644 index 0000000..745039f --- /dev/null +++ b/src/DevHive.Services/Models/Feed/GetPageServiceModel.cs @@ -0,0 +1,15 @@ +using System; + +namespace DevHive.Services.Models +{ + public class GetPageServiceModel + { + public Guid UserId { get; set; } + + public int PageNumber { get; set; } + + public DateTime FirstRequestIssued { get; set; } + + public int PageSize { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs new file mode 100644 index 0000000..f291de7 --- /dev/null +++ b/src/DevHive.Services/Models/Feed/ReadPageServiceModel.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Models +{ + public class ReadPageServiceModel + { + public List Posts { get; set; } = new(); + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 3aa0d44..3e41057 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -7,7 +7,7 @@ namespace DevHive.Services.Models.Identity.User { public class UserServiceModel : BaseUserServiceModel { - public HashSet Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); public HashSet Friends { get; set; } = new HashSet(); diff --git a/src/DevHive.Services/Services/FeedService.cs b/src/DevHive.Services/Services/FeedService.cs new file mode 100644 index 0000000..cae986f --- /dev/null +++ b/src/DevHive.Services/Services/FeedService.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Services +{ + public class FeedService : IFeedService + { + private readonly IMapper _mapper; + private readonly IFeedRepository _feedRepository; + private readonly IUserRepository _userRepository; + + public FeedService(IFeedRepository feedRepository, IUserRepository userRepository, IMapper mapper) + { + this._feedRepository = feedRepository; + this._userRepository = userRepository; + this._mapper = mapper; + } + + public async Task GetPage(GetPageServiceModel model) + { + User user = await this._userRepository.GetByIdAsync(model.UserId) ?? + throw new ArgumentException("User doesn't exist!"); + + List friendsList = user.Friends.ToList(); + // if(friendsList.Count == 0) + // throw new ArgumentException("This user does not have any friends!"); + + List posts = await this._feedRepository + .GetFriendsPosts(friendsList, model.FirstRequestIssued, model.PageNumber, model.PageSize) ?? + throw new ArgumentException("No posts to query."); + + ReadPageServiceModel readPageServiceModel = new(); + foreach (Post post in posts) + readPageServiceModel.Posts.Add(this._mapper.Map(post)); + + return readPageServiceModel; + } + } +} diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index a8b8e17..9f7a5ac 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 d7013e1..1beb07f 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,7 +111,7 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - //Preserve roles + /* Roles */ int roleCount = updateUserServiceModel.Roles.Count; for (int i = 0; i < roleCount; i++) { @@ -123,6 +123,7 @@ namespace DevHive.Services.Services updateUserServiceModel.Roles.Add(updateRoleServiceModel); } + /* Languages */ int langCount = updateUserServiceModel.Languages.Count; for (int i = 0; i < langCount; i++) { @@ -133,10 +134,10 @@ namespace DevHive.Services.Services 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++) { @@ -147,11 +148,25 @@ namespace DevHive.Services.Services 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); + User user = this._userMapper.Map(updateUserServiceModel); + user.Friends = friends; + bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); if (!successful) @@ -189,14 +204,14 @@ namespace DevHive.Services.Services /* Check if user is trying to do something to himself, unless he's an admin */ - if (!jwtRoleNames.Contains(Role.AdminRole)) - if (user.Id != id) - return false; - /* Check roles */ if (jwtRoleNames.Contains(Role.AdminRole)) return true; + if (!jwtRoleNames.Contains(Role.AdminRole)) + if (user.Id != id) + return false; + // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) foreach (var role in user.Roles) { @@ -290,5 +305,29 @@ namespace DevHive.Services.Services return tokenHandler.WriteToken(token); } #endregion + + 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")) + { + Role adminRole = new() + { + Name = Role.AdminRole + }; + adminRole.Users.Add(user); + + await this._roleRepository.AddAsync(adminRole); + } + + Role admin = await this._roleRepository.GetByNameAsync(Role.AdminRole); + + user.Roles.Add(admin); + await this._userRepository.EditAsync(user.Id, user); + + return admin.Id; + } } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index bcf16ac..d7c859e 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -17,12 +17,14 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs new file mode 100644 index 0000000..159582d --- /dev/null +++ b/src/DevHive.Web/Configurations/Mapping/FeedMappings.cs @@ -0,0 +1,18 @@ +using AutoMapper; +using DevHive.Services.Models; +using DevHive.Web.Controllers; +using DevHive.Web.Models.Feed; + +namespace DevHive.Web.Configurations.Mapping +{ + public class FeedMappings : Profile + { + public FeedMappings() + { + CreateMap() + .ForMember(dest => dest.FirstRequestIssued, src => src.MapFrom(p => p.FirstPageTimeIssued)); + + CreateMap(); + } + } +} diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 2ea2742..2f01f77 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 9dbf613..e80a69a 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -25,6 +25,9 @@ namespace DevHive.Web.Configurations.Mapping .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(); diff --git a/src/DevHive.Web/Controllers/FeedController.cs b/src/DevHive.Web/Controllers/FeedController.cs new file mode 100644 index 0000000..7d0269b --- /dev/null +++ b/src/DevHive.Web/Controllers/FeedController.cs @@ -0,0 +1,36 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Services.Interfaces; +using DevHive.Services.Models; +using DevHive.Web.Models.Feed; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + public class FeedController + { + private readonly IFeedService _feedService; + private readonly IMapper _mapper; + + public FeedController(IFeedService feedService, IMapper mapper) + { + this._feedService = feedService; + this._mapper = mapper; + } + + [HttpGet] + public async Task GetPosts(Guid userId, [FromBody] GetPageWebModel getPageWebModel) + { + GetPageServiceModel getPageServiceModel = this._mapper.Map(getPageWebModel); + getPageServiceModel.UserId = userId; + + ReadPageServiceModel readPageServiceModel = await this._feedService.GetPage(getPageServiceModel); + ReadPageWebModel readPageWebModel = this._mapper.Map(readPageServiceModel); + + return new OkObjectResult(readPageWebModel); + } + } +} diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs index b5e1c98..151c688 100644 --- a/src/DevHive.Web/Controllers/PostController.cs +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -13,7 +13,7 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - // [Authorize(Roles = "User")] + [Authorize(Roles = "User,Admin")] public class PostController { private readonly IPostService _postService; diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index c68a32b..d8bb60c 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) { - RoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); + ReadRoleServiceModel roleServiceModel = await this._roleService.GetRoleById(id); RoleWebModel roleWebModel = this._roleMapper.Map(roleServiceModel); return new OkObjectResult(roleWebModel); diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index dd94089..e409eea 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -12,7 +12,7 @@ namespace DevHive.Web.Controllers { [ApiController] [Route("/api/[controller]")] - [Authorize(Policy = "User")] + [Authorize(Roles = "User,Admin")] public class UserController : ControllerBase { private readonly IUserService _userService; @@ -104,5 +104,18 @@ namespace DevHive.Web.Controllers return new OkResult(); } #endregion + + [HttpPost] + [Route("SuperSecretPromotionToAdmin")] + public async Task SuperSecretPromotionToAdmin(Guid userId) + { + object obj = new + { + UserId = userId, + AdminRoleId = await this._userService.SuperSecretPromotionToAdmin(userId) + }; + + return new OkObjectResult(obj); + } } } diff --git a/src/DevHive.Web/Models/Feed/GetPageWebModel.cs b/src/DevHive.Web/Models/Feed/GetPageWebModel.cs new file mode 100644 index 0000000..4ea44cc --- /dev/null +++ b/src/DevHive.Web/Models/Feed/GetPageWebModel.cs @@ -0,0 +1,19 @@ +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace DevHive.Web.Models.Feed +{ + public class GetPageWebModel + { + [Range(1, int.MaxValue)] + public int PageNumber { get; set; } + + [Required] + public DateTime FirstPageTimeIssued { get; set; } + + [DefaultValue(5)] + [Range(1, int.MaxValue)] + public int PageSize { get; set; } + } +} diff --git a/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs new file mode 100644 index 0000000..40d29c9 --- /dev/null +++ b/src/DevHive.Web/Models/Feed/ReadPageWebModel.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using DevHive.Web.Models.Post.Post; + +namespace DevHive.Web.Controllers +{ + public class ReadPageWebModel + { + public List Posts { get; set; } = new(); + } +} -- 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/Configurations/Mapping/RoleMappings.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