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 +++++++++++---------- 9 files changed, 96 insertions(+), 79 deletions(-) (limited to 'src/DevHive.Services') 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 } } -- cgit v1.2.3 From f1515439c5fd26a96817723db5d48b77baa82fb6 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 14:38:21 +0200 Subject: Working on Update User; Currently not updating user in UserRepo --- .../Interfaces/Repositories/IUserRepository.cs | 9 +--- src/DevHive.Data/Repositories/UserRepository.cs | 54 ++++++++----------- src/DevHive.Services/Services/UserService.cs | 62 ++++++++++++---------- 3 files changed, 57 insertions(+), 68 deletions(-) (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs index c29669d..4346e9c 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -10,20 +10,13 @@ namespace DevHive.Data.Interfaces.Repositories { //Read Task GetByUsernameAsync(string username); - Language GetUserLanguage(User user, Language language); - HashSet GetUserLanguages(User user); - HashSet GetUserTechnologies(User user); - Technology GetUserTechnology(User user, Technology technology); IEnumerable QueryAll(); //Validations Task DoesEmailExistAsync(string email); Task DoesUserExistAsync(Guid id); Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId); - Task DoesUsernameExistAsync(string username); - bool DoesUserHaveThisLanguage(User user, Language language); bool DoesUserHaveThisUsername(Guid id, string username); - bool DoesUserHaveFriends(User user); - bool DoesUserHaveThisTechnology(User user, Technology technology); + Task DoesUsernameExistAsync(string username); } } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index a2298db..d319ce7 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -8,7 +8,20 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : BaseRepository, IUserRepository + public interface IUserRepository + { + Task DoesEmailExistAsync(string email); + Task DoesUserExistAsync(Guid id); + Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId); + bool DoesUserHaveThisUsername(Guid id, string username); + Task DoesUsernameExistAsync(string username); + Task EditAsync(Guid id, User newEntity); + Task GetByIdAsync(Guid id); + Task GetByUsernameAsync(string username); + IEnumerable QueryAll(); + } + + public class UserRepository : BaseRepository, IUserRepository, IUserRepository { private readonly DevHiveContext _context; @@ -48,27 +61,17 @@ namespace DevHive.Data.Repositories .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } + #endregion - public HashSet GetUserLanguages(User user) - { - return user.Languages; - } - - public Language GetUserLanguage(User user, Language language) + #region Update + public override async Task EditAsync(Guid id, User newEntity) { - return user.Languages - .FirstOrDefault(x => x.Id == language.Id); - } + User user = await GetByIdAsync(id); - public HashSet GetUserTechnologies(User user) - { - return user.Technologies; - } + this._context.Update(user); + user = newEntity; - public Technology GetUserTechnology(User user, Technology technology) - { - return user.Technologies - .FirstOrDefault(x => x.Id == technology.Id); + return await this.SaveChangesAsync(this._context); } #endregion @@ -113,21 +116,6 @@ namespace DevHive.Data.Repositories .Any(x => x.Id == id && x.UserName == username); } - - public bool DoesUserHaveFriends(User user) - { - return user.Friends.Count >= 1; - } - - public bool DoesUserHaveThisLanguage(User user, Language language) - { - return user.Languages.Contains(language); - } - - public bool DoesUserHaveThisTechnology(User user, Technology technology) - { - return user.Technologies.Contains(technology); - } #endregion } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 960630e..0e3bf72 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,9 +111,7 @@ namespace DevHive.Services.Services await this.ValidateUserCollections(updateUserServiceModel); - updateUserServiceModel = await this.PopulateUpdateModelWithIds(updateUserServiceModel); - - User user = this._userMapper.Map(updateUserServiceModel); + User user = await this.PopulateModel(updateUserServiceModel); bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); @@ -298,49 +296,59 @@ namespace DevHive.Services.Services return admin.Id; } - private async Task PopulateUpdateModelWithIds(UpdateUserServiceModel updateUserServiceModel) + private async Task PopulateModel(UpdateUserServiceModel updateUserServiceModel) { - /* Roles */ - int roleCount = updateUserServiceModel.Roles.Count; - for (int i = 0; i < roleCount; i++) + User user = this._userMapper.Map(updateUserServiceModel); + + /* Fetch Roles and replace model's*/ + HashSet roles = new(); + int rolesCount = updateUserServiceModel.Roles.Count; + for (int i = 0; i < rolesCount; 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; + roles.Add(role); } + user.Roles = roles; - /* Languages */ - int langCount = updateUserServiceModel.Languages.Count; - for (int i = 0; i < langCount; i++) + /* Fetch Friends and replace model's*/ + HashSet friends = new(); + int friendsCount = updateUserServiceModel.Friends.Count; + for (int i = 0; i < friendsCount; i++) { - Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid language name!"); + User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? + throw new ArgumentException("Invalid friend's username!"); - updateUserServiceModel.Languages.ElementAt(i).Id = language.Id; + friends.Add(friend); } + user.Friends = friends; - /* Technologies */ - int techCount = updateUserServiceModel.Technologies.Count; - for (int i = 0; i < techCount; i++) + /* Fetch Languages and replace model's*/ + HashSet languages = new(); + int languagesCount = updateUserServiceModel.Languages.Count; + for (int i = 0; i < languagesCount; i++) { - Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? - throw new ArgumentException("Invalid technology name!"); + Language language = await this._languageRepository.GetByNameAsync(updateUserServiceModel.Languages.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid language name!"); - updateUserServiceModel.Technologies.ElementAt(i).Id = technology.Id; + languages.Add(language); } + user.Languages = languages; - /* Friends */ - int friendsCount = updateUserServiceModel.Friends.Count; - for (int i = 0; i < friendsCount; i++) + /* Fetch Technologies and replace model's*/ + HashSet technologies = new(); + int technologiesCount = updateUserServiceModel.Technologies.Count; + for (int i = 0; i < technologiesCount; i++) { - User friend = await this._userRepository.GetByUsernameAsync(updateUserServiceModel.Friends.ElementAt(i).UserName) ?? - throw new ArgumentException("Invalid friend's username!"); + Technology technology = await this._technologyRepository.GetByNameAsync(updateUserServiceModel.Technologies.ElementAt(i).Name) ?? + throw new ArgumentException("Invalid technology name!"); - updateUserServiceModel.Friends.ElementAt(i).Id = friend.Id; + technologies.Add(technology); } + user.Technologies = technologies; - return updateUserServiceModel; + return user; } #endregion } -- cgit v1.2.3 From bddc0b0566bc19e936ccae9d3aa16b6e0116b186 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 15:38:40 +0200 Subject: GetLanguages&GetTechnologies implmenented --- .../Interfaces/Repositories/ILanguageRepository.cs | 5 ++++- .../Interfaces/Repositories/ITechnologyRepository.cs | 5 ++++- src/DevHive.Data/Repositories/LanguageRepository.cs | 7 +++++++ src/DevHive.Data/Repositories/TechnologyRepository.cs | 7 +++++++ .../Configurations/Mapping/TechnologyMappings.cs | 1 + src/DevHive.Services/Interfaces/ILanguageService.cs | 2 ++ src/DevHive.Services/Interfaces/ITechnologyService.cs | 2 ++ src/DevHive.Services/Services/LanguageService.cs | 12 ++++++++---- src/DevHive.Services/Services/TechnologyService.cs | 7 +++++++ src/DevHive.Web/Controllers/LanguageController.cs | 12 ++++++++++++ src/DevHive.Web/Controllers/TechnologyController.cs | 12 ++++++++++++ 11 files changed, 66 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Services') diff --git a/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs index f1d7248..db2949a 100644 --- a/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories.Interfaces; @@ -7,8 +8,10 @@ namespace DevHive.Data.Interfaces.Repositories { public interface ILanguageRepository : IRepository { + HashSet GetLanguages(); + Task GetByNameAsync(string name); + Task DoesLanguageExistAsync(Guid id); Task DoesLanguageNameExistAsync(string languageName); - Task GetByNameAsync(string name); } } diff --git a/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs index fb0ba20..9126bfc 100644 --- a/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Data.Models; using DevHive.Data.Repositories.Interfaces; @@ -7,8 +8,10 @@ namespace DevHive.Data.Interfaces.Repositories { public interface ITechnologyRepository : IRepository { + Task GetByNameAsync(string name); + HashSet GetTechnologies(); + Task DoesTechnologyExistAsync(Guid id); Task DoesTechnologyNameExistAsync(string technologyName); - Task GetByNameAsync(string name); } } diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index c28ef31..f2cc67f 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; @@ -23,6 +25,11 @@ namespace DevHive.Data.Repositories .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } + + public HashSet GetLanguages() + { + return this._context.Languages.ToHashSet(); + } #endregion #region Validations diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 87540fb..e03291d 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; @@ -23,6 +25,11 @@ namespace DevHive.Data.Repositories .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } + + public HashSet GetTechnologies() + { + return this._context.Technologies.ToHashSet(); + } #endregion #region Validations diff --git a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs index 0103ccf..85b57f1 100644 --- a/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/TechnologyMappings.cs @@ -14,6 +14,7 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); + CreateMap(); CreateMap(); } } diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs index 0df9a95..fabbec2 100644 --- a/src/DevHive.Services/Interfaces/ILanguageService.cs +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Services.Models.Language; @@ -9,6 +10,7 @@ namespace DevHive.Services.Interfaces Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); Task GetLanguageById(Guid id); + HashSet GetLanguages(); Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs index 9c5661d..1d4fa8b 100644 --- a/src/DevHive.Services/Interfaces/ITechnologyService.cs +++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using DevHive.Services.Models.Technology; @@ -9,6 +10,7 @@ namespace DevHive.Services.Interfaces Task Create(CreateTechnologyServiceModel technologyServiceModel); Task GetTechnologyById(Guid id); + HashSet GetTechnologies(); Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel); diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index a602d43..a6364d8 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Interfaces.Repositories; @@ -20,7 +21,6 @@ namespace DevHive.Services.Services } #region Create - public async Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel) { if (await this._languageRepository.DoesLanguageNameExistAsync(createLanguageServiceModel.Name)) @@ -40,7 +40,6 @@ namespace DevHive.Services.Services #endregion #region Read - public async Task GetLanguageById(Guid id) { Language language = await this._languageRepository.GetByIdAsync(id); @@ -50,10 +49,16 @@ namespace DevHive.Services.Services return this._languageMapper.Map(language); } + + public HashSet GetLanguages() + { + HashSet languages = this._languageRepository.GetLanguages(); + + return this._languageMapper.Map>(languages); + } #endregion #region Update - public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { bool langExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); @@ -71,7 +76,6 @@ namespace DevHive.Services.Services #endregion #region Delete - public async Task DeleteLanguage(Guid id) { if (!await this._languageRepository.DoesLanguageExistAsync(id)) diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index c879ad7..039cd8a 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Interfaces.Repositories; @@ -48,6 +49,12 @@ namespace DevHive.Services.Services return this._technologyMapper.Map(technology); } + public HashSet GetTechnologies() + { + HashSet technologies = this._technologyRepository.GetTechnologies(); + + return this._technologyMapper.Map>(technologies); + } #endregion #region Update diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index de6bf15..85ec1e1 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Services.Interfaces; @@ -45,6 +46,17 @@ namespace DevHive.Web.Controllers return new OkObjectResult(languageWebModel); } + [HttpGet] + [Route("GetLanguages")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingLanguages() + { + HashSet languageServiceModels = this._languageService.GetLanguages(); + HashSet languageWebModels = this._languageMapper.Map>(languageServiceModels); + + return new OkObjectResult(languageWebModels); + } + [HttpPut] [Authorize(Roles = "Admin")] public async Task Update(Guid id, [FromBody] UpdateLanguageWebModel updateModel) diff --git a/src/DevHive.Web/Controllers/TechnologyController.cs b/src/DevHive.Web/Controllers/TechnologyController.cs index c107c6e..6453d12 100644 --- a/src/DevHive.Web/Controllers/TechnologyController.cs +++ b/src/DevHive.Web/Controllers/TechnologyController.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Services.Interfaces; @@ -45,6 +46,17 @@ namespace DevHive.Web.Controllers return new OkObjectResult(createTechnologyWebModel); } + [HttpGet] + [Route("GetTechnologies")] + [Authorize(Roles = "User,Admin")] + public IActionResult GetAllExistingTechnologies() + { + HashSet technologyServiceModels = this._technologyService.GetTechnologies(); + HashSet languageWebModels = this._technologyMapper.Map>(technologyServiceModels); + + return new OkObjectResult(languageWebModels); + } + [HttpPut] [Authorize(Roles = "Admin")] public async Task Update(Guid id, [FromBody] UpdateTechnologyWebModel updateModel) -- cgit v1.2.3