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 ---------- 1 file changed, 10 deletions(-) delete mode 100644 src/DevHive.Common/Models/Identity/RoleModel.cs (limited to 'src/DevHive.Common/Models') 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; } - } -} -- cgit v1.2.3 From 33b5f6297c2c975bec8a74a8facc208261c03c9e Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 20:36:39 +0200 Subject: Replaced RepoMethods with BaseClass and inherited it where needed --- .../Models/Data/RepositoryMethods.cs | 15 -------------- src/DevHive.Data/Repositories/BaseRepository.cs | 15 ++++++++++++++ .../Repositories/LanguageRepository.cs | 8 ++++---- src/DevHive.Data/Repositories/PostRepository.cs | 14 ++++++------- src/DevHive.Data/Repositories/RoleRepository.cs | 8 ++++---- .../Repositories/TechnologyRepository.cs | 8 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 24 +++++++++++----------- 7 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 src/DevHive.Common/Models/Data/RepositoryMethods.cs create mode 100644 src/DevHive.Data/Repositories/BaseRepository.cs (limited to 'src/DevHive.Common/Models') diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs deleted file mode 100644 index bfd057f..0000000 --- a/src/DevHive.Common/Models/Data/RepositoryMethods.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Common.Models.Misc -{ - public static class RepositoryMethods - { - public static async Task SaveChangesAsync(DbContext context) - { - int result = await context.SaveChangesAsync(); - - return result >= 1; - } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs new file mode 100644 index 0000000..b0f0f3e --- /dev/null +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class BaseRepository + { + public async Task SaveChangesAsync(DbContext context) + { + int result = await context.SaveChangesAsync(); + + return result >= 1; + } + } +} diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index e644fc4..108b307 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : ILanguageRepository + public class LanguageRepository : BaseRepository, ILanguageRepository { private readonly DevHiveContext _context; @@ -24,7 +24,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -52,7 +52,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -64,7 +64,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index c5e8569..db2c4af 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : BaseRepository, IPostRepository { private readonly DevHiveContext _context; @@ -23,7 +23,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(post); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddCommentAsync(Comment entity) @@ -32,7 +32,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Read @@ -71,7 +71,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newPost); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditCommentAsync(Comment newEntity) @@ -80,7 +80,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Delete @@ -90,7 +90,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(post); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task DeleteCommentAsync(Comment entity) @@ -99,7 +99,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #region Validations diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index ca3fb8b..684fbd7 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRoleRepository + public class RoleRepository : BaseRepository, IRoleRepository { private readonly DevHiveContext _context; @@ -23,7 +23,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Read @@ -51,7 +51,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Delete @@ -61,7 +61,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task DoesNameExist(string name) diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 73827a7..390ad3f 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class TechnologyRepository : ITechnologyRepository + public class TechnologyRepository : BaseRepository, ITechnologyRepository { private readonly DevHiveContext _context; @@ -25,7 +25,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -52,7 +52,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -64,7 +64,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 6d4a0bf..81c974c 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IUserRepository + public class UserRepository : BaseRepository, IUserRepository { private readonly DevHiveContext _context; @@ -25,7 +25,7 @@ namespace DevHive.Data.Repositories await this._context.Users .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddFriendToUserAsync(User user, User friend) @@ -33,7 +33,7 @@ namespace DevHive.Data.Repositories this._context.Update(user); user.Friends.Add(friend); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddLanguageToUserAsync(User user, Language language) @@ -42,7 +42,7 @@ namespace DevHive.Data.Repositories user.Languages.Add(language); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddTechnologyToUserAsync(User user, Technology technology) @@ -51,7 +51,7 @@ namespace DevHive.Data.Repositories user.Technologies.Add(technology); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -116,7 +116,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditUserLanguageAsync(User user, Language oldLang, Language newLang) @@ -126,7 +126,7 @@ namespace DevHive.Data.Repositories user.Languages.Remove(oldLang); user.Languages.Add(newLang); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech) @@ -136,7 +136,7 @@ namespace DevHive.Data.Repositories user.Technologies.Remove(oldTech); user.Technologies.Add(newTech); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -147,7 +147,7 @@ namespace DevHive.Data.Repositories this._context.Users .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveFriendAsync(User user, User friend) @@ -155,7 +155,7 @@ namespace DevHive.Data.Repositories this._context.Update(user); user.Friends.Remove(friend); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveLanguageFromUserAsync(User user, Language language) @@ -164,7 +164,7 @@ namespace DevHive.Data.Repositories user.Languages.Remove(language); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) @@ -173,7 +173,7 @@ namespace DevHive.Data.Repositories user.Technologies.Remove(technology); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion -- cgit v1.2.3 From 009e01dc3dc2f78db6a660c65bf0d20bae702348 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 09:23:06 +0200 Subject: Tryed implementing the http patch --- .../Models/Misc/PasswordModifications.cs | 13 ++++ src/DevHive.Common/Models/Misc/Patch.cs | 9 +++ src/DevHive.Data/Repositories/UserRepository.cs | 7 -- .../Configurations/Mapping/RoleMapings.cs | 3 + .../Mapping/UserCollectionMappings.cs | 17 ++--- .../Configurations/Mapping/UserMappings.cs | 6 +- src/DevHive.Services/Interfaces/IUserService.cs | 6 +- .../Models/Identity/User/BaseUserServiceModel.cs | 1 + .../Technology/ReadTechnologyServiceModel.cs | 7 ++ src/DevHive.Services/Services/UserService.cs | 78 ++++++++++++---------- .../Configurations/Mapping/TechnologyMappings.cs | 2 + src/DevHive.Web/Controllers/UserController.cs | 19 ++---- .../Models/Technology/ReadTechnologyWebModel.cs | 14 ++++ 13 files changed, 113 insertions(+), 69 deletions(-) create mode 100644 src/DevHive.Common/Models/Misc/PasswordModifications.cs create mode 100644 src/DevHive.Common/Models/Misc/Patch.cs create mode 100644 src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs create mode 100644 src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs (limited to 'src/DevHive.Common/Models') diff --git a/src/DevHive.Common/Models/Misc/PasswordModifications.cs b/src/DevHive.Common/Models/Misc/PasswordModifications.cs new file mode 100644 index 0000000..f10a334 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/PasswordModifications.cs @@ -0,0 +1,13 @@ +using System.Security.Cryptography; +using System.Text; + +namespace DevHive.Common.Models.Misc +{ + public static class PasswordModifications + { + public static string GeneratePasswordHash(string password) + { + return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + } + } +} diff --git a/src/DevHive.Common/Models/Misc/Patch.cs b/src/DevHive.Common/Models/Misc/Patch.cs new file mode 100644 index 0000000..ea5a4f1 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/Patch.cs @@ -0,0 +1,9 @@ +namespace DevHive.Common.Models.Misc +{ + public class Patch + { + public string Name { get; set; } + public object Value { get; set; } + public string Action { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 492d46b..3f9af70 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -109,13 +109,6 @@ namespace DevHive.Data.Repositories public async Task EditAsync(User newEntity) { - // User user = await this.GetByIdAsync(newEntity.Id); - - // this._context - // .Entry(user) - // .CurrentValues - // .SetValues(newEntity); - this._context.Update(newEntity); return await this.SaveChangesAsync(this._context); diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 4ddd253..b5541f9 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -9,6 +9,9 @@ namespace DevHive.Services.Configurations.Mapping public RoleMappings() { CreateMap(); + CreateMap(); + + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs index ee505a2..7a773e8 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -8,14 +8,15 @@ namespace DevHive.Services.Configurations.Mapping { 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)); + CreateMap(); + 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 541e16e..5d9e41c 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Identity.User; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -10,10 +11,13 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password)); CreateMap(); CreateMap(); + CreateMap() + .ForMember(x => x.Password, opt => opt.Ignore()); CreateMap(); } } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index 121fec3..88be0c8 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -1,9 +1,9 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Common.Models.Identity; -using DevHive.Data.Models; +using DevHive.Common.Models.Misc; using DevHive.Services.Models.Identity.User; -using Microsoft.AspNetCore.JsonPatch; namespace DevHive.Services.Interfaces { @@ -18,7 +18,7 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, JsonPatchDocument jsonPatch); + Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); diff --git a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs index 514f82a..7a160f8 100644 --- a/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/BaseUserServiceModel.cs @@ -6,5 +6,6 @@ namespace DevHive.Services.Models.Identity.User public string Email { get; set; } public string FirstName { get; set; } public string LastName { get; set; } + public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs new file mode 100644 index 0000000..cbfdc7d --- /dev/null +++ b/src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Technology +{ + public class ReadTechnologyServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 51c4432..629b489 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -7,15 +7,14 @@ using System; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using Microsoft.IdentityModel.Tokens; -using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; -using Microsoft.AspNetCore.JsonPatch; using System.Linq; -using Newtonsoft.Json; +using DevHive.Common.Models.Misc; +using System.Reflection; namespace DevHive.Services.Services { @@ -52,7 +51,7 @@ namespace DevHive.Services.Services User user = await this._userRepository.GetByUsernameAsync(loginModel.UserName); - if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) + if (user.PasswordHash != PasswordModifications.GeneratePasswordHash(loginModel.Password)) throw new ArgumentException("Incorrect password!"); return new TokenModel(WriteJWTSecurityToken(user.Id, user.Roles)); @@ -67,7 +66,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); - user.PasswordHash = GeneratePasswordHash(registerModel.Password); + user.PasswordHash = PasswordModifications.GeneratePasswordHash(registerModel.Password); // Make sure the default role exists if (!await this._roleRepository.DoesNameExist(Role.DefaultRole)) @@ -135,6 +134,7 @@ 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!"); @@ -144,6 +144,7 @@ namespace DevHive.Services.Services 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) ?? @@ -159,51 +160,35 @@ namespace DevHive.Services.Services user.Languages = languages; user.Technologies = technologies; - bool success = await this._userRepository.EditAsync(user); + bool successful = await this._userRepository.EditAsync(user); - if (!success) + if (!successful) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; } - public async Task PatchUser(Guid id, JsonPatchDocument jsonPatch) + public async Task PatchUser(Guid id, List patchList) { User user = await this._userRepository.GetByIdAsync(id) ?? throw new ArgumentException("User does not exist!"); - object password = jsonPatch.Operations - .Where(x => x.path == "/password") - .Select(x => x.value) - .FirstOrDefault(); - - IEnumerable friends = jsonPatch.Operations - .Where(x => x.path == "/friends") - .Select(x => x.value); - - if(password != null) - { - string passwordHash = this.GeneratePasswordHash(password.ToString()); - user.PasswordHash = passwordHash; - } + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(user); - if (friends != null) + foreach (Patch patch in patchList) { - foreach (object friendObj in friends) + bool successful = patch.Action switch { - FriendServiceModel friendServiceModel = - JsonConvert.DeserializeObject(friendObj.ToString()); - - User amigo = await this._userRepository.GetByUsernameAsync(friendServiceModel.UserName) - ?? throw new ArgumentException($"User {friendServiceModel.UserName} does not exist!"); - - user.Friends.Add(amigo); - } + "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"); } - //Remove password and friends peace from the request patch before applying the rest - // jsonPatch.ApplyTo(user); - bool success = await this._userRepository.EditAsync(user); if (success) { @@ -326,6 +311,11 @@ namespace DevHive.Services.Services } } + private async Task ValidateUserOnUpdate(UpdateUserServiceModel updateUserServiceModel) + { + + } + private string WriteJWTSecurityToken(Guid userId, HashSet roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -354,9 +344,23 @@ namespace DevHive.Services.Services return tokenHandler.WriteToken(token); } - private string GeneratePasswordHash(string password) + 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) { - return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); + throw new NotImplementedException(); } #endregion } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 828dac1..4ecd5f3 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -9,10 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public TechnologyMappings() { CreateMap(); + CreateMap(); CreateMap(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); CreateMap(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 471d2bb..7121ac8 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -6,14 +6,10 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Misc; -using DevHive.Web.Models.Language; -using DevHive.Services.Models.Language; -using DevHive.Web.Models.Technology; -using DevHive.Services.Models.Technology; using DevHive.Services.Interfaces; -using DevHive.Data.Models; using Microsoft.AspNetCore.JsonPatch; +using DevHive.Common.Models.Misc; +using System.Collections.Generic; namespace DevHive.Web.Controllers { @@ -87,15 +83,12 @@ namespace DevHive.Web.Controllers #region Update [HttpPut] - public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization) + public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateUserWebModel, [FromHeader] string authorization) { if (!await this._userService.ValidJWT(id, authorization)) return new UnauthorizedResult(); - // if (!ModelState.IsValid) - // return BadRequest("Not a valid model!"); - - UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateModel); + UpdateUserServiceModel updateUserServiceModel = this._userMapper.Map(updateUserWebModel); updateUserServiceModel.Id = id; UserServiceModel userServiceModel = await this._userService.UpdateUser(updateUserServiceModel); @@ -105,12 +98,12 @@ namespace DevHive.Web.Controllers } [HttpPatch] - public async Task Patch(Guid id, [FromBody] JsonPatchDocument jsonPatch, [FromHeader] string authorization) + 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, jsonPatch); + UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); if (userServiceModel == null) return new BadRequestObjectResult("Wrong patch properties"); diff --git a/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs new file mode 100644 index 0000000..edaaaef --- /dev/null +++ b/src/DevHive.Web/Models/Technology/ReadTechnologyWebModel.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.CodeAnalysis; + +namespace DevHive.Web.Models.Technology +{ + public class ReadTechnologyWebModel + { + [NotNull] + [Required] + [MinLength(3)] + [MaxLength(50)] + public string Name { get; set; } + } +} -- cgit v1.2.3