diff options
Diffstat (limited to 'src/DevHive.Services')
4 files changed, 53 insertions, 2 deletions
diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index d57c6ba..97355d6 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,6 +11,9 @@ namespace DevHive.Services.Configurations.Mapping CreateMap<UserServiceModel, User>(); CreateMap<RegisterServiceModel, User>(); CreateMap<UpdateUserServiceModel, User>(); + CreateMap<UpdateUserCollectionServiceModel, User>() + .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); + CreateMap<User, UserServiceModel>(); } diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs new file mode 100644 index 0000000..c40a980 --- /dev/null +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Identity.User +{ + public class UpdateUserCollectionServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 96d1ff0..5b5a178 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -1,9 +1,19 @@ using System; +using System.Collections.Generic; namespace DevHive.Services.Models.Identity.User { - public class UpdateUserServiceModel : UserServiceModel + public class UpdateUserServiceModel : BaseUserServiceModel { public Guid Id { get; set; } + + public IList<UpdateUserCollectionServiceModel> Roles { get; set; } = new List<UpdateUserCollectionServiceModel>(); + + public IList<UpdateUserCollectionServiceModel> Friends { get; set; } = new List<UpdateUserCollectionServiceModel>(); + + public IList<UpdateUserCollectionServiceModel> Languages { get; set; } = new List<UpdateUserCollectionServiceModel>(); + + public IList<UpdateUserCollectionServiceModel> Technologies { get; set; } = new List<UpdateUserCollectionServiceModel>(); + } } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 1dc1bd5..3dd030a 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Interfaces; using DevHive.Data.Interfaces.Repositories; +using DevHive.Services.Models.Language; namespace DevHive.Services.Services { @@ -139,7 +140,7 @@ namespace DevHive.Services.Services && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) throw new ArgumentException("Username already exists!"); - //Add validations for everything else + await this.ValidateUserCollections(updateModel); User user = this._userMapper.Map<User>(updateModel); bool result = await this._userRepository.EditAsync(user); @@ -149,6 +150,36 @@ namespace DevHive.Services.Services return this._userMapper.Map<UserServiceModel>(user); ; } + + private async Task ValidateUserCollections(UpdateUserServiceModel updateUserServiceModel) + { + // Friends + foreach (UpdateUserCollectionServiceModel friend in updateUserServiceModel.Friends) + { + User returnedFriend = await this._userRepository.GetByUsernameAsync(friend.Name); + + if (default(User) == returnedFriend) + throw new ArgumentException($"User {friend.Name} does not exist!"); + } + + // Languages + foreach (UpdateUserCollectionServiceModel language in updateUserServiceModel.Languages) + { + Language returnedLanguage = await this._languageRepository.GetByNameAsync(language.Name); + + if (default(Language) == returnedLanguage) + throw new ArgumentException($"Language {language.Name} does not exist!"); + } + + // Technology + foreach (UpdateUserCollectionServiceModel technology in updateUserServiceModel.Technologies) + { + Technology returnedTechnology = await this._technologyRepository.GetByNameAsync(technology.Name); + + if (default(Technology) == returnedTechnology) + throw new ArgumentException($"Technology {technology.Name} does not exist!"); + } + } #endregion #region Delete |
