aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Services
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-21 22:13:16 +0200
committertranstrike <transtrike@gmail.com>2021-01-21 22:13:16 +0200
commit13a2ceda912f961a232c87236f1b29aa29bb6160 (patch)
tree59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Services
parenta47ea20ab91017da53437f750ed8e0f939f5cdba (diff)
parentbda98b96433d7a9952524fab4ec65f96998b55de (diff)
downloadDevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar
DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.tar.gz
DevHive-13a2ceda912f961a232c87236f1b29aa29bb6160.zip
Merge branch 'refactor_user_updating' into dev
Diffstat (limited to 'src/DevHive.Services')
-rw-r--r--src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs4
-rw-r--r--src/DevHive.Services/Configurations/Mapping/RoleMapings.cs11
-rw-r--r--src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs2
-rw-r--r--src/DevHive.Services/Configurations/Mapping/UserMappings.cs8
-rw-r--r--src/DevHive.Services/Interfaces/ILanguageService.cs8
-rw-r--r--src/DevHive.Services/Interfaces/IPostService.cs4
-rw-r--r--src/DevHive.Services/Interfaces/IRoleService.cs10
-rw-r--r--src/DevHive.Services/Interfaces/ITechnologyService.cs4
-rw-r--r--src/DevHive.Services/Interfaces/IUserService.cs11
-rw-r--r--src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs14
-rw-r--r--src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs7
-rw-r--r--src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs10
-rw-r--r--src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs10
-rw-r--r--src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs6
-rw-r--r--src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs10
-rw-r--r--src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs17
-rw-r--r--src/DevHive.Services/Models/Identity/User/UserServiceModel.cs15
-rw-r--r--src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs7
-rw-r--r--src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs3
-rw-r--r--src/DevHive.Services/Models/Technology/ReadTechnologyServiceModel.cs7
-rw-r--r--src/DevHive.Services/Services/LanguageService.cs33
-rw-r--r--src/DevHive.Services/Services/PostService.cs29
-rw-r--r--src/DevHive.Services/Services/RoleService.cs30
-rw-r--r--src/DevHive.Services/Services/TechnologyService.cs32
-rw-r--r--src/DevHive.Services/Services/UserService.cs253
25 files changed, 305 insertions, 240 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs
index e483fff..9c572df 100644
--- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs
@@ -9,12 +9,14 @@ namespace DevHive.Services.Configurations.Mapping
public LanguageMappings()
{
CreateMap<LanguageServiceModel, Language>();
+ CreateMap<ReadLanguageServiceModel, Language>();
CreateMap<CreateLanguageServiceModel, Language>();
CreateMap<UpdateLanguageServiceModel, Language>();
CreateMap<Language, LanguageServiceModel>();
+ CreateMap<Language, ReadLanguageServiceModel>();
CreateMap<Language, CreateLanguageServiceModel>();
CreateMap<Language, UpdateLanguageServiceModel>();
}
}
-} \ No newline at end of file
+}
diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
index 65b0b5a..d6c8511 100644
--- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
+++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs
@@ -1,15 +1,18 @@
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<RoleModel, Role>();
- CreateMap<Role, RoleModel>();
+ CreateMap<RoleServiceModel, Role>();
+ CreateMap<UpdateRoleServiceModel, Role>();
+
+ 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/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs
index d57c6ba..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,9 +11,14 @@ namespace DevHive.Services.Configurations.Mapping
{
CreateMap<UserServiceModel, User>();
CreateMap<RegisterServiceModel, User>();
- CreateMap<UpdateUserServiceModel, User>();
+ CreateMap<UpdateUserServiceModel, User>()
+ .AfterMap((src, dest) => dest.PasswordHash = PasswordModifications.GeneratePasswordHash(src.Password));
+ CreateMap<FriendServiceModel, User>();
CreateMap<User, UserServiceModel>();
+ CreateMap<User, UpdateUserServiceModel>()
+ .ForMember(x => x.Password, opt => opt.Ignore());
+ CreateMap<User, FriendServiceModel>();
}
}
}
diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs
index eb45a8d..0df9a95 100644
--- a/src/DevHive.Services/Interfaces/ILanguageService.cs
+++ b/src/DevHive.Services/Interfaces/ILanguageService.cs
@@ -6,12 +6,12 @@ namespace DevHive.Services.Interfaces
{
public interface ILanguageService
{
- Task<bool> CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel);
+ Task<Guid> CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel);
- Task<LanguageServiceModel> GetLanguageById(Guid languageId);
+ Task<ReadLanguageServiceModel> GetLanguageById(Guid id);
- Task<bool> UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel);
+ Task<bool> UpdateLanguage(UpdateLanguageServiceModel languageServiceModel);
- Task<bool> DeleteLanguage(Guid languageId);
+ Task<bool> DeleteLanguage(Guid id);
}
}
diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs
index dd886b4..4364c67 100644
--- a/src/DevHive.Services/Interfaces/IPostService.cs
+++ b/src/DevHive.Services/Interfaces/IPostService.cs
@@ -7,8 +7,8 @@ namespace DevHive.Services.Interfaces
{
public interface IPostService
{
- Task<bool> CreatePost(CreatePostServiceModel postServiceModel);
- Task<bool> AddComment(CreateCommentServiceModel commentServiceModel);
+ Task<Guid> CreatePost(CreatePostServiceModel postServiceModel);
+ Task<Guid> AddComment(CreateCommentServiceModel commentServiceModel);
Task<CommentServiceModel> GetCommentById(Guid id);
Task<PostServiceModel> GetPostById(Guid id);
diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs
index 2c7195c..fd661be 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<bool> CreateRole(RoleModel roleServiceModel);
+ Task<Guid> CreateRole(RoleServiceModel roleServiceModel);
- Task<RoleModel> GetRoleById(Guid id);
+ Task<RoleServiceModel> GetRoleById(Guid id);
- Task<bool> UpdateRole(RoleModel roleServiceModel);
+ Task<bool> UpdateRole(UpdateRoleServiceModel roleServiceModel);
Task<bool> DeleteRole(Guid id);
}
diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs
index 0797078..9c5661d 100644
--- a/src/DevHive.Services/Interfaces/ITechnologyService.cs
+++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs
@@ -6,11 +6,11 @@ namespace DevHive.Services.Interfaces
{
public interface ITechnologyService
{
- Task<bool> Create(CreateTechnologyServiceModel technologyServiceModel);
+ Task<Guid> Create(CreateTechnologyServiceModel technologyServiceModel);
Task<CreateTechnologyServiceModel> GetTechnologyById(Guid id);
- Task<bool> UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel);
+ Task<bool> UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel);
Task<bool> DeleteTechnology(Guid id);
}
diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs
index 5ef141f..51e3cf9 100644
--- a/src/DevHive.Services/Interfaces/IUserService.cs
+++ b/src/DevHive.Services/Interfaces/IUserService.cs
@@ -2,8 +2,6 @@ using System;
using System.Threading.Tasks;
using DevHive.Common.Models.Identity;
using DevHive.Services.Models.Identity.User;
-using DevHive.Services.Models.Language;
-using DevHive.Services.Models.Technology;
namespace DevHive.Services.Interfaces
{
@@ -12,19 +10,12 @@ namespace DevHive.Services.Interfaces
Task<TokenModel> LoginUser(LoginServiceModel loginModel);
Task<TokenModel> RegisterUser(RegisterServiceModel registerModel);
- Task<bool> AddFriend(Guid userId, Guid friendId);
- Task<bool> AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel);
- Task<bool> AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel);
-
- Task<UserServiceModel> GetFriendById(Guid friendId);
+ Task<UserServiceModel> GetUserByUsername(string username);
Task<UserServiceModel> GetUserById(Guid id);
Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel);
Task DeleteUser(Guid id);
- Task<bool> RemoveFriend(Guid userId, Guid friendId);
- Task<bool> RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel);
- Task<bool> RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel);
Task<bool> ValidJWT(Guid id, string rawTokenData);
}
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/Role/RoleServiceModel.cs b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs
new file mode 100644
index 0000000..07249fe
--- /dev/null
+++ b/src/DevHive.Services/Models/Identity/Role/RoleServiceModel.cs
@@ -0,0 +1,7 @@
+namespace DevHive.Services.Models.Identity.Role
+{
+ public class RoleServiceModel
+ {
+ 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..e21e6b4
--- /dev/null
+++ b/src/DevHive.Services/Models/Identity/Role/UpdateRoleServiceModel.cs
@@ -0,0 +1,10 @@
+using System;
+
+namespace DevHive.Services.Models.Identity.Role
+{
+ public class UpdateRoleServiceModel
+ {
+ public Guid Id { get; set; }
+ 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
new file mode 100644
index 0000000..a784f5c
--- /dev/null
+++ b/src/DevHive.Services/Models/Identity/User/FriendServiceModel.cs
@@ -0,0 +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/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs
index 74f66b4..adc4119 100644
--- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs
@@ -6,8 +6,10 @@ namespace DevHive.Services.Models.Identity.User
{
public class RegisterServiceModel : BaseUserServiceModel
{
- public IList<LanguageServiceModel> Languages { get; set; }
- public IList<TechnologyServiceModel> Technologies { get; set; }
public string Password { get; set; }
+
+ public HashSet<LanguageServiceModel> Languages { get; set; }
+
+ public HashSet<TechnologyServiceModel> Technologies { 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/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs
index 96d1ff0..9277e3e 100644
--- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs
@@ -1,9 +1,24 @@
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
{
- public class UpdateUserServiceModel : UserServiceModel
+ public class UpdateUserServiceModel : BaseUserServiceModel
{
public Guid Id { get; set; }
+
+ public string Password { get; set; }
+
+ public HashSet<UpdateRoleServiceModel> Roles { get; set; } = new HashSet<UpdateRoleServiceModel>();
+
+ public HashSet<UpdateFriendServiceModel> Friends { get; set; } = new HashSet<UpdateFriendServiceModel>();
+
+ public HashSet<UpdateLanguageServiceModel> Languages { get; set; } = new HashSet<UpdateLanguageServiceModel>();
+
+ public HashSet<UpdateTechnologyServiceModel> Technologies { get; set; } = new HashSet<UpdateTechnologyServiceModel>();
+
}
}
diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
index 8825f50..5fcd494 100644
--- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
+++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs
@@ -1,15 +1,18 @@
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<RoleModel> Roles { get; set; } = new List<RoleModel>();
- public IList<UserServiceModel> Friends { get; set; } = new List<UserServiceModel>();
- public IList<LanguageServiceModel> Languages { get; set; } = new List<LanguageServiceModel>();
- public IList<TechnologyServiceModel> TechnologyServiceModels { get; set; } = new List<TechnologyServiceModel>();
+ public HashSet<RoleServiceModel> Roles { get; set; } = new HashSet<RoleServiceModel>();
+
+ public HashSet<FriendServiceModel> Friends { get; set; } = new HashSet<FriendServiceModel>();
+
+ public HashSet<LanguageServiceModel> Languages { get; set; } = new HashSet<LanguageServiceModel>();
+
+ public HashSet<TechnologyServiceModel> Technologies { get; set; } = new HashSet<TechnologyServiceModel>();
}
}
diff --git a/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs
new file mode 100644
index 0000000..653444e
--- /dev/null
+++ b/src/DevHive.Services/Models/Language/ReadLanguageServiceModel.cs
@@ -0,0 +1,7 @@
+namespace DevHive.Services.Models.Language
+{
+ public class ReadLanguageServiceModel
+ {
+ public string Name { get; set; }
+ }
+}
diff --git a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs
index 3aa92ee..54d6838 100644
--- a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs
+++ b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs
@@ -5,7 +5,8 @@ namespace DevHive.Services.Models.Post.Comment
public class BaseCommentServiceModel
{
public Guid Id { get; set; }
+ public Guid PostId { get; set; }
public Guid IssuerId { get; set; }
public string Message { get; set; }
}
-} \ No newline at end of file
+}
diff --git a/src/DevHive.Services/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/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs
index be035c2..89df654 100644
--- a/src/DevHive.Services/Services/LanguageService.cs
+++ b/src/DevHive.Services/Services/LanguageService.cs
@@ -21,45 +21,50 @@ namespace DevHive.Services.Services
#region Create
- public async Task<bool> CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel)
+ public async Task<Guid> CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel)
{
if (await this._languageRepository.DoesLanguageNameExistAsync(createLanguageServiceModel.Name))
throw new ArgumentException("Language already exists!");
Language language = this._languageMapper.Map<Language>(createLanguageServiceModel);
- bool result = await this._languageRepository.AddAsync(language);
-
- return result;
+ bool success = await this._languageRepository.AddAsync(language);
+
+ if (success)
+ {
+ Language newLanguage = await this._languageRepository.GetByNameAsync(createLanguageServiceModel.Name);
+ return newLanguage.Id;
+ }
+ else
+ return Guid.Empty;
}
#endregion
#region Read
- public async Task<LanguageServiceModel> GetLanguageById(Guid languageId)
+ public async Task<ReadLanguageServiceModel> GetLanguageById(Guid id)
{
- Language language = await this._languageRepository.GetByIdAsync(languageId);
+ Language language = await this._languageRepository.GetByIdAsync(id);
if (language == null)
throw new ArgumentException("The language does not exist");
- return this._languageMapper.Map<LanguageServiceModel>(language);
+ return this._languageMapper.Map<ReadLanguageServiceModel>(language);
}
#endregion
#region Update
- public async Task<bool> UpdateLanguage(Guid languageId, UpdateLanguageServiceModel languageServiceModel)
+ public async Task<bool> UpdateLanguage(UpdateLanguageServiceModel languageServiceModel)
{
- bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageId);
+ bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id);
bool newLangNameExists = await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name);
if (!langExists)
throw new ArgumentException("Language does not exist!");
if (newLangNameExists)
- throw new ArgumentException("This name is already in our datbase!");
+ throw new ArgumentException("Language name already exists in our data base!");
- languageServiceModel.Id = languageId;
Language lang = this._languageMapper.Map<Language>(languageServiceModel);
return await this._languageRepository.EditAsync(lang);
}
@@ -67,12 +72,12 @@ namespace DevHive.Services.Services
#region Delete
- public async Task<bool> DeleteLanguage(Guid languageId)
+ public async Task<bool> DeleteLanguage(Guid id)
{
- if (!await this._languageRepository.DoesLanguageExistAsync(languageId))
+ if (!await this._languageRepository.DoesLanguageExistAsync(id))
throw new ArgumentException("Language does not exist!");
- Language language = await this._languageRepository.GetByIdAsync(languageId);
+ Language language = await this._languageRepository.GetByIdAsync(id);
return await this._languageRepository.DeleteAsync(language);
}
#endregion
diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs
index 6e83ad4..9503b8a 100644
--- a/src/DevHive.Services/Services/PostService.cs
+++ b/src/DevHive.Services/Services/PostService.cs
@@ -9,6 +9,7 @@ using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using DevHive.Services.Interfaces;
using DevHive.Data.Interfaces.Repositories;
+using System.Linq;
namespace DevHive.Services.Services
{
@@ -26,21 +27,35 @@ namespace DevHive.Services.Services
}
//Create
- public async Task<bool> CreatePost(CreatePostServiceModel postServiceModel)
+ public async Task<Guid> CreatePost(CreatePostServiceModel postServiceModel)
{
Post post = this._postMapper.Map<Post>(postServiceModel);
- return await this._postRepository.AddAsync(post);
+ bool success = await this._postRepository.AddAsync(post);
+
+ if(success)
+ {
+ Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated);
+ return newPost.Id;
+ }
+ else
+ return Guid.Empty;
}
- public async Task<bool> AddComment(CreateCommentServiceModel commentServiceModel)
+ public async Task<Guid> AddComment(CreateCommentServiceModel commentServiceModel)
{
commentServiceModel.TimeCreated = DateTime.Now;
Comment comment = this._postMapper.Map<Comment>(commentServiceModel);
- bool result = await this._postRepository.AddCommentAsync(comment);
+ bool success = await this._postRepository.AddCommentAsync(comment);
- return result;
+ if(success)
+ {
+ Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated);
+ return newComment.Id;
+ }
+ else
+ return Guid.Empty;
}
//Read
@@ -117,8 +132,8 @@ namespace DevHive.Services.Services
{
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
- string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0];
- //List<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
+ string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims).First();
+ //HashSet<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
User user = await this._userRepository.GetByUsernameAsync(jwtUserName)
?? throw new ArgumentException("User does not exist!");
diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs
index c38ac74..3ebb7c8 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,33 +20,42 @@ namespace DevHive.Services.Services
this._roleMapper = mapper;
}
- public async Task<bool> CreateRole(RoleModel roleServiceModel)
+ public async Task<Guid> CreateRole(RoleServiceModel roleServiceModel)
{
if (await this._roleRepository.DoesNameExist(roleServiceModel.Name))
throw new ArgumentException("Role already exists!");
+
Role role = this._roleMapper.Map<Role>(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<RoleModel> GetRoleById(Guid id)
+ public async Task<RoleServiceModel> GetRoleById(Guid id)
{
Role role = await this._roleRepository.GetByIdAsync(id)
?? throw new ArgumentException("Role does not exist!");
- return this._roleMapper.Map<RoleModel>(role);
+ return this._roleMapper.Map<RoleServiceModel>(role);
}
- public async Task<bool> UpdateRole(RoleModel roleServiceModel)
+ public async Task<bool> UpdateRole(UpdateRoleServiceModel updateRoleServiceModel)
{
- if (!await this._roleRepository.DoesRoleExist(roleServiceModel.Id))
+ if (!await this._roleRepository.DoesRoleExist(updateRoleServiceModel.Id))
throw new ArgumentException("Role does not exist!");
- if (await this._roleRepository.DoesNameExist(roleServiceModel.Name))
+ if (await this._roleRepository.DoesNameExist(updateRoleServiceModel.Name))
throw new ArgumentException("Role name already exists!");
- Role role = this._roleMapper.Map<Role>(roleServiceModel);
+ Role role = this._roleMapper.Map<Role>(updateRoleServiceModel);
return await this._roleRepository.EditAsync(role);
}
diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs
index d8b7262..88ed702 100644
--- a/src/DevHive.Services/Services/TechnologyService.cs
+++ b/src/DevHive.Services/Services/TechnologyService.cs
@@ -20,24 +20,28 @@ namespace DevHive.Services.Services
}
#region Create
-
- public async Task<bool> Create(CreateTechnologyServiceModel technologyServiceModel)
+ public async Task<Guid> Create(CreateTechnologyServiceModel technologyServiceModel)
{
if (await this._technologyRepository.DoesTechnologyNameExistAsync(technologyServiceModel.Name))
throw new ArgumentException("Technology already exists!");
Technology technology = this._technologyMapper.Map<Technology>(technologyServiceModel);
- bool result = await this._technologyRepository.AddAsync(technology);
-
- return result;
+ bool success = await this._technologyRepository.AddAsync(technology);
+
+ if (success)
+ {
+ Technology newTechnology = await this._technologyRepository.GetByNameAsync(technologyServiceModel.Name);
+ return newTechnology.Id;
+ }
+ else
+ return Guid.Empty;
}
#endregion
#region Read
-
- public async Task<CreateTechnologyServiceModel> GetTechnologyById(Guid technologyId)
+ public async Task<CreateTechnologyServiceModel> GetTechnologyById(Guid id)
{
- Technology technology = await this._technologyRepository.GetByIdAsync(technologyId);
+ Technology technology = await this._technologyRepository.GetByIdAsync(id);
if (technology == null)
throw new ArgumentException("The technology does not exist");
@@ -47,16 +51,14 @@ namespace DevHive.Services.Services
#endregion
#region Update
-
- public async Task<bool> UpdateTechnology(Guid technologyId, UpdateTechnologyServiceModel updateTechnologyServiceModel)
+ public async Task<bool> UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel)
{
- if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId))
+ if (!await this._technologyRepository.DoesTechnologyExistAsync(updateTechnologyServiceModel.Id))
throw new ArgumentException("Technology does not exist!");
if (await this._technologyRepository.DoesTechnologyNameExistAsync(updateTechnologyServiceModel.Name))
throw new ArgumentException("Technology name already exists!");
- updateTechnologyServiceModel.Id = technologyId;
Technology technology = this._technologyMapper.Map<Technology>(updateTechnologyServiceModel);
bool result = await this._technologyRepository.EditAsync(technology);
@@ -65,12 +67,12 @@ namespace DevHive.Services.Services
#endregion
#region Delete
- public async Task<bool> DeleteTechnology(Guid technologyId)
+ public async Task<bool> DeleteTechnology(Guid id)
{
- if (!await this._technologyRepository.DoesTechnologyExistAsync(technologyId))
+ if (!await this._technologyRepository.DoesTechnologyExistAsync(id))
throw new ArgumentException("Technology does not exist!");
- Technology technology = await this._technologyRepository.GetByIdAsync(technologyId);
+ Technology technology = await this._technologyRepository.GetByIdAsync(id);
bool result = await this._technologyRepository.DeleteAsync(technology);
return result;
diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs
index 37dbc7b..217154e 100644
--- a/src/DevHive.Services/Services/UserService.cs
+++ b/src/DevHive.Services/Services/UserService.cs
@@ -7,15 +7,16 @@ 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.Models.Language;
using DevHive.Services.Interfaces;
-using DevHive.Services.Models.Technology;
-using DevHive.Data.Repositories;
using DevHive.Data.Interfaces.Repositories;
+using System.Linq;
+using DevHive.Common.Models.Misc;
+using DevHive.Services.Models.Language;
+using DevHive.Services.Models.Technology;
+using DevHive.Services.Models.Identity.Role;
namespace DevHive.Services.Services
{
@@ -52,7 +53,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 +68,7 @@ namespace DevHive.Services.Services
throw new ArgumentException("Email already exists!");
User user = this._userMapper.Map<User>(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))
@@ -75,7 +76,7 @@ namespace DevHive.Services.Services
// Set the default role to the user
Role defaultRole = await this._roleRepository.GetByNameAsync(Role.DefaultRole);
- user.Roles = new List<Role>() { defaultRole };
+ user.Roles = new HashSet<Role>() { defaultRole };
await this._userRepository.AddAsync(user);
@@ -83,117 +84,84 @@ namespace DevHive.Services.Services
}
#endregion
- #region Create
-
- public async Task<bool> AddFriend(Guid userId, Guid friendId)
+ #region Read
+ public async Task<UserServiceModel> GetUserById(Guid id)
{
- Task<bool> userExists = this._userRepository.DoesUserExistAsync(userId);
- Task<bool> friendExists = this._userRepository.DoesUserExistAsync(friendId);
-
- await Task.WhenAll(userExists, friendExists);
-
- if (!userExists.Result)
- throw new ArgumentException("User doesn't exist!");
-
- if (!friendExists.Result)
- throw new ArgumentException("Friend doesn't exist!");
-
- if (await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId))
- throw new ArgumentException("Friend already exists in your friends list.");
-
- User user = await this._userRepository.GetByIdAsync(userId);
- User friend = await this._userRepository.GetByIdAsync(friendId);
+ User user = await this._userRepository.GetByIdAsync(id)
+ ?? throw new ArgumentException("User does not exist!");
- return user != default(User) && friend != default(User) ?
- await this._userRepository.AddFriendToUserAsync(user, friend) :
- throw new ArgumentException("Invalid user!");
+ return this._userMapper.Map<UserServiceModel>(user);
}
- public async Task<bool> AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel)
+ public async Task<UserServiceModel> GetUserByUsername(string username)
{
- bool userExists = await this._userRepository.DoesUserExistAsync(userId);
- bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id);
+ User friend = await this._userRepository.GetByUsernameAsync(username);
- if (!userExists)
+ if (friend == null)
throw new ArgumentException("User does not exist!");
- if (!languageExists)
- throw new ArgumentException("Language does noy exist!");
-
- User user = await this._userRepository.GetByIdAsync(userId);
- Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id);
-
- if (this._userRepository.DoesUserHaveThisLanguage(user, language))
- throw new ArgumentException("User already has this language!");
-
- return await this._userRepository.AddLanguageToUserAsync(user, language);
+ return this._userMapper.Map<UserServiceModel>(friend);
}
+ #endregion
- public async Task<bool> AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel)
+ #region Update
+ public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateUserServiceModel)
{
- bool userExists = await this._userRepository.DoesUserExistAsync(userId);
- bool technologyExists = await this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id);
-
- if (!userExists)
- throw new ArgumentException("User does not exist!");
+ await this.ValidateUserOnUpdate(updateUserServiceModel);
- if (!technologyExists)
- throw new ArgumentException("Technology does not exist!");
+ await this.ValidateUserCollections(updateUserServiceModel);
- Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id);
- User user = await this._userRepository.GetByIdAsync(userId);
+ //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!");
- if (this._userRepository.DoesUserHaveThisTechnology(user, technology))
- throw new ArgumentException("User already has this language!");
+ UpdateRoleServiceModel updateRoleServiceModel = this._userMapper.Map<UpdateRoleServiceModel>(role);
- return await this._userRepository.AddTechnologyToUserAsync(user, technology);
- }
- #endregion
-
- #region Read
+ updateUserServiceModel.Roles.Add(updateRoleServiceModel);
+ }
- public async Task<UserServiceModel> GetUserById(Guid id)
- {
- User user = await this._userRepository.GetByIdAsync(id)
- ?? throw new ArgumentException("User does not exist!");
+ 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!");
- return this._userMapper.Map<UserServiceModel>(user);
- }
+ UpdateLanguageServiceModel updateLanguageServiceModel = this._userMapper.Map<UpdateLanguageServiceModel>(language);
- public async Task<UserServiceModel> GetFriendById(Guid friendId)
- {
- if (!await _userRepository.DoesUserExistAsync(friendId))
- throw new ArgumentException("User does not exist!");
+ updateUserServiceModel.Languages.Add(updateLanguageServiceModel);
+ }
- User friend = await this._userRepository.GetByIdAsync(friendId);
+ //Clean the already replaced languages
+ updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty);
- return this._userMapper.Map<UserServiceModel>(friend);
- }
- #endregion
+ 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!");
- #region Update
+ UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map<UpdateTechnologyServiceModel>(technology);
- public async Task<UserServiceModel> UpdateUser(UpdateUserServiceModel updateModel)
- {
- if (!await this._userRepository.DoesUserExistAsync(updateModel.Id))
- throw new ArgumentException("User does not exist!");
+ updateUserServiceModel.Technologies.Add(updateTechnologyServiceModel);
+ }
- if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName)
- && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName))
- throw new ArgumentException("Username already exists!");
+ //Clean the already replaced technologies
+ updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty);
- User user = this._userMapper.Map<User>(updateModel);
- bool result = await this._userRepository.EditAsync(user);
+ User user = this._userMapper.Map<User>(updateUserServiceModel);
+ bool successful = await this._userRepository.EditAsync(user);
- if (!result)
+ if (!successful)
throw new InvalidOperationException("Unable to edit user!");
- return this._userMapper.Map<UserServiceModel>(user); ;
+ return this._userMapper.Map<UserServiceModel>(user);
}
#endregion
#region Delete
-
public async Task DeleteUser(Guid id)
{
if (!await this._userRepository.DoesUserExistAsync(id))
@@ -205,75 +173,15 @@ namespace DevHive.Services.Services
if (!result)
throw new InvalidOperationException("Unable to delete user!");
}
-
- public async Task<bool> RemoveFriend(Guid userId, Guid friendId)
- {
- Task<bool> userExists = this._userRepository.DoesUserExistAsync(userId);
- Task<bool> friendExists = this._userRepository.DoesUserExistAsync(friendId);
-
- await Task.WhenAll(userExists, friendExists);
-
- if (!userExists.Result)
- throw new ArgumentException("User doesn't exist!");
-
- if (!friendExists.Result)
- throw new ArgumentException("Friend doesn't exist!");
-
- if (!await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId))
- throw new ArgumentException("This ain't your friend, amigo.");
-
- return await this.RemoveFriend(userId, friendId);
- }
-
- public async Task<bool> RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel)
- {
- bool userExists = await this._userRepository.DoesUserExistAsync(userId);
- bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id);
-
- if (!userExists)
- throw new ArgumentException("User does not exist!");
-
- if (!languageExists)
- throw new ArgumentException("Language does not exist!");
-
- User user = await this._userRepository.GetByIdAsync(userId);
- Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id);
-
- if (!this._userRepository.DoesUserHaveThisLanguage(user, language))
- throw new ArgumentException("User does not have this language!");
-
- return await this._userRepository.RemoveLanguageFromUserAsync(user, language);
- }
-
- public async Task<bool> RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel)
- {
- bool userExists = await this._userRepository.DoesUserExistAsync(userId);
- bool technologyExists = await this._technologyRepository.DoesTechnologyExistAsync(technologyServiceModel.Id);
-
- if (!userExists)
- throw new ArgumentException("User does not exist!");
-
- if (!technologyExists)
- throw new ArgumentException("Language does not exist!");
-
- User user = await this._userRepository.GetByIdAsync(userId);
- Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id);
-
- if (!this._userRepository.DoesUserHaveThisTechnology(user, technology))
- throw new ArgumentException("User does not have this technology!");
-
- return await this._userRepository.RemoveTechnologyFromUserAsync(user, technology);
- }
#endregion
#region Validations
-
public async Task<bool> ValidJWT(Guid id, string rawTokenData)
{
// There is authorization name in the beginning, i.e. "Bearer eyJh..."
var jwt = new JwtSecurityTokenHandler().ReadJwtToken(rawTokenData.Remove(0, 7));
- Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims)[0]);
+ Guid jwtUserID = new Guid(this.GetClaimTypeValues("ID", jwt.Claims).First());
List<string> jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims);
User user = await this._userRepository.GetByIdAsync(jwtUserID)
@@ -312,11 +220,51 @@ namespace DevHive.Services.Services
return toReturn;
}
- private string WriteJWTSecurityToken(Guid userId, IList<Role> roles)
+ 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 (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!");
+ }
+
+ // Languages
+ foreach (var language in updateUserServiceModel.Languages)
+ {
+ Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name);
+
+ if (returnedLanguage == null)
+ 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)
+ throw new ArgumentException($"Technology {technology.Name} does not exist!");
+ }
+ }
+
+ private string WriteJWTSecurityToken(Guid userId, HashSet<Role> roles)
{
byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret);
- List<Claim> claims = new()
+ HashSet<Claim> claims = new()
{
new Claim("ID", $"{userId}"),
};
@@ -339,11 +287,6 @@ namespace DevHive.Services.Services
SecurityToken token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
-
- private string GeneratePasswordHash(string password)
- {
- return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password)));
- }
#endregion
}
}