aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-21 19:12:04 +0200
committertranstrike <transtrike@gmail.com>2021-01-21 19:12:04 +0200
commit9e86699c9b3aff17e0c4d19850b41b792a9625ef (patch)
tree8504470031e886e9defcb6399fc7365bab17ddff /src/DevHive.Services
parent1001c3d5c6f979c56daf98e7ed82cee2ff09ab7f (diff)
downloadDevHive-9e86699c9b3aff17e0c4d19850b41b792a9625ef.tar
DevHive-9e86699c9b3aff17e0c4d19850b41b792a9625ef.tar.gz
DevHive-9e86699c9b3aff17e0c4d19850b41b792a9625ef.zip
Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/RoleMapings.cs2
-rw-r--r--src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs2
-rw-r--r--src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs22
-rw-r--r--src/DevHive.Services/Interfaces/IUserService.cs1
-rw-r--r--src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs14
-rw-r--r--src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs3
-rw-r--r--src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs (renamed from src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs)5
-rw-r--r--src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs11
-rw-r--r--src/DevHive.Services/Services/UserService.cs135
9 files changed, 86 insertions, 109 deletions
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<UpdateRoleServiceModel, Role>();
CreateMap<Role, RoleServiceModel>();
- CreateMap<Role, RoleServiceModel>();
+ CreateMap<Role, UpdateRoleServiceModel>();
}
}
}
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<CreateTechnologyServiceModel, Technology>();
CreateMap<UpdateTechnologyServiceModel, Technology>();
CreateMap<TechnologyServiceModel, Technology>();
+
CreateMap<Technology, CreateTechnologyServiceModel>();
CreateMap<Technology, TechnologyServiceModel>();
+ CreateMap<Technology, UpdateTechnologyServiceModel>();
}
}
}
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<UpdateUserCollectionServiceModel, User>();
- CreateMap<UpdateUserCollectionServiceModel, Role>();
- CreateMap<UpdateUserCollectionServiceModel, Language>();
- CreateMap<UpdateUserCollectionServiceModel, Technology>();
-
- CreateMap<User, UpdateUserCollectionServiceModel>();
- CreateMap<Role, UpdateUserCollectionServiceModel>();
- CreateMap<Language, UpdateUserCollectionServiceModel>();
- CreateMap<Technology, UpdateUserCollectionServiceModel>();
- }
- }
-}
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<UserServiceModel> GetUserById(Guid id);
Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel);
- Task<UserServiceModel> PatchUser(Guid id, List<Patch> patch);
Task DeleteUser(Guid id);
Task<bool> 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/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs
index c40a980..83fcc34 100644
--- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs
@@ -1,7 +1,10 @@
+using System;
+
namespace DevHive.Services.Models.Identity.User
{
- public class UpdateUserCollectionServiceModel
+ public class UpdateFriendServiceModel
{
+ public Guid Id { get; set; }
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<UpdateUserCollectionServiceModel> Roles { get; set; } = new HashSet<UpdateUserCollectionServiceModel>();
+ public HashSet<UpdateRoleServiceModel> Roles { get; set; } = new HashSet<UpdateRoleServiceModel>();
- public HashSet<UpdateUserCollectionServiceModel> Friends { get; set; } = new HashSet<UpdateUserCollectionServiceModel>();
+ public HashSet<UpdateFriendServiceModel> Friends { get; set; } = new HashSet<UpdateFriendServiceModel>();
- public HashSet<UpdateUserCollectionServiceModel> Languages { get; set; } = new HashSet<UpdateUserCollectionServiceModel>();
+ public HashSet<UpdateLanguageServiceModel> Languages { get; set; } = new HashSet<UpdateLanguageServiceModel>();
- public HashSet<UpdateUserCollectionServiceModel> Technologies { get; set; } = new HashSet<UpdateUserCollectionServiceModel>();
+ public HashSet<UpdateTechnologyServiceModel> Technologies { get; set; } = new HashSet<UpdateTechnologyServiceModel>();
}
}
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<UserServiceModel> 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<Language> 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<Technology> 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<UpdateRoleServiceModel>(role);
- User user = this._userMapper.Map<User>(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<UpdateLanguageServiceModel>(language);
- if (!successful)
- throw new InvalidOperationException("Unable to edit user!");
+ updateUserServiceModel.Languages.Add(updateLanguageServiceModel);
+ }
- return this._userMapper.Map<UserServiceModel>(user); ;
- }
+ //Clean the already replaced languages
+ updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty);
- public async Task<UserServiceModel> PatchUser(Guid id, List<Patch> 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<UpdateUserServiceModel>(user);
+ UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map<UpdateTechnologyServiceModel>(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<UserServiceModel>(user);
- }
- else
- return null;
+ //Clean the already replaced technologies
+ updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty);
+
+ User user = this._userMapper.Map<User>(updateUserServiceModel);
+ bool successful = await this._userRepository.EditAsync(user);
+
+ if (!successful)
+ throw new InvalidOperationException("Unable to edit user!");
+
+ return this._userMapper.Map<UserServiceModel>(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<Role> 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
}
}