From f0398cf1b7e6477bbd184e7509a1030054fc1926 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sun, 17 Jan 2021 16:26:32 +0200 Subject: Fix lang naming --- src/DevHive.Data/Interfaces/Models/IUser.cs | 2 +- ...112111416_User_Implements_Languages.Designer.cs | 4 ++-- .../Migrations/DevHiveContextModelSnapshot.cs | 4 ++-- src/DevHive.Data/Models/User.cs | 4 ++-- src/DevHive.Data/Repositories/UserRepository.cs | 16 +++++++-------- .../Mapping/UserCollectionMappings.cs | 21 ++++++++++++++++++++ .../Configurations/Mapping/UserMappings.cs | 3 --- src/DevHive.Services/Services/UserService.cs | 23 ++++++++++++++-------- .../DevHive.Data.Tests/UserRepositoryTests.cs | 8 ++++---- .../Configurations/Mapping/RoleMappings.cs | 2 +- 10 files changed, 56 insertions(+), 31 deletions(-) create mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs (limited to 'src') diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs index 0a770f0..ef8c927 100644 --- a/src/DevHive.Data/Interfaces/Models/IUser.cs +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -8,7 +8,7 @@ namespace DevHive.Data.Interfaces.Models string FirstName { get; set; } string LastName { get; set; } string ProfilePictureUrl { get; set; } - IList Langauges { get; set; } + IList Languages { get; set; } IList Technologies { get; set; } IList Roles { get; set; } IList Friends { get; set; } diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs index 0f1aa80..1605b5b 100644 --- a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs @@ -307,7 +307,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -395,7 +395,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index cc6d24d..7197c81 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -305,7 +305,7 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.Language", b => { b.HasOne("DevHive.Data.Models.User", null) - .WithMany("Langauges") + .WithMany("Languages") .HasForeignKey("UserId"); }); @@ -393,7 +393,7 @@ namespace DevHive.Data.Migrations { b.Navigation("Friends"); - b.Navigation("Langauges"); + b.Navigation("Languages"); b.Navigation("Technologies"); }); diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 944bf6a..31e36ac 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -18,12 +18,12 @@ namespace DevHive.Data.Models /// /// Languages that the user uses or is familiar with /// - public IList Langauges { get; set; } + public IList Languages { get; set; } /// /// Technologies that the user uses or is familiar with /// - public IList Technologies { get; set; } + public IList Technologies { get; set; } = new List(); public IList Roles { get; set; } = new List(); diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 17ca93b..6d4a0bf 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -40,7 +40,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Add(language); + user.Languages.Add(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -70,7 +70,7 @@ namespace DevHive.Data.Repositories return await this._context.Users .Include(x => x.Friends) .Include(x => x.Roles) - .Include(x => x.Langauges) + .Include(x => x.Languages) .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.Id == id); } @@ -84,12 +84,12 @@ namespace DevHive.Data.Repositories public IList GetUserLanguages(User user) { - return user.Langauges; + return user.Languages; } public Language GetUserLanguage(User user, Language language) { - return user.Langauges + return user.Languages .FirstOrDefault(x => x.Id == language.Id); } @@ -123,8 +123,8 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(oldLang); - user.Langauges.Add(newLang); + user.Languages.Remove(oldLang); + user.Languages.Add(newLang); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -162,7 +162,7 @@ namespace DevHive.Data.Repositories { this._context.Update(user); - user.Langauges.Remove(language); + user.Languages.Remove(language); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -224,7 +224,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisLanguage(User user, Language language) { - return user.Langauges.Contains(language); + return user.Languages.Contains(language); } public bool DoesUserHaveThisTechnology(User user, Technology technology) diff --git a/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs new file mode 100644 index 0000000..ee505a2 --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Services.Models.Identity.User; + +namespace DevHive.Services.Configurations.Mapping +{ + public class UserCollectionMappings : Profile + { + public UserCollectionMappings() + { + CreateMap() + .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + CreateMap() + .ForMember(r => r.Name, u => u.MapFrom(src => src.Name)); + } + } +} diff --git a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs index 97355d6..d57c6ba 100644 --- a/src/DevHive.Services/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/UserMappings.cs @@ -11,9 +11,6 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap() - .ForMember(up => up.UserName, u => u.MapFrom(src => src.Name)); - CreateMap(); } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 3dd030a..b549b1c 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -131,21 +131,28 @@ namespace DevHive.Services.Services #region Update - public async Task UpdateUser(UpdateUserServiceModel updateModel) + public async Task UpdateUser(UpdateUserServiceModel updateUserServiceModel) { - if (!await this._userRepository.DoesUserExistAsync(updateModel.Id)) + if (!await this._userRepository.DoesUserExistAsync(updateUserServiceModel.Id)) throw new ArgumentException("User does not exist!"); - if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) - && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) + if (!this._userRepository.DoesUserHaveThisUsername(updateUserServiceModel.Id, updateUserServiceModel.UserName) + && await this._userRepository.DoesUsernameExistAsync(updateUserServiceModel.UserName)) throw new ArgumentException("Username already exists!"); - await this.ValidateUserCollections(updateModel); + await this.ValidateUserCollections(updateUserServiceModel); - User user = this._userMapper.Map(updateModel); - bool result = await this._userRepository.EditAsync(user); + //Query proper lang, tech and role and insert the full class in updateUserServiceModel + List properLanguages = new(); + foreach (UpdateUserCollectionServiceModel lang in updateUserServiceModel.Languages) + properLanguages.Add(await this._languageRepository.GetByNameAsync(lang.Name)); - if (!result) + User user = this._userMapper.Map(updateUserServiceModel); + user.Languages = properLanguages; + + bool success = await this._userRepository.EditAsync(user); + + if (!success) throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user); ; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index 46f8aaf..b0a5b93 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -86,7 +86,7 @@ namespace DevHive.Data.Tests //Assert Assert.True(result, "The language isn't inserted properly to the database"); - Assert.True(dummyUser.Langauges.Contains(language), "The language doesn't get added properly to the user"); + Assert.True(dummyUser.Languages.Contains(language), "The language doesn't get added properly to the user"); } [Test] @@ -165,7 +165,7 @@ namespace DevHive.Data.Tests //Arrange User dummyUser = CreateDummyUser(); await this._userRepository.AddAsync(dummyUser); - IList dummyUserLanguages = dummyUser.Langauges; + IList dummyUserLanguages = dummyUser.Languages; //Act IList languages = this._userRepository.GetUserLanguages(dummyUser); @@ -229,7 +229,7 @@ namespace DevHive.Data.Tests FirstName = "Spas", LastName = "Spasov", Email = "abv@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; @@ -271,7 +271,7 @@ namespace DevHive.Data.Tests FirstName = "Alex", LastName = "Spiridonov", Email = "a_spiridonov@abv.bg", - Langauges = languages, + Languages = languages, Technologies = technologies, Roles = roles }; diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index bce7c07..66ae8e3 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Web.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { -- cgit v1.2.3