From b6855296340e4b70ee7a83d3118780eb23e46c83 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Tue, 15 Dec 2020 20:39:30 +0200 Subject: Adding LanguageRepository --- .../Repositories/LanguageRepository.cs | 58 ++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/DevHive.Data/Repositories/LanguageRepository.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs new file mode 100644 index 0000000..26d0d61 --- /dev/null +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class LanguageRepository : IRepository + { + private readonly DbContext _context; + + public LanguageRepository(DbContext context) + { + this._context = context; + } + + //Create + public async Task AddAsync(Language entity) + { + await this._context + .Set() + .AddAsync(entity); + + await this._context.SaveChangesAsync(); + } + + //Read + public async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + //Update + public async Task EditAsync(Language newEntity) + { + this._context + .Set() + .Update(newEntity); + + await this._context.SaveChangesAsync(); + } + + //Delete + public async Task DeleteAsync(Language entity) + { + this._context + .Set() + .Remove(entity); + + await this._context.SaveChangesAsync(); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 13c74f612f392630d162746f939cf99b208ca4c2 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Tue, 15 Dec 2020 21:28:34 +0200 Subject: Adding LanguageService and updating LanguageRepository --- .../Repositories/LanguageRepository.cs | 18 +++++- src/DevHive.Services/Services/LanguageService.cs | 69 ++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/DevHive.Services/Services/LanguageService.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 26d0d61..add0b8c 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; @@ -35,6 +33,22 @@ namespace DevHive.Data.Repositories .FindAsync(id); } + public async Task DoesLanguageNameExist(string languageName) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Name == languageName); + } + + public async Task DoesLanguageExist(Guid id) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + //Update public async Task EditAsync(Language newEntity) { diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs new file mode 100644 index 0000000..f57a2a8 --- /dev/null +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -0,0 +1,69 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Language; +using Microsoft.AspNetCore.Mvc; + +namespace DevHive.Services.Services +{ + public class LanguageService + { + private readonly LanguageRepository _languageRepository; + private readonly IMapper _languageMapper; + + public LanguageService(DevHiveContext context, IMapper mapper) + { + this._languageRepository = new LanguageRepository(context); + this._languageMapper = mapper; + } + + public async Task CreateLanguage(LanguageServiceModel languageServiceModel) + { + if (!await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + return new BadRequestObjectResult("Language already exists!"); + + Language language = this._languageMapper.Map(languageServiceModel); + + await this._languageRepository.AddAsync(language); + + return new CreatedResult("CreateLanguage", language); + } + + public async Task GetLanguageByID(Guid id) + { + Language language = await this._languageRepository.GetByIdAsync(id); + + if(language == null) + return new NotFoundObjectResult("The language does not exist"); + + return new ObjectResult(language); + } + + public async Task UpdateLanguage(LanguageServiceModel languageServiceModel) + { + if (!await this._languageRepository.DoesLanguageExist(languageServiceModel.Id)) + return new NotFoundObjectResult("Language does not exist!"); + + if (!await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + return new BadRequestObjectResult("Language name already exists!"); + + Language language = this._languageMapper.Map(languageServiceModel); + await this._languageRepository.EditAsync(language); + + return new AcceptedResult("UpdateLanguage", language); + } + + public async Task DeleteLanguage(Guid id) + { + if (!await this._languageRepository.DoesLanguageExist(id)) + return new NotFoundObjectResult("Language does not exist!"); + + Language language = await this._languageRepository.GetByIdAsync(id); + await this._languageRepository.DeleteAsync(language); + + return new OkResult(); + } + } +} \ No newline at end of file -- cgit v1.2.3 From 680ad5a635a01278f948d40bacd7fb077522119b Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Wed, 16 Dec 2020 22:09:13 +0200 Subject: Refactored LanguageData --- src/DevHive.Data/Repositories/LanguageRepository.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index add0b8c..08efd18 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -16,13 +16,13 @@ namespace DevHive.Data.Repositories } //Create - public async Task AddAsync(Language entity) + public async Task AddAsync(Language entity) { await this._context .Set() .AddAsync(entity); - await this._context.SaveChangesAsync(); + return await this.SaveChangesAsync(); } //Read @@ -50,23 +50,30 @@ namespace DevHive.Data.Repositories } //Update - public async Task EditAsync(Language newEntity) + public async Task EditAsync(Language newEntity) { this._context .Set() .Update(newEntity); - await this._context.SaveChangesAsync(); + return await this.SaveChangesAsync(); } //Delete - public async Task DeleteAsync(Language entity) + public async Task DeleteAsync(Language entity) { this._context .Set() .Remove(entity); - await this._context.SaveChangesAsync(); + return await this.SaveChangesAsync(); } + + private async Task SaveChangesAsync() + { + int result = await this._context.SaveChangesAsync(); + + return result >= 0; + } } } \ No newline at end of file -- cgit v1.2.3 From cd02428e748e691a0005b2687edf69a99766aac6 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 16 Dec 2020 22:38:25 +0200 Subject: Added DevHive.Common; Changed repositories behavior; Abstracted some common logic --- src/DevHive.Common/DevHive.Common.csproj | 12 ++++++++++++ src/DevHive.Common/Models/Data/RepositoryMethods.cs | 15 +++++++++++++++ src/DevHive.Common/Models/Identity/TokenModel.cs | 12 ++++++++++++ src/DevHive.Data/DevHive.Data.csproj | 4 ++++ src/DevHive.Data/Repositories/LanguageRepository.cs | 14 ++++---------- src/DevHive.Data/Repositories/RoleRepository.cs | 20 ++++++++++++-------- src/DevHive.Data/Repositories/UserRepository.cs | 13 +++++++------ src/DevHive.Services/DevHive.Services.csproj | 4 ++++ .../Models/Identity/User/TokenServiceModel.cs | 12 ------------ src/DevHive.Services/Services/UserService.cs | 20 +++++++++++++------- .../Configurations/Mapping/UserMappings.cs | 4 ++-- src/DevHive.Web/Controllers/UserController.cs | 10 +++++----- src/DevHive.Web/DevHive.Web.csproj | 1 + src/DevHive.code-workspace | 4 ++++ 14 files changed, 95 insertions(+), 50 deletions(-) create mode 100644 src/DevHive.Common/DevHive.Common.csproj create mode 100644 src/DevHive.Common/Models/Data/RepositoryMethods.cs create mode 100644 src/DevHive.Common/Models/Identity/TokenModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Common/DevHive.Common.csproj b/src/DevHive.Common/DevHive.Common.csproj new file mode 100644 index 0000000..1aca8a6 --- /dev/null +++ b/src/DevHive.Common/DevHive.Common.csproj @@ -0,0 +1,12 @@ + + + + net5.0 + + + + + + + + diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs new file mode 100644 index 0000000..b56aad9 --- /dev/null +++ b/src/DevHive.Common/Models/Data/RepositoryMethods.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Common.Models.Data +{ + public static class RepositoryMethods + { + public static async Task SaveChangesAsync(DbContext context) + { + int result = await context.SaveChangesAsync(); + + return result >= 1; + } + } +} \ No newline at end of file diff --git a/src/DevHive.Common/Models/Identity/TokenModel.cs b/src/DevHive.Common/Models/Identity/TokenModel.cs new file mode 100644 index 0000000..0fb6c82 --- /dev/null +++ b/src/DevHive.Common/Models/Identity/TokenModel.cs @@ -0,0 +1,12 @@ +namespace DevHive.Common.Models.Identity +{ + public class TokenModel + { + public TokenModel(string token) + { + this.Token = token; + } + + public string Token { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/DevHive.Data.csproj b/src/DevHive.Data/DevHive.Data.csproj index d472d1c..c1e1592 100644 --- a/src/DevHive.Data/DevHive.Data.csproj +++ b/src/DevHive.Data/DevHive.Data.csproj @@ -16,4 +16,8 @@ + + + + diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 08efd18..a33bd5b 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Data; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -22,7 +23,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Read @@ -56,7 +57,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Delete @@ -66,14 +67,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await this.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } - - private async Task SaveChangesAsync() - { - int result = await this._context.SaveChangesAsync(); - - return result >= 0; - } } } \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 9b6cf14..ad9bda0 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,6 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Data; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -16,13 +17,13 @@ namespace DevHive.Data.Repositories } //Create - public async Task AddAsync(Role entity) + public async Task AddAsync(Role entity) { await this._context .Set() .AddAsync(entity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Read @@ -35,23 +36,26 @@ namespace DevHive.Data.Repositories } //Update - public async Task EditAsync(Role newEntity) + public async Task EditAsync(Role newEntity) { + Role role = await this.GetByIdAsync(newEntity.Id); + this._context - .Set() - .Update(newEntity); + .Entry(role) + .CurrentValues + .SetValues(newEntity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Delete - public async Task DeleteAsync(Role entity) + public async Task DeleteAsync(Role entity) { this._context .Set() .Remove(entity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } public async Task DoesNameExist(string name) diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 714218d..f5a074b 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Data; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -18,13 +19,13 @@ namespace DevHive.Data.Repositories } //Create - public async Task AddAsync(User entity) + public async Task AddAsync(User entity) { await this._context .Set() .AddAsync(entity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Read @@ -51,7 +52,7 @@ namespace DevHive.Data.Repositories } //Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User newEntity) { User user = await this.GetByIdAsync(newEntity.Id); @@ -60,17 +61,17 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Delete - public async Task DeleteAsync(User entity) + public async Task DeleteAsync(User entity) { this._context .Set() .Remove(entity); - await this._context.SaveChangesAsync(); + return await RepositoryMethods.SaveChangesAsync(this._context); } //Validations diff --git a/src/DevHive.Services/DevHive.Services.csproj b/src/DevHive.Services/DevHive.Services.csproj index 280610d..19f67d8 100644 --- a/src/DevHive.Services/DevHive.Services.csproj +++ b/src/DevHive.Services/DevHive.Services.csproj @@ -16,5 +16,9 @@ + + + + diff --git a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs b/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs deleted file mode 100644 index 631808e..0000000 --- a/src/DevHive.Services/Models/Identity/User/TokenServiceModel.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace DevHive.Services.Models.Identity.User -{ - public class TokenServiceModel - { - public TokenServiceModel(string token) - { - this.Token = token; - } - - public string Token { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 24f74f5..63b8389 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -3,7 +3,6 @@ using DevHive.Data.Repositories; using DevHive.Services.Options; using DevHive.Services.Models.Identity.User; using System.Threading.Tasks; -using Microsoft.AspNetCore.Mvc; using DevHive.Data.Models; using System; using System.IdentityModel.Tokens.Jwt; @@ -12,6 +11,7 @@ using Microsoft.IdentityModel.Tokens; using System.Security.Cryptography; using System.Text; using System.Collections.Generic; +using DevHive.Common.Models.Identity; namespace DevHive.Services.Services { @@ -28,7 +28,7 @@ namespace DevHive.Services.Services this._jwtOptions = jwtOptions; } - public async Task LoginUser(LoginServiceModel loginModel) + public async Task LoginUser(LoginServiceModel loginModel) { if (!await this._userRepository.IsUsernameValid(loginModel.UserName)) throw new ArgumentException("Invalid username!"); @@ -38,10 +38,10 @@ namespace DevHive.Services.Services if (user.PasswordHash != GeneratePasswordHash(loginModel.Password)) throw new ArgumentException("Incorrect password!"); - return new TokenServiceModel(WriteJWTSecurityToken(user.Role)); + return new TokenModel(WriteJWTSecurityToken(user.Role)); } - public async Task RegisterUser(RegisterServiceModel registerModel) + public async Task RegisterUser(RegisterServiceModel registerModel) { if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) throw new ArgumentException("Username already exists!"); @@ -55,7 +55,7 @@ namespace DevHive.Services.Services await this._userRepository.AddAsync(user); - return new TokenServiceModel(WriteJWTSecurityToken(user.Role)); + return new TokenModel(WriteJWTSecurityToken(user.Role)); } public async Task GetUserById(Guid id) @@ -76,7 +76,10 @@ namespace DevHive.Services.Services throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map(updateModel); - await this._userRepository.EditAsync(user); + bool result = await this._userRepository.EditAsync(user); + + if (!result) + throw new InvalidOperationException("Unable to edit user!"); return this._userMapper.Map(user);; } @@ -87,7 +90,10 @@ namespace DevHive.Services.Services throw new ArgumentException("User does not exist!"); User user = await this._userRepository.GetByIdAsync(id); - await this._userRepository.DeleteAsync(user); + bool result = await this._userRepository.DeleteAsync(user); + + if (!result) + throw new InvalidOperationException("Unable to delete user!"); } private string GeneratePasswordHash(string password) diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 1fdfc6a..4420368 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -1,7 +1,7 @@ -using DevHive.Data.Models; using AutoMapper; using DevHive.Services.Models.Identity.User; using DevHive.Web.Models.Identity.User; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Configurations.Mapping { @@ -16,7 +16,7 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 80e1bde..1360bfe 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -1,7 +1,6 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Models.Identity.User; using DevHive.Services.Options; @@ -9,6 +8,7 @@ using DevHive.Services.Services; using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using DevHive.Common.Models.Identity; namespace DevHive.Web.Controllers { @@ -33,8 +33,8 @@ namespace DevHive.Web.Controllers { LoginServiceModel loginServiceModel = this._userMapper.Map(loginModel); - TokenServiceModel tokenServiceModel = await this._userService.LoginUser(loginServiceModel); - TokenWebModel tokenWebModel = this._userMapper.Map(tokenServiceModel); + TokenModel TokenModel = await this._userService.LoginUser(loginServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(TokenModel); return new OkObjectResult(tokenWebModel); } @@ -46,8 +46,8 @@ namespace DevHive.Web.Controllers { RegisterServiceModel registerServiceModel = this._userMapper.Map(registerModel); - TokenServiceModel tokenServiceModel = await this._userService.RegisterUser(registerServiceModel); - TokenWebModel tokenWebModel = this._userMapper.Map(tokenServiceModel); + TokenModel TokenModel = await this._userService.RegisterUser(registerServiceModel); + TokenWebModel tokenWebModel = this._userMapper.Map(TokenModel); return new CreatedResult("Register", tokenWebModel); } diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj index 2a1faf1..0be5bdc 100644 --- a/src/DevHive.Web/DevHive.Web.csproj +++ b/src/DevHive.Web/DevHive.Web.csproj @@ -20,5 +20,6 @@ + \ No newline at end of file diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace index 730aab1..7e952c7 100644 --- a/src/DevHive.code-workspace +++ b/src/DevHive.code-workspace @@ -12,6 +12,10 @@ "name": "DevHive.Data", "path": "./DevHive.Data" }, + { + "name": "DevHive.Common", + "path": "./DevHive.Common" + }, ], "settings": { "files.exclude": { -- cgit v1.2.3 From fa09c0cc6cf99034dcc1301692ccb4d019087213 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 19 Dec 2020 15:47:01 +0200 Subject: Moved Friends to User(you no longer have friends) --- src/DevHive.Data/DevHiveContext.cs | 6 +- src/DevHive.Data/Repositories/FriendsRepository.cs | 38 ----------- .../Repositories/LanguageRepository.cs | 4 +- src/DevHive.Data/Repositories/RoleRepository.cs | 4 +- .../Repositories/TechnologyRepository.cs | 4 +- src/DevHive.Data/Repositories/UserRepository.cs | 20 +++++- src/DevHive.Services/Services/FriendsService.cs | 76 ---------------------- src/DevHive.Services/Services/UserService.cs | 73 +++++++++++++++++---- .../Configurations/Extensions/ConfigureDatabase.cs | 3 +- src/DevHive.Web/Controllers/FriendsController.cs | 59 ----------------- src/DevHive.Web/Controllers/UserController.cs | 28 ++++++++ src/DevHive.Web/Startup.cs | 2 +- 12 files changed, 117 insertions(+), 200 deletions(-) delete mode 100644 src/DevHive.Data/Repositories/FriendsRepository.cs delete mode 100644 src/DevHive.Services/Services/FriendsService.cs delete mode 100644 src/DevHive.Web/Controllers/FriendsController.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index e8eb5fb..39d39d3 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -7,7 +7,7 @@ namespace DevHive.Data { public class DevHiveContext : IdentityDbContext { - public DevHiveContext(DbContextOptions options) + public DevHiveContext(DbContextOptions options) : base(options) { } public DbSet Technologies { get; set; } @@ -25,10 +25,6 @@ namespace DevHive.Data builder.Entity() .HasMany(x => x.Friends); - builder.Entity() - .HasIndex(x => x.Id) - .IsUnique(); - base.OnModelCreating(builder); } } diff --git a/src/DevHive.Data/Repositories/FriendsRepository.cs b/src/DevHive.Data/Repositories/FriendsRepository.cs deleted file mode 100644 index c65ba5e..0000000 --- a/src/DevHive.Data/Repositories/FriendsRepository.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using DevHive.Common.Models.Data; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class FriendsRepository : UserRepository - { - private readonly DbContext _context; - - public FriendsRepository(DbContext context) - : base(context) - { - this._context = context; - } - - //Create - public async Task AddFriendAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Add(friend); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - //Delete - public async Task RemoveFriendAsync(User user, User friend) - { - this._context.Update(user); - user.Friends.Remove(friend); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index a33bd5b..6e3a2e3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories { public class LanguageRepository : IRepository { - private readonly DbContext _context; + private readonly DevHiveContext _context; - public LanguageRepository(DbContext context) + public LanguageRepository(DevHiveContext context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 21c29db..1d6f2da 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories { public class RoleRepository : IRepository { - private readonly DbContext _context; + private readonly DevHiveContext _context; - public RoleRepository(DbContext context) + public RoleRepository(DevHiveContext context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index debfd3e..e2e4257 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -9,9 +9,9 @@ namespace DevHive.Data.Repositories { public class TechnologyRepository : IRepository { - private readonly DbContext _context; + private readonly DevHiveContext _context; - public TechnologyRepository(DbContext context) + public TechnologyRepository(DevHiveContext context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5cd4264..d2a8830 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -11,9 +11,9 @@ namespace DevHive.Data.Repositories { public class UserRepository : IRepository { - private readonly DbContext _context; + private readonly DevHiveContext _context; - public UserRepository(DbContext context) + public UserRepository(DevHiveContext context) { this._context = context; } @@ -27,6 +27,14 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + + public async Task AddFriendAsync(User user, User friend) + { + this._context.Update(user); + user.Friends.Add(friend); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } //Read public IEnumerable QueryAll() @@ -77,6 +85,14 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + + public async Task RemoveFriendAsync(User user, User friend) + { + this._context.Update(user); + user.Friends.Remove(friend); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } //Validations public bool DoesUserExist(Guid id) diff --git a/src/DevHive.Services/Services/FriendsService.cs b/src/DevHive.Services/Services/FriendsService.cs deleted file mode 100644 index 119c955..0000000 --- a/src/DevHive.Services/Services/FriendsService.cs +++ /dev/null @@ -1,76 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Identity.User; -using DevHive.Services.Options; - -namespace DevHive.Services.Services -{ - public class FriendsService - { - private readonly FriendsRepository _friendsRepository; - private readonly IMapper _friendsMapper; - - public FriendsService(FriendsRepository friendsRepository, IMapper mapper) - { - this._friendsRepository = friendsRepository; - this._friendsMapper = mapper; - } - - //Create - public async Task AddFriend(Guid userId, Guid friendId) - { - User user = await this._friendsRepository.GetByIdAsync(userId); - User friend = await this._friendsRepository.GetByIdAsync(friendId); - - if (DoesUserHaveThisFriend(user, friend)) - throw new ArgumentException("Friend already exists in your friends list."); - - return user != default(User) && friend != default(User) ? - await this._friendsRepository.AddFriendAsync(user, friend) : - throw new ArgumentException("Invalid user!"); - } - - //Read - public async Task GetFriendById(Guid friendId) - { - if(!_friendsRepository.DoesUserExist(friendId)) - throw new ArgumentException("User does not exist!"); - - User friend = await this._friendsRepository.GetByIdAsync(friendId); - - return this._friendsMapper.Map(friend); - } - - public async Task RemoveFriend(Guid userId, Guid friendId) - { - if(!this._friendsRepository.DoesUserExist(userId) && - !this._friendsRepository.DoesUserExist(friendId)) - throw new ArgumentException("Invalid user!"); - - User user = await this._friendsRepository.GetByIdAsync(userId); - User friend = await this._friendsRepository.GetByIdAsync(friendId); - - if(!this.DoesUserHaveFriends(user)) - throw new ArgumentException("User does not have any friends."); - - if (!DoesUserHaveThisFriend(user, friend)) - throw new ArgumentException("This ain't your friend, amigo."); - - return await this.RemoveFriend(user.Id, friendId); - } - - //Validation - private bool DoesUserHaveThisFriend(User user, User friend) - { - return user.Friends.Contains(friend); - } - - private bool DoesUserHaveFriends(User user) - { - return user.Friends.Count >= 1; - } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 99a8d9f..7fd0682 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -67,6 +67,19 @@ namespace DevHive.Services.Services return new TokenModel(WriteJWTSecurityToken(user.UserName, user.Roles)); } + public async Task AddFriend(Guid userId, Guid friendId) + { + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + if (DoesUserHaveThisFriend(user, friend)) + throw new ArgumentException("Friend already exists in your friends list."); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriendAsync(user, friend) : + throw new ArgumentException("Invalid user!"); + } + public async Task GetUserById(Guid id) { User user = await this._userRepository.GetByIdAsync(id) @@ -75,6 +88,16 @@ namespace DevHive.Services.Services return this._userMapper.Map(user); } + public async Task GetFriendById(Guid friendId) + { + if(!_userRepository.DoesUserExist(friendId)) + throw new ArgumentException("User does not exist!"); + + User friend = await this._userRepository.GetByIdAsync(friendId); + + return this._userMapper.Map(friend); + } + public async Task UpdateUser(UpdateUserServiceModel updateModel) { if (!this._userRepository.DoesUserExist(updateModel.Id)) @@ -105,6 +128,24 @@ namespace DevHive.Services.Services throw new InvalidOperationException("Unable to delete user!"); } + public async Task RemoveFriend(Guid userId, Guid friendId) + { + if(!this._userRepository.DoesUserExist(userId) && + !this._userRepository.DoesUserExist(friendId)) + throw new ArgumentException("Invalid user!"); + + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + if(!this.DoesUserHaveFriends(user)) + throw new ArgumentException("User does not have any friends."); + + if (!DoesUserHaveThisFriend(user, friend)) + throw new ArgumentException("This ain't your friend, amigo."); + + return await this.RemoveFriend(user.Id, friendId); + } + public async Task ValidJWT(Guid id, string rawTokenData) { // There is authorization name in the beginning, i.e. "Bearer eyJh..." @@ -143,6 +184,27 @@ namespace DevHive.Services.Services return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); } + private bool DoesUserHaveThisFriend(User user, User friend) + { + return user.Friends.Contains(friend); + } + + private bool DoesUserHaveFriends(User user) + { + return user.Friends.Count >= 1; + } + + private List GetClaimTypeValues(string type, IEnumerable claims) + { + List toReturn = new(); + + foreach(var claim in claims) + if (claim.Type == type) + toReturn.Add(claim.Value); + + return toReturn; + } + private string WriteJWTSecurityToken(string userName, IList roles) { byte[] signingKey = Encoding.ASCII.GetBytes(_jwtOptions.Secret); @@ -170,16 +232,5 @@ namespace DevHive.Services.Services SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); return tokenHandler.WriteToken(token); } - - private List GetClaimTypeValues(string type, IEnumerable claims) - { - List toReturn = new(); - - foreach(var claim in claims) - if (claim.Type == type) - toReturn.Add(claim.Value); - - return toReturn; - } } } diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs index 7ef144c..b42ae05 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDatabase.cs @@ -16,8 +16,7 @@ namespace DevHive.Web.Configurations.Extensions public static void DatabaseConfiguration(this IServiceCollection services, IConfiguration configuration) { services.AddDbContext(options => - options.UseNpgsql(configuration.GetConnectionString("DEV"), - x => x.MigrationsAssembly("DevHive.Web"))); + options.UseNpgsql(configuration.GetConnectionString("DEV"))); services.AddIdentity() .AddEntityFrameworkStores(); diff --git a/src/DevHive.Web/Controllers/FriendsController.cs b/src/DevHive.Web/Controllers/FriendsController.cs deleted file mode 100644 index 1b9b9c5..0000000 --- a/src/DevHive.Web/Controllers/FriendsController.cs +++ /dev/null @@ -1,59 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Common.Models; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Identity.User; -using DevHive.Services.Options; -using DevHive.Services.Services; -using DevHive.Web.Models.Identity.User; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; - -namespace DevHive.Web.Controllers -{ - [ApiController] - [Route("/api")] - [Authorize(Roles = "User")] - public class FriendsController - { - private readonly FriendsService _friendsService; - private readonly IMapper _friendsMapper; - - public FriendsController(FriendsService friendsService, IMapper mapper) - { - this._friendsService = friendsService; - this._friendsMapper = mapper; - } - - //Create - [HttpPost] - [Route("AddAFriend")] - public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) - { - return await this._friendsService.AddFriend(userId, friendIdModel.Id) ? - new OkResult() : - new BadRequestResult(); - } - - //Read - [HttpGet] - [Route("GetAFriend")] - public async Task GetAFriend(Guid friendId) - { - UserServiceModel friendServiceModel = await this._friendsService.GetFriendById(friendId); - UserWebModel friend = this._friendsMapper.Map(friendServiceModel); - - return new OkObjectResult(friend); - } - - //Delete - [HttpDelete] - [Route("RemoveAFriend")] - public async Task RemoveAFriend(Guid userId, Guid friendId) - { - await this._friendsService.RemoveFriend(userId, friendId); - return new OkResult(); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index b23fdee..ad2656c 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,6 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; +using DevHive.Common.Models; namespace DevHive.Web.Controllers { @@ -53,6 +54,15 @@ namespace DevHive.Web.Controllers return new CreatedResult("Register", tokenWebModel); } + [HttpPost] + [Route("AddAFriend")] + public async Task AddAFriend(Guid userId, [FromBody] IdModel friendIdModel) + { + return await this._userService.AddFriend(userId, friendIdModel.Id) ? + new OkResult() : + new BadRequestResult(); + } + //Read [HttpGet] public async Task GetById(Guid id, [FromHeader] string authorization) @@ -66,6 +76,16 @@ namespace DevHive.Web.Controllers return new OkObjectResult(userWebModel); } + [HttpGet] + [Route("GetAFriend")] + public async Task GetAFriend(Guid friendId) + { + UserServiceModel friendServiceModel = await this._userService.GetFriendById(friendId); + UserWebModel friend = this._userMapper.Map(friendServiceModel); + + return new OkObjectResult(friend); + } + //Update [HttpPut] public async Task Update(Guid id, [FromBody] UpdateUserWebModel updateModel, [FromHeader] string authorization) @@ -93,5 +113,13 @@ namespace DevHive.Web.Controllers await this._userService.DeleteUser(id); return new OkResult(); } + + [HttpDelete] + [Route("RemoveAFriend")] + public async Task RemoveAFriend(Guid userId, Guid friendId) + { + await this._userService.RemoveFriend(userId, friendId); + return new OkResult(); + } } } diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index 66fde9e..12f72f7 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -45,7 +45,7 @@ namespace DevHive.Web } else { - app.UseExceptionHandler("/api/HttpError"); + app.UseExceptionHandler("/api/Error"); app.UseHsts(); } -- cgit v1.2.3 From 278130d86378a6b2db6ba443631f303fb7d7e207 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 30 Dec 2020 21:21:49 +0200 Subject: Implemented Posts and merged Comment to Post --- src/DevHive.Common/Models/Data/IdModel.cs | 9 -- .../Models/Data/RepositoryMethods.cs | 2 +- src/DevHive.Common/Models/Misc/IdModel.cs | 9 ++ src/DevHive.Data/Models/Comment.cs | 4 +- src/DevHive.Data/Models/Post.cs | 21 ++++ src/DevHive.Data/Repositories/CommentRepository.cs | 62 ---------- .../Repositories/LanguageRepository.cs | 2 +- src/DevHive.Data/Repositories/PostRepository.cs | 107 +++++++++++++++++ src/DevHive.Data/Repositories/RoleRepository.cs | 2 +- .../Repositories/TechnologyRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 2 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- .../Configurations/Mapping/PostMappings.cs | 16 +++ .../Models/Comment/CommentServiceModel.cs | 10 -- .../Models/Comment/GetByIdCommentServiceModel.cs | 9 -- .../Models/Comment/UpdateCommnetServiceModel.cs | 9 -- .../Models/Post/Comment/BaseCommentServiceModel.cs | 11 ++ .../Models/Post/Comment/CommentServiceModel.cs | 9 ++ .../Post/Comment/CreateCommentServiceModel.cs | 9 ++ .../Post/Comment/UpdateCommnetServiceModel.cs | 8 ++ .../Models/Post/Post/BasePostServiceModel.cs | 11 ++ .../Models/Post/Post/CreatePostServiceModel.cs | 9 ++ .../Models/Post/Post/PostServiceModel.cs | 12 ++ .../Models/Post/Post/UpdatePostServiceModel.cs | 7 ++ src/DevHive.Services/Services/CommentService.cs | 62 ---------- src/DevHive.Services/Services/PostService.cs | 98 +++++++++++++++ .../Extensions/ConfigureDependencyInjection.cs | 4 +- .../Configurations/Mapping/CommentMappings.cs | 7 +- src/DevHive.Web/Controllers/CommentController.cs | 72 ----------- src/DevHive.Web/Controllers/PostController.cs | 132 +++++++++++++++++++++ src/DevHive.Web/Controllers/RoleController.cs | 2 +- src/DevHive.Web/Controllers/UserController.cs | 2 +- src/DevHive.Web/Models/Comment/CommentWebModel.cs | 10 -- .../Models/Comment/GetByIdCommentWebModel.cs | 9 -- .../Models/Post/Comment/CommentWebModel.cs | 10 ++ .../Models/Post/Post/BasePostWebModel.cs | 10 ++ .../Models/Post/Post/CreatePostWebModel.cs | 8 ++ src/DevHive.Web/Models/Post/Post/PostWebModel.cs | 21 ++++ .../Models/Post/Post/UpdatePostModel.cs | 7 ++ src/DevHive.Web/Startup.cs | 4 - 40 files changed, 533 insertions(+), 274 deletions(-) delete mode 100644 src/DevHive.Common/Models/Data/IdModel.cs create mode 100644 src/DevHive.Common/Models/Misc/IdModel.cs create mode 100644 src/DevHive.Data/Models/Post.cs delete mode 100644 src/DevHive.Data/Repositories/CommentRepository.cs create mode 100644 src/DevHive.Data/Repositories/PostRepository.cs create mode 100644 src/DevHive.Services/Configurations/Mapping/PostMappings.cs delete mode 100644 src/DevHive.Services/Models/Comment/CommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/PostServiceModel.cs create mode 100644 src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs delete mode 100644 src/DevHive.Services/Services/CommentService.cs create mode 100644 src/DevHive.Services/Services/PostService.cs delete mode 100644 src/DevHive.Web/Controllers/CommentController.cs create mode 100644 src/DevHive.Web/Controllers/PostController.cs delete mode 100644 src/DevHive.Web/Models/Comment/CommentWebModel.cs delete mode 100644 src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/PostWebModel.cs create mode 100644 src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Common/Models/Data/IdModel.cs b/src/DevHive.Common/Models/Data/IdModel.cs deleted file mode 100644 index 1f2bf9a..0000000 --- a/src/DevHive.Common/Models/Data/IdModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Common.Models.Data -{ - public class IdModel - { - public Guid Id { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs index b56aad9..bfd057f 100644 --- a/src/DevHive.Common/Models/Data/RepositoryMethods.cs +++ b/src/DevHive.Common/Models/Data/RepositoryMethods.cs @@ -1,7 +1,7 @@ using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; -namespace DevHive.Common.Models.Data +namespace DevHive.Common.Models.Misc { public static class RepositoryMethods { diff --git a/src/DevHive.Common/Models/Misc/IdModel.cs b/src/DevHive.Common/Models/Misc/IdModel.cs new file mode 100644 index 0000000..a5c7b65 --- /dev/null +++ b/src/DevHive.Common/Models/Misc/IdModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Common.Models.Misc +{ + public class IdModel + { + public Guid Id { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs index f949a42..8cf848f 100644 --- a/src/DevHive.Data/Models/Comment.cs +++ b/src/DevHive.Data/Models/Comment.cs @@ -4,8 +4,8 @@ namespace DevHive.Data.Models public class Comment : IModel { public Guid Id { get; set; } - public Guid UserId { get; set; } + public Guid IssuerId { get; set; } public string Message { get; set; } - public DateTime Date { get; set; } + public DateTime TimeCreated { get; set; } } } \ No newline at end of file diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs new file mode 100644 index 0000000..a5abf12 --- /dev/null +++ b/src/DevHive.Data/Models/Post.cs @@ -0,0 +1,21 @@ +using System; +using System.ComponentModel.DataAnnotations.Schema; + +namespace DevHive.Data.Models +{ + [Table("Posts")] + public class Post + { + public Guid Id { get; set; } + + public Guid IssuerId { get; set; } + + public DateTime TimeCreated { get; set; } + + public string Message { get; set; } + + //public File[] Files { get; set; } + + public Comment[] Comments { get; set; } + } +} diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs deleted file mode 100644 index 5a4ef17..0000000 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Threading.Tasks; -using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; -using DevHive.Data.Models; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Data.Repositories -{ - public class CommentRepository : IRepository - { - private readonly DevHiveContext _context; - - public CommentRepository(DevHiveContext context) - { - this._context = context; - } - - public async Task AddAsync(Comment entity) - { - await this._context - .Set() - .AddAsync(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task GetByIdAsync(Guid id) - { - return await this._context - .Set() - .FindAsync(id); - } - - - public async Task EditAsync(Comment newEntity) - { - this._context - .Set() - .Update(newEntity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - - public async Task DoesCommentExist(Guid id) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } - - public async Task DeleteAsync(Comment entity) - { - this._context - .Set() - .Remove(entity); - - return await RepositoryMethods.SaveChangesAsync(this._context); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 6e3a2e3..1ab870a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs new file mode 100644 index 0000000..5dfee0b --- /dev/null +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -0,0 +1,107 @@ +using System; +using System.Threading.Tasks; +using Data.Models.Interfaces.Database; +using DevHive.Common.Models.Misc; +using DevHive.Data.Models; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class PostRepository : IRepository + { + private readonly DevHiveContext _context; + + public PostRepository(DevHiveContext context) + { + this._context = context; + } + + //Create + public async Task AddAsync(Post post) + { + await this._context + .Set() + .AddAsync(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddCommentAsync(Comment entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Read + public async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public async Task GetCommentByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + //Update + public async Task EditAsync(Post newPost) + { + this._context + .Set() + .Update(newPost); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task EditCommentAsync(Comment newEntity) + { + this._context + .Set() + .Update(newEntity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + //Delete + public async Task DeleteAsync(Post post) + { + this._context + .Set() + .Remove(post); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DeleteCommentAsync(Comment entity) + { + this._context + .Set() + .Remove(entity); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task DoesPostExist(Guid postId) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == postId); + } + + public async Task DoesCommentExist(Guid id) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(r => r.Id == id); + } + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 1d6f2da..fd04866 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index e2e4257..935582c 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -3,7 +3,7 @@ using System.Threading.Tasks; using Data.Models.Interfaces.Database; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; namespace DevHive.Data.Repositories { diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index d2a8830..5600451 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Data.Models.Interfaces.Database; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs index f4197a0..f903128 100644 --- a/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/CommentMappings.cs @@ -1,6 +1,7 @@ using DevHive.Data.Models; using AutoMapper; -using DevHive.Services.Models.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Common.Models.Misc; namespace DevHive.Services.Configurations.Mapping { @@ -11,8 +12,8 @@ namespace DevHive.Services.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Configurations/Mapping/PostMappings.cs b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs new file mode 100644 index 0000000..695ffdb --- /dev/null +++ b/src/DevHive.Services/Configurations/Mapping/PostMappings.cs @@ -0,0 +1,16 @@ +using DevHive.Data.Models; +using AutoMapper; +using DevHive.Services.Models.Post; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Configurations.Mapping +{ + public class PostMappings : Profile + { + public PostMappings() + { + CreateMap(); + CreateMap(); + } + } +} diff --git a/src/DevHive.Services/Models/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Comment/CommentServiceModel.cs deleted file mode 100644 index f3638ac..0000000 --- a/src/DevHive.Services/Models/Comment/CommentServiceModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class CommentServiceModel - { - public Guid UserId { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs b/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs deleted file mode 100644 index c2b84b3..0000000 --- a/src/DevHive.Services/Models/Comment/GetByIdCommentServiceModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class GetByIdCommentServiceModel : CommentServiceModel - { - public DateTime Date { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs deleted file mode 100644 index 3a461c5..0000000 --- a/src/DevHive.Services/Models/Comment/UpdateCommnetServiceModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Services.Models.Comment -{ - public class UpdateCommentServiceModel : CommentServiceModel - { - public Guid Id { get; set; } - } -} diff --git a/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs new file mode 100644 index 0000000..3aa92ee --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/BaseCommentServiceModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class BaseCommentServiceModel + { + public Guid Id { 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/Post/Comment/CommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs new file mode 100644 index 0000000..a0fa53e --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/CommentServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class CommentServiceModel : CreateCommentServiceModel + { + + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs new file mode 100644 index 0000000..33f3bfe --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/CreateCommentServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class CreateCommentServiceModel : BaseCommentServiceModel + { + public DateTime TimeCreated { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs new file mode 100644 index 0000000..9516a2b --- /dev/null +++ b/src/DevHive.Services/Models/Post/Comment/UpdateCommnetServiceModel.cs @@ -0,0 +1,8 @@ +using System; + +namespace DevHive.Services.Models.Post.Comment +{ + public class UpdateCommentServiceModel : BaseCommentServiceModel + { + } +} diff --git a/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs new file mode 100644 index 0000000..45a677c --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/BasePostServiceModel.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class BasePostServiceModel + { + public Guid Id { 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/Post/Post/CreatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs new file mode 100644 index 0000000..97225c7 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/CreatePostServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class CreatePostServiceModel : BasePostServiceModel + { + public DateTime TimeCreated { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs new file mode 100644 index 0000000..b9c2128 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/PostServiceModel.cs @@ -0,0 +1,12 @@ +using System; + +namespace DevHive.Services.Models.Post.Post +{ + public class PostServiceModel : CreatePostServiceModel + { + + //public File[] Files { get; set; } + + //public Comment[] Comments { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs new file mode 100644 index 0000000..de61a72 --- /dev/null +++ b/src/DevHive.Services/Models/Post/Post/UpdatePostServiceModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Services.Models.Post.Post +{ + public class UpdatePostServiceModel : BasePostServiceModel + { + + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Services/CommentService.cs b/src/DevHive.Services/Services/CommentService.cs deleted file mode 100644 index 69dbcc0..0000000 --- a/src/DevHive.Services/Services/CommentService.cs +++ /dev/null @@ -1,62 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Models; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Comment; - -namespace DevHive.Services.Services -{ - public class CommentService - { - private readonly CommentRepository _commentRepository; - private readonly IMapper _commentMapper; - - public CommentService(CommentRepository commentRepository, IMapper mapper) - { - this._commentRepository = commentRepository; - this._commentMapper = mapper; - } - - public async Task CreateComment(CommentServiceModel commentServiceModel) - { - Comment comment = this._commentMapper.Map(commentServiceModel); - comment.Date = DateTime.Now; - bool result = await this._commentRepository.AddAsync(comment); - - return result; - } - - public async Task GetCommentById(Guid id) - { - Comment comment = await this._commentRepository.GetByIdAsync(id); - - if(comment == null) - throw new ArgumentException("The comment does not exist"); - - return this._commentMapper.Map(comment); - } - - public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) - { - if (!await this._commentRepository.DoesCommentExist(commentServiceModel.Id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = this._commentMapper.Map(commentServiceModel); - bool result = await this._commentRepository.EditAsync(comment); - - return result; - } - - public async Task DeleteComment(Guid id) - { - if (!await this._commentRepository.DoesCommentExist(id)) - throw new ArgumentException("Comment does not exist!"); - - Comment comment = await this._commentRepository.GetByIdAsync(id); - bool result = await this._commentRepository.DeleteAsync(comment); - - return result; - } - } -} \ No newline at end of file diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs new file mode 100644 index 0000000..0c0fd5c --- /dev/null +++ b/src/DevHive.Services/Services/PostService.cs @@ -0,0 +1,98 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Services +{ + public class PostService + { + private readonly PostRepository _postRepository; + private readonly IMapper _postMapper; + + public PostService(PostRepository postRepository, IMapper postMapper) + { + this._postRepository = postRepository; + this._postMapper = postMapper; + } + + //Create + public async Task CreatePost(CreatePostServiceModel postServiceModel) + { + Post post = this._postMapper.Map(postServiceModel); + + return await this._postRepository.AddAsync(post); + } + + public async Task AddComment(CreateCommentServiceModel commentServiceModel) + { + commentServiceModel.TimeCreated = DateTime.Now; + Comment comment = this._postMapper.Map(commentServiceModel); + + bool result = await this._postRepository.AddCommentAsync(comment); + + return result; + } + + //Read + public async Task GetPostById(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id) + ?? throw new ArgumentException("Post does not exist!"); + + return this._postMapper.Map(post); + } + + public async Task GetCommentById(Guid id) + { + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + + if(comment == null) + throw new ArgumentException("The comment does not exist"); + + return this._postMapper.Map(comment); + } + + //Update + public async Task UpdatePost(UpdatePostServiceModel postServiceModel) + { + if (!await this._postRepository.DoesPostExist(postServiceModel.IssuerId)) + throw new ArgumentException("Comment does not exist!"); + + Post post = this._postMapper.Map(postServiceModel); + return await this._postRepository.EditAsync(post); + } + + public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) + { + if (!await this._postRepository.DoesCommentExist(commentServiceModel.Id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = this._postMapper.Map(commentServiceModel); + bool result = await this._postRepository.EditCommentAsync(comment); + + return result; + } + + //Delete + public async Task DeletePost(Guid id) + { + Post post = await this._postRepository.GetByIdAsync(id); + return await this._postRepository.DeleteAsync(post); + } + + public async Task DeleteComment(Guid id) + { + if (!await this._postRepository.DoesCommentExist(id)) + throw new ArgumentException("Comment does not exist!"); + + Comment comment = await this._postRepository.GetCommentByIdAsync(id); + bool result = await this._postRepository.DeleteCommentAsync(comment); + + return result; + } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index 76d7c6f..2707d91 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -12,13 +12,13 @@ namespace DevHive.Web.Configurations.Extensions services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); - services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs index dd11420..394490e 100644 --- a/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/CommentMappings.cs @@ -1,10 +1,10 @@ using AutoMapper; -using DevHive.Web.Models.Comment; -using DevHive.Services.Models.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Web.Models.Post.Comment; namespace DevHive.Web.Configurations.Mapping { - public class CommentMappings : Profile + public class CommentMappings : Profile { public CommentMappings() { @@ -12,7 +12,6 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); CreateMap(); CreateMap(); - CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/CommentController.cs b/src/DevHive.Web/Controllers/CommentController.cs deleted file mode 100644 index 5b6b0ee..0000000 --- a/src/DevHive.Web/Controllers/CommentController.cs +++ /dev/null @@ -1,72 +0,0 @@ -using System; -using System.Threading.Tasks; -using AutoMapper; -using DevHive.Data.Repositories; -using DevHive.Services.Models.Comment; -using DevHive.Services.Services; -using DevHive.Web.Models.Comment; -using Microsoft.AspNetCore.Mvc; - -namespace DevHive.Web.Controllers -{ - [ApiController] - [Route("/api/[controller]")] - public class CommentController - { - private readonly CommentService _commentService; - private readonly IMapper _commentMapper; - - public CommentController(CommentService commentService, IMapper mapper) - { - this._commentService = commentService; - this._commentMapper = mapper; - } - - [HttpPost] - public async Task Create([FromBody] CommentWebModel commentWebModel) - { - CommentServiceModel commentServiceModel = this._commentMapper.Map(commentWebModel); - - bool result = await this._commentService.CreateComment(commentServiceModel); - - if(!result) - return new BadRequestObjectResult("Could not create the Comment"); - - return new OkResult(); - } - - [HttpGet] - public async Task GetById(Guid id) - { - GetByIdCommentServiceModel getByIdCommentServiceModel = await this._commentService.GetCommentById(id); - GetByIdCommentWebModel getByIdCommentWebModel = this._commentMapper.Map(getByIdCommentServiceModel); - - return new OkObjectResult(getByIdCommentWebModel); - } - - [HttpPut] - public async Task Update(Guid id, [FromBody] CommentWebModel commentWebModel) - { - UpdateCommentServiceModel updateCommentServiceModel = this._commentMapper.Map(commentWebModel); - updateCommentServiceModel.Id = id; - - bool result = await this._commentService.UpdateComment(updateCommentServiceModel); - - if (!result) - return new BadRequestObjectResult("Could not update Comment"); - - return new OkResult(); - } - - [HttpDelete] - public async Task Delete(Guid id) - { - bool result = await this._commentService.DeleteComment(id); - - if (!result) - return new BadRequestObjectResult("Could not delete Comment"); - - return new OkResult(); - } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/PostController.cs b/src/DevHive.Web/Controllers/PostController.cs new file mode 100644 index 0000000..397ddbc --- /dev/null +++ b/src/DevHive.Web/Controllers/PostController.cs @@ -0,0 +1,132 @@ +using System.Threading.Tasks; +using DevHive.Services.Services; +using Microsoft.AspNetCore.Mvc; +using AutoMapper; +using System; +using DevHive.Web.Models.Post.Post; +using DevHive.Services.Models.Post.Post; +using DevHive.Web.Models.Post.Comment; +using DevHive.Services.Models.Post.Comment; +using DevHive.Common.Models.Misc; + +namespace DevHive.Web.Controllers +{ + [ApiController] + [Route("/api/[controller]")] + //[Authorize(Posts = "Admin")] + public class PostController + { + private readonly PostService _postService; + private readonly IMapper _postMapper; + + public PostController(PostService postService, IMapper mapper) + { + this._postService = postService; + this._postMapper = mapper; + } + + //Create + [HttpPost] + public async Task Create([FromBody] CreatePostWebModel createPostModel) + { + CreatePostServiceModel postServiceModel = + this._postMapper.Map(createPostModel); + + bool result = await this._postService.CreatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not create post!"); + + return new OkResult(); + } + + [HttpPost] + [Route("Comment")] + public async Task AddComment([FromBody] CommentWebModel commentWebModel) + { + CommentServiceModel commentServiceModel = this._postMapper.Map(commentWebModel); + + bool result = await this._postService.AddComment(commentServiceModel); + + if(!result) + return new BadRequestObjectResult("Could not create the Comment"); + + return new OkResult(); + } + + //Read + [HttpGet] + public async Task GetById(Guid id) + { + PostServiceModel postServiceModel = await this._postService.GetPostById(id); + PostWebModel postWebModel = this._postMapper.Map(postServiceModel); + + return new OkObjectResult(postWebModel); + } + + [HttpGet] + [Route("Comment")] + public async Task GetCommentById(Guid id) + { + CommentServiceModel commentServiceModel = await this._postService.GetCommentById(id); + IdModel idModel = this._postMapper.Map(commentServiceModel); + + return new OkObjectResult(idModel); + } + + //Update + [HttpPut] + public async Task Update(Guid id, [FromBody] UpdatePostWebModel updatePostModel) + { + UpdatePostServiceModel postServiceModel = + this._postMapper.Map(updatePostModel); + postServiceModel.IssuerId = id; + + bool result = await this._postService.UpdatePost(postServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update post!"); + + return new OkResult(); + } + + [HttpPut] + [Route("Comment")] + public async Task UpdateComment(Guid id, [FromBody] CommentWebModel commentWebModel) + { + UpdateCommentServiceModel updateCommentServiceModel = this._postMapper.Map(commentWebModel); + updateCommentServiceModel.Id = id; + + bool result = await this._postService.UpdateComment(updateCommentServiceModel); + + if (!result) + return new BadRequestObjectResult("Could not update Comment"); + + return new OkResult(); + } + + //Delete + [HttpDelete] + public async Task Delete(Guid id) + { + bool result = await this._postService.DeletePost(id); + + if (!result) + return new BadRequestObjectResult("Could not delete post!"); + + return new OkResult(); + } + + [HttpDelete] + [Route("Comment")] + public async Task DeleteComment(Guid id) + { + bool result = await this._postService.DeleteComment(id); + + if (!result) + return new BadRequestObjectResult("Could not delete Comment"); + + return new OkResult(); + } + } +} diff --git a/src/DevHive.Web/Controllers/RoleController.cs b/src/DevHive.Web/Controllers/RoleController.cs index 6b81ab8..d710f5a 100644 --- a/src/DevHive.Web/Controllers/RoleController.cs +++ b/src/DevHive.Web/Controllers/RoleController.cs @@ -67,7 +67,7 @@ namespace DevHive.Web.Controllers bool result = await this._roleService.DeleteRole(id); if (!result) - return new BadRequestObjectResult("Could nor delete role!"); + return new BadRequestObjectResult("Could not delete role!"); return new OkResult(); } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 02cae0c..ce972a5 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -9,7 +9,7 @@ using DevHive.Web.Models.Identity.User; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Common.Models.Identity; -using DevHive.Common.Models.Data; +using DevHive.Common.Models.Misc; namespace DevHive.Web.Controllers { diff --git a/src/DevHive.Web/Models/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Comment/CommentWebModel.cs deleted file mode 100644 index 37806a5..0000000 --- a/src/DevHive.Web/Models/Comment/CommentWebModel.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Comment -{ - public class CommentWebModel - { - public Guid UserId { get; set; } - public string Message { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs b/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs deleted file mode 100644 index 3d03348..0000000 --- a/src/DevHive.Web/Models/Comment/GetByIdCommentWebModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Web.Models.Comment -{ - public class GetByIdCommentWebModel : CommentWebModel - { - public DateTime Date { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs new file mode 100644 index 0000000..2a8650a --- /dev/null +++ b/src/DevHive.Web/Models/Post/Comment/CommentWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Post.Comment +{ + public class CommentWebModel + { + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs new file mode 100644 index 0000000..caa9925 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/BasePostWebModel.cs @@ -0,0 +1,10 @@ +using System; + +namespace DevHive.Web.Models.Post.Post +{ + public class BasePostWebModel + { + public Guid IssuerId { get; set; } + public string Message { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs new file mode 100644 index 0000000..7558b2c --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/CreatePostWebModel.cs @@ -0,0 +1,8 @@ +using System; + +namespace DevHive.Web.Models.Post.Post +{ + public class CreatePostWebModel : BasePostWebModel + { + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/PostWebModel.cs b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs new file mode 100644 index 0000000..fa18c3a --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/PostWebModel.cs @@ -0,0 +1,21 @@ +using DevHive.Web.Models.Post.Comment; + +namespace DevHive.Web.Models.Post.Post +{ + public class PostWebModel + { + //public string Picture { get; set; } + + public string IssuerFirstName { get; set; } + + public string IssuerLastName { get; set; } + + public string IssuerUsername { get; set; } + + public string Message { get; set; } + + //public Files[] Files { get; set; } + + public CommentWebModel[] Comments { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs b/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs new file mode 100644 index 0000000..c774900 --- /dev/null +++ b/src/DevHive.Web/Models/Post/Post/UpdatePostModel.cs @@ -0,0 +1,7 @@ +namespace DevHive.Web.Models.Post.Post +{ + public class UpdatePostWebModel : BasePostWebModel + { + //public Files[] Files { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Startup.cs b/src/DevHive.Web/Startup.cs index de1295c..42fc88a 100644 --- a/src/DevHive.Web/Startup.cs +++ b/src/DevHive.Web/Startup.cs @@ -1,14 +1,10 @@ -using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using DevHive.Web.Configurations.Extensions; -using AutoMapper; using Newtonsoft.Json; -using DevHive.Data.Repositories; -using DevHive.Services.Services; namespace DevHive.Web { -- cgit v1.2.3 From 4f838ea0411445f6d7757273515062f2faac1ee7 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Mon, 11 Jan 2021 22:17:07 +0200 Subject: Added Interfaces for all repository classes --- .../Repositories/Contracts/ILanguageRepository.cs | 13 +++++ .../Repositories/Contracts/IPostRepository.cs | 21 +++++++ .../Repositories/Contracts/IRepository.cs | 21 +++++++ .../Repositories/Contracts/IRoleRepository.cs | 15 +++++ .../Contracts/ITechnologyRepository.cs | 13 +++++ .../Repositories/Contracts/IUserRepository.cs | 26 +++++++++ src/DevHive.Data/Repositories/IRepository.cs | 21 ------- .../Repositories/LanguageRepository.cs | 4 +- src/DevHive.Data/Repositories/PostRepository.cs | 4 +- src/DevHive.Data/Repositories/RoleRepository.cs | 4 +- .../Repositories/TechnologyRepository.cs | 6 +- src/DevHive.Data/Repositories/UserRepository.cs | 4 +- src/DevHive.Services/Services/LanguageService.cs | 6 +- src/DevHive.Services/Services/PostService.cs | 8 +-- src/DevHive.Services/Services/RoleService.cs | 6 +- src/DevHive.Services/Services/TechnologyService.cs | 6 +- src/DevHive.Services/Services/UserService.cs | 8 +-- .../TechnologyRepository.Tests.cs | 1 - .../DevHive.Services.Tests.csproj | 2 + .../TechnologyService.Tests.cs | 64 ++++++++++++++++++++++ .../DevHive.Services.Tests/UnitTest1.cs | 18 ------ 21 files changed, 203 insertions(+), 68 deletions(-) create mode 100644 src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs create mode 100644 src/DevHive.Data/Repositories/Contracts/IPostRepository.cs create mode 100644 src/DevHive.Data/Repositories/Contracts/IRepository.cs create mode 100644 src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs create mode 100644 src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs create mode 100644 src/DevHive.Data/Repositories/Contracts/IUserRepository.cs delete mode 100644 src/DevHive.Data/Repositories/IRepository.cs create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs delete mode 100644 src/DevHive.Tests/DevHive.Services.Tests/UnitTest1.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs b/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs new file mode 100644 index 0000000..e44d27b --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs @@ -0,0 +1,13 @@ +using DevHive.Data.Models; +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface ILanguageRepository : IRepository + { + public Task DoesLanguageNameExist(string languageName); + + public Task DoesLanguageExist(Guid id); + } +} diff --git a/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs b/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs new file mode 100644 index 0000000..930138a --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs @@ -0,0 +1,21 @@ +using DevHive.Data.Models; +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface IPostRepository : IRepository + { + public Task AddCommentAsync(Comment entity); + + public Task GetCommentByIdAsync(Guid id); + + public Task EditCommentAsync(Comment newEntity); + + public Task DeleteCommentAsync(Comment entity); + + public Task DoesPostExist(Guid postId); + + public Task DoesCommentExist(Guid id); + } +} diff --git a/src/DevHive.Data/Repositories/Contracts/IRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRepository.cs new file mode 100644 index 0000000..37c5170 --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface IRepository + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Find entity by id + Task GetByIdAsync(Guid id); + + //Modify Entity from database + Task EditAsync(TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(TEntity entity); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs new file mode 100644 index 0000000..6cb8a4e --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs @@ -0,0 +1,15 @@ +using DevHive.Data.Models; +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface IRoleRepository : IRepository + { + public Task GetByNameAsync(string name); + + public Task DoesNameExist(string name); + + public Task DoesRoleExist(Guid id); + } +} diff --git a/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs b/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs new file mode 100644 index 0000000..3c4a6b6 --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs @@ -0,0 +1,13 @@ +using DevHive.Data.Models; +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface ITechnologyRepository : IRepository + { + public Task DoesTechnologyNameExist(string technologyName); + + public Task DoesTechnologyExist(Guid id); + } +} diff --git a/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs b/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs new file mode 100644 index 0000000..74c4486 --- /dev/null +++ b/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs @@ -0,0 +1,26 @@ +using DevHive.Data.Models; +using System; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Contracts +{ + public interface IUserRepository : IRepository + { + public Task AddFriendAsync(User user, User friend); + + public IEnumerable QueryAll(); + + public Task GetByUsername(string username); + + public Task RemoveFriendAsync(User user, User friend); + + public bool DoesUserExist(Guid id); + + public bool DoesUserHaveThisUsername(Guid id, string username); + + public Task DoesUsernameExist(string username); + + public Task DoesEmailExist(string email); + } +} diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs deleted file mode 100644 index 96762b9..0000000 --- a/src/DevHive.Data/Repositories/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace Data.Models.Interfaces.Database -{ - public interface IRepository - where TEntity : class - { - //Add Entity to database - Task AddAsync(TEntity entity); - - //Find entity by id - Task GetByIdAsync(Guid id); - - //Modify Entity from database - Task EditAsync(TEntity newEntity); - - //Delete Entity from database - Task DeleteAsync(TEntity entity); - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 1ab870a..243192a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,13 +1,13 @@ using System; using System.Threading.Tasks; -using Data.Models.Interfaces.Database; using DevHive.Common.Models.Misc; using DevHive.Data.Models; +using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : IRepository + public class LanguageRepository : ILanguageRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 5dfee0b..0acfc23 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,13 +1,13 @@ using System; using System.Threading.Tasks; -using Data.Models.Interfaces.Database; using DevHive.Common.Models.Misc; using DevHive.Data.Models; +using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IRepository + public class PostRepository : IPostRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index fd04866..d6f83a8 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,13 +1,13 @@ using System; using System.Threading.Tasks; -using Data.Models.Interfaces.Database; using DevHive.Common.Models.Misc; using DevHive.Data.Models; +using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRepository + public class RoleRepository : IRoleRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 492c6d2..27918ca 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,16 +1,16 @@ using System; using System.Threading.Tasks; -using Data.Models.Interfaces.Database; using DevHive.Common.Models.Misc; using DevHive.Data.Models; +using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class TechnologyRepository : IRepository + public abstract class TechnologyRepository : ITechnologyRepository { - private readonly DevHiveContext _context; + private DevHiveContext _context; public TechnologyRepository(DevHiveContext context) { diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5600451..5142b82 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -2,14 +2,14 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Data.Models.Interfaces.Database; using DevHive.Common.Models.Misc; using DevHive.Data.Models; +using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IRepository + public class UserRepository : IUserRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 3493292..3c011c6 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -2,17 +2,17 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Data.Repositories.Contracts; using DevHive.Services.Models.Language; namespace DevHive.Services.Services { public class LanguageService { - private readonly LanguageRepository _languageRepository; + private readonly ILanguageRepository _languageRepository; private readonly IMapper _languageMapper; - public LanguageService(LanguageRepository languageRepository, IMapper mapper) + public LanguageService(ILanguageRepository languageRepository, IMapper mapper) { this._languageRepository = languageRepository; this._languageMapper = mapper; diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index b2ea694..e0a2be9 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -3,21 +3,21 @@ using System.Collections.Generic; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories; using DevHive.Services.Models.Post.Comment; using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; +using DevHive.Data.Repositories.Contracts; namespace DevHive.Services.Services { public class PostService { - private readonly PostRepository _postRepository; - private readonly UserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; private readonly IMapper _postMapper; - public PostService(PostRepository postRepository, UserRepository userRepository , IMapper postMapper) + public PostService(IPostRepository postRepository, IUserRepository userRepository , IMapper postMapper) { this._postRepository = postRepository; this._userRepository = userRepository; diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 7ba0a98..372984d 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -3,16 +3,16 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Data.Repositories.Contracts; namespace DevHive.Services.Services { public class RoleService { - private readonly RoleRepository _roleRepository; + private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; - public RoleService(RoleRepository roleRepository, IMapper mapper) + public RoleService(IRoleRepository roleRepository, IMapper mapper) { this._roleRepository = roleRepository; this._roleMapper = mapper; diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 2913a55..e03d4d1 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -2,17 +2,17 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Data.Repositories.Contracts; using DevHive.Services.Models.Technology; namespace DevHive.Services.Services { public class TechnologyService { - private readonly TechnologyRepository _technologyRepository; + private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _technologyMapper; - public TechnologyService(TechnologyRepository technologyRepository, IMapper technologyMapper) + public TechnologyService(ITechnologyRepository technologyRepository, IMapper technologyMapper) { this._technologyRepository = technologyRepository; this._technologyMapper = technologyMapper; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 7fd0682..c8bcab9 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -1,5 +1,4 @@ using AutoMapper; -using DevHive.Data.Repositories; using DevHive.Services.Options; using DevHive.Services.Models.Identity.User; using System.Threading.Tasks; @@ -12,17 +11,18 @@ using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; +using DevHive.Data.Repositories.Contracts; namespace DevHive.Services.Services { public class UserService { - private readonly UserRepository _userRepository; - private readonly RoleRepository _roleRepository; + private readonly IUserRepository _userRepository; + private readonly IRoleRepository _roleRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; - public UserService(UserRepository userRepository, RoleRepository roleRepository, IMapper mapper, JWTOptions jwtOptions) + public UserService(IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper, JWTOptions jwtOptions) { this._userRepository = userRepository; this._roleRepository = roleRepository; diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index fcec727..ee0ca4b 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -132,7 +132,6 @@ namespace DevHive.Data.Tests } #endregion - //TO DO #region EditAsync [Test] public void EditAsync_UpdatesEntity() diff --git a/src/DevHive.Tests/DevHive.Services.Tests/DevHive.Services.Tests.csproj b/src/DevHive.Tests/DevHive.Services.Tests/DevHive.Services.Tests.csproj index 7cc8473..fb94b61 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/DevHive.Services.Tests.csproj +++ b/src/DevHive.Tests/DevHive.Services.Tests/DevHive.Services.Tests.csproj @@ -7,6 +7,8 @@ + + diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs new file mode 100644 index 0000000..d38146f --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs @@ -0,0 +1,64 @@ +using AutoMapper; +using DevHive.Data; +using DevHive.Data.Models; +using DevHive.Data.Repositories; +using DevHive.Data.Repositories.Contracts; +using DevHive.Services.Models.Technology; +using DevHive.Services.Services; +using Microsoft.EntityFrameworkCore; +using Moq; +using NUnit.Framework; +using System.Threading.Tasks; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class TechnologyServiceTests + { + protected Mock TechnologyRepositoryMock { get; set; } + protected Mock MapperMock { get; set; } + protected TechnologyService TechnologyService { get; set; } + + [SetUp] + public void Setup() + { + this.TechnologyRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.TechnologyService = new TechnologyService(this.TechnologyRepositoryMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + public void Create_ReturnsTrue_WhenEntityIsAddedSuccessfully() + { + Task.Run(async () => + { + TechnologyServiceModel technologyServiceModel = new TechnologyServiceModel + { + Name = "Some name" + }; + Technology technology = new Technology + { + Name = "Some name" + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + bool result = await this.TechnologyService.Create(technologyServiceModel); + + Assert.IsTrue(result, "Create returns false when entity is created successfully"); + }).GetAwaiter().GetResult(); + } + + + #endregion + + [Test] + public void Test() + { + Assert.IsTrue(true); + } + } +} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/UnitTest1.cs b/src/DevHive.Tests/DevHive.Services.Tests/UnitTest1.cs deleted file mode 100644 index b6681da..0000000 --- a/src/DevHive.Tests/DevHive.Services.Tests/UnitTest1.cs +++ /dev/null @@ -1,18 +0,0 @@ -using NUnit.Framework; - -namespace DevHive.Services.Tests -{ - public class Tests - { - [SetUp] - public void Setup() - { - } - - [Test] - public void Test1() - { - Assert.Pass(); - } - } -} \ No newline at end of file -- cgit v1.2.3 From 11bd1d9a9760c7bc6a601d78b3d89ec9028647a2 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 12 Jan 2021 13:16:39 +0200 Subject: Language layers refactored; User implements adding & removing Languages; Migrations added --- ...112111416_User_Implements_Languages.Designer.cs | 405 +++++++++++++++++++++ .../20210112111416_User_Implements_Languages.cs | 106 ++++++ .../Migrations/DevHiveContextModelSnapshot.cs | 38 +- src/DevHive.Data/Models/Technology.cs | 1 - src/DevHive.Data/Models/User.cs | 12 +- .../Repositories/Contracts/ILanguageRepository.cs | 13 - .../Repositories/Contracts/IPostRepository.cs | 21 -- .../Repositories/Contracts/IRepository.cs | 21 -- .../Repositories/Contracts/IRoleRepository.cs | 15 - .../Contracts/ITechnologyRepository.cs | 13 - .../Repositories/Contracts/IUserRepository.cs | 26 -- src/DevHive.Data/Repositories/IRepository.cs | 21 ++ .../Repositories/LanguageRepository.cs | 55 +-- src/DevHive.Data/Repositories/PostRepository.cs | 6 +- src/DevHive.Data/Repositories/RoleRepository.cs | 3 +- .../Repositories/TechnologyRepository.cs | 5 +- src/DevHive.Data/Repositories/UserRepository.cs | 162 +++++++-- .../Configurations/Mapping/LanguageMappings.cs | 1 - .../Models/Identity/User/RegisterServiceModel.cs | 8 +- .../Models/Language/CreateLanguageServiceModel.cs | 9 + .../Models/Language/LanguageServiceModel.cs | 4 +- .../Models/Language/UpdateLanguageServiceModel.cs | 7 +- src/DevHive.Services/Services/LanguageService.cs | 38 +- src/DevHive.Services/Services/PostService.cs | 8 +- src/DevHive.Services/Services/RoleService.cs | 6 +- src/DevHive.Services/Services/TechnologyService.cs | 6 +- src/DevHive.Services/Services/UserService.cs | 146 +++++--- .../TechnologyRepository.Tests.cs | 4 +- .../Configurations/Mapping/LanguageMappings.cs | 7 +- src/DevHive.Web/Controllers/LanguageController.cs | 4 +- .../Models/Language/CreateLanguageWebModel.cs | 9 + .../Models/Language/LanguageWebModel.cs | 4 +- .../Models/Language/UpdateLanguageWebModel.cs | 5 +- 33 files changed, 912 insertions(+), 277 deletions(-) create mode 100644 src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs create mode 100644 src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IPostRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IUserRepository.cs create mode 100644 src/DevHive.Data/Repositories/IRepository.cs create mode 100644 src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs create mode 100644 src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs new file mode 100644 index 0000000..0f1aa80 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.Designer.cs @@ -0,0 +1,405 @@ +// +using System; +using DevHive.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +namespace DevHive.Data.Migrations +{ + [DbContext(typeof(DevHiveContext))] + [Migration("20210112111416_User_Implements_Languages")] + partial class User_Implements_Languages + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .UseIdentityByDefaultColumns() + .HasAnnotation("Relational:MaxIdentifierLength", 63) + .HasAnnotation("ProductVersion", "5.0.1"); + + modelBuilder.Entity("DevHive.Data.Models.Comment", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("IssuerId") + .HasColumnType("uuid"); + + b.Property("Message") + .HasColumnType("text"); + + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); + + b.HasKey("Id"); + + b.ToTable("Comments"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Languages"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Role", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Name") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedName") + .IsUnique() + .HasDatabaseName("RoleNameIndex"); + + b.ToTable("AspNetRoles"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("Technologies"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccessFailedCount") + .HasColumnType("integer"); + + b.Property("ConcurrencyStamp") + .IsConcurrencyToken() + .HasColumnType("text"); + + b.Property("Email") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("EmailConfirmed") + .HasColumnType("boolean"); + + b.Property("FirstName") + .HasColumnType("text"); + + b.Property("LastName") + .HasColumnType("text"); + + b.Property("LockoutEnabled") + .HasColumnType("boolean"); + + b.Property("LockoutEnd") + .HasColumnType("timestamp with time zone"); + + b.Property("NormalizedEmail") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("NormalizedUserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.Property("PasswordHash") + .HasColumnType("text"); + + b.Property("PhoneNumber") + .HasColumnType("text"); + + b.Property("PhoneNumberConfirmed") + .HasColumnType("boolean"); + + b.Property("ProfilePictureUrl") + .HasColumnType("text"); + + b.Property("SecurityStamp") + .HasColumnType("text"); + + b.Property("TwoFactorEnabled") + .HasColumnType("boolean"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("UserName") + .HasMaxLength(256) + .HasColumnType("character varying(256)"); + + b.HasKey("Id"); + + b.HasIndex("NormalizedEmail") + .HasDatabaseName("EmailIndex"); + + b.HasIndex("NormalizedUserName") + .IsUnique() + .HasDatabaseName("UserNameIndex"); + + b.HasIndex("UserId"); + + b.HasIndex("UserName") + .IsUnique(); + + b.ToTable("AspNetUsers"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetRoleClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .UseIdentityByDefaultColumn(); + + b.Property("ClaimType") + .HasColumnType("text"); + + b.Property("ClaimValue") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("Id"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserClaims"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("ProviderKey") + .HasColumnType("text"); + + b.Property("ProviderDisplayName") + .HasColumnType("text"); + + b.Property("UserId") + .HasColumnType("uuid"); + + b.HasKey("LoginProvider", "ProviderKey"); + + b.HasIndex("UserId"); + + b.ToTable("AspNetUserLogins"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("RoleId") + .HasColumnType("uuid"); + + b.HasKey("UserId", "RoleId"); + + b.HasIndex("RoleId"); + + b.ToTable("AspNetUserRoles"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.Property("UserId") + .HasColumnType("uuid"); + + b.Property("LoginProvider") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("Value") + .HasColumnType("text"); + + b.HasKey("UserId", "LoginProvider", "Name"); + + b.ToTable("AspNetUserTokens"); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.Property("RolesId") + .HasColumnType("uuid"); + + b.Property("UsersId") + .HasColumnType("uuid"); + + b.HasKey("RolesId", "UsersId"); + + b.HasIndex("UsersId"); + + b.ToTable("RoleUser"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Langauges") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Technologies") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Friends") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RoleId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("RoleUser", b => + { + b.HasOne("DevHive.Data.Models.Role", null) + .WithMany() + .HasForeignKey("RolesId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("DevHive.Data.Models.User", null) + .WithMany() + .HasForeignKey("UsersId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("DevHive.Data.Models.User", b => + { + b.Navigation("Friends"); + + b.Navigation("Langauges"); + + b.Navigation("Technologies"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs new file mode 100644 index 0000000..a51ad09 --- /dev/null +++ b/src/DevHive.Data/Migrations/20210112111416_User_Implements_Languages.cs @@ -0,0 +1,106 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +namespace DevHive.Data.Migrations +{ + public partial class User_Implements_Languages : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.RenameColumn( + name: "UserId", + table: "Comments", + newName: "IssuerId"); + + migrationBuilder.RenameColumn( + name: "Date", + table: "Comments", + newName: "TimeCreated"); + + migrationBuilder.RenameColumn( + name: "ProfilePicture", + table: "AspNetUsers", + newName: "ProfilePictureUrl"); + + migrationBuilder.AddColumn( + name: "UserId", + table: "Technologies", + type: "uuid", + nullable: true); + + migrationBuilder.AddColumn( + name: "UserId", + table: "Languages", + type: "uuid", + nullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Technologies_UserId", + table: "Technologies", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_Languages_UserId", + table: "Languages", + column: "UserId"); + + migrationBuilder.AddForeignKey( + name: "FK_Languages_AspNetUsers_UserId", + table: "Languages", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Technologies_AspNetUsers_UserId", + table: "Technologies", + column: "UserId", + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Languages_AspNetUsers_UserId", + table: "Languages"); + + migrationBuilder.DropForeignKey( + name: "FK_Technologies_AspNetUsers_UserId", + table: "Technologies"); + + migrationBuilder.DropIndex( + name: "IX_Technologies_UserId", + table: "Technologies"); + + migrationBuilder.DropIndex( + name: "IX_Languages_UserId", + table: "Languages"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Technologies"); + + migrationBuilder.DropColumn( + name: "UserId", + table: "Languages"); + + migrationBuilder.RenameColumn( + name: "TimeCreated", + table: "Comments", + newName: "Date"); + + migrationBuilder.RenameColumn( + name: "IssuerId", + table: "Comments", + newName: "UserId"); + + migrationBuilder.RenameColumn( + name: "ProfilePictureUrl", + table: "AspNetUsers", + newName: "ProfilePicture"); + } + } +} diff --git a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs index c7ff6c6..cc6d24d 100644 --- a/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs +++ b/src/DevHive.Data/Migrations/DevHiveContextModelSnapshot.cs @@ -25,14 +25,14 @@ namespace DevHive.Data.Migrations .ValueGeneratedOnAdd() .HasColumnType("uuid"); - b.Property("Date") - .HasColumnType("timestamp without time zone"); + b.Property("IssuerId") + .HasColumnType("uuid"); b.Property("Message") .HasColumnType("text"); - b.Property("UserId") - .HasColumnType("uuid"); + b.Property("TimeCreated") + .HasColumnType("timestamp without time zone"); b.HasKey("Id"); @@ -48,8 +48,13 @@ namespace DevHive.Data.Migrations b.Property("Name") .HasColumnType("text"); + b.Property("UserId") + .HasColumnType("uuid"); + b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Languages"); }); @@ -89,8 +94,13 @@ namespace DevHive.Data.Migrations b.Property("Name") .HasColumnType("text"); + b.Property("UserId") + .HasColumnType("uuid"); + b.HasKey("Id"); + b.HasIndex("UserId"); + b.ToTable("Technologies"); }); @@ -143,7 +153,7 @@ namespace DevHive.Data.Migrations b.Property("PhoneNumberConfirmed") .HasColumnType("boolean"); - b.Property("ProfilePicture") + b.Property("ProfilePictureUrl") .HasColumnType("text"); b.Property("SecurityStamp") @@ -292,6 +302,20 @@ namespace DevHive.Data.Migrations b.ToTable("RoleUser"); }); + modelBuilder.Entity("DevHive.Data.Models.Language", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Langauges") + .HasForeignKey("UserId"); + }); + + modelBuilder.Entity("DevHive.Data.Models.Technology", b => + { + b.HasOne("DevHive.Data.Models.User", null) + .WithMany("Technologies") + .HasForeignKey("UserId"); + }); + modelBuilder.Entity("DevHive.Data.Models.User", b => { b.HasOne("DevHive.Data.Models.User", null) @@ -368,6 +392,10 @@ namespace DevHive.Data.Migrations modelBuilder.Entity("DevHive.Data.Models.User", b => { b.Navigation("Friends"); + + b.Navigation("Langauges"); + + b.Navigation("Technologies"); }); #pragma warning restore 612, 618 } diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index 2e0aeed..a462d20 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -5,7 +5,6 @@ namespace DevHive.Data.Models public class Technology : IModel { public Guid Id { get; set; } - public string Name { get; set; } } } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index eef0af2..fda4651 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -12,7 +12,17 @@ namespace DevHive.Data.Models public string LastName { get; set; } - public string ProfilePicture { get; set; } + public string ProfilePictureUrl { get; set; } + + /// + /// Languages that the user uses or is familiar with + /// + public IList Langauges { get; set; } + + /// + /// Technologies that the user uses or is familiar with + /// + public IList Technologies { get; set; } public virtual IList Roles { get; set; } = new List(); diff --git a/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs b/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs deleted file mode 100644 index e44d27b..0000000 --- a/src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface ILanguageRepository : IRepository - { - public Task DoesLanguageNameExist(string languageName); - - public Task DoesLanguageExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs b/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs deleted file mode 100644 index 930138a..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IPostRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IPostRepository : IRepository - { - public Task AddCommentAsync(Comment entity); - - public Task GetCommentByIdAsync(Guid id); - - public Task EditCommentAsync(Comment newEntity); - - public Task DeleteCommentAsync(Comment entity); - - public Task DoesPostExist(Guid postId); - - public Task DoesCommentExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRepository.cs deleted file mode 100644 index 37c5170..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IRepository - where TEntity : class - { - //Add Entity to database - Task AddAsync(TEntity entity); - - //Find entity by id - Task GetByIdAsync(Guid id); - - //Modify Entity from database - Task EditAsync(TEntity newEntity); - - //Delete Entity from database - Task DeleteAsync(TEntity entity); - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs deleted file mode 100644 index 6cb8a4e..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs +++ /dev/null @@ -1,15 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IRoleRepository : IRepository - { - public Task GetByNameAsync(string name); - - public Task DoesNameExist(string name); - - public Task DoesRoleExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs b/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs deleted file mode 100644 index 3c4a6b6..0000000 --- a/src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs +++ /dev/null @@ -1,13 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface ITechnologyRepository : IRepository - { - public Task DoesTechnologyNameExist(string technologyName); - - public Task DoesTechnologyExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs b/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs deleted file mode 100644 index 74c4486..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IUserRepository.cs +++ /dev/null @@ -1,26 +0,0 @@ -using DevHive.Data.Models; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - public interface IUserRepository : IRepository - { - public Task AddFriendAsync(User user, User friend); - - public IEnumerable QueryAll(); - - public Task GetByUsername(string username); - - public Task RemoveFriendAsync(User user, User friend); - - public bool DoesUserExist(Guid id); - - public bool DoesUserHaveThisUsername(Guid id, string username); - - public Task DoesUsernameExist(string username); - - public Task DoesEmailExist(string email); - } -} diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs new file mode 100644 index 0000000..920ba13 --- /dev/null +++ b/src/DevHive.Data/Repositories/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories +{ + public interface IRepository + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Find entity by id + Task GetByIdAsync(Guid id); + + //Modify Entity from database + Task EditAsync(TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(TEntity entity); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 243192a..5d8217a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,13 +1,13 @@ using System; +using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : ILanguageRepository + public class LanguageRepository : IRepository { private readonly DevHiveContext _context; @@ -16,7 +16,8 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task AddAsync(Language entity) { await this._context @@ -25,42 +26,32 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Read - //Read public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } + #endregion - public async Task DoesLanguageNameExist(string languageName) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Name == languageName); - } - - public async Task DoesLanguageExist(Guid id) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } + #region Update - //Update public async Task EditAsync(Language newEntity) { - this._context - .Set() - .Update(newEntity); + this._context + .Set() + .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Delete - //Delete public async Task DeleteAsync(Language entity) { this._context @@ -69,5 +60,21 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Validations + + public async Task DoesLanguageNameExistAsync(string languageName) + { + return await this._context.Languages + .AnyAsync(r => r.Name == languageName); + } + + public async Task DoesLanguageExistAsync(Guid id) + { + return await this._context.Languages + .AnyAsync(r => r.Id == id); + } + #endregion } } \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 0acfc23..002fb17 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -2,12 +2,11 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : IRepository { private readonly DevHiveContext _context; @@ -88,6 +87,8 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #region Validations + public async Task DoesPostExist(Guid postId) { return await this._context @@ -103,5 +104,6 @@ namespace DevHive.Data.Repositories .AsNoTracking() .AnyAsync(r => r.Id == id); } + #endregion } } \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index d6f83a8..0ca1646 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -2,12 +2,11 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRoleRepository + public class RoleRepository : IRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 27918ca..2ed3a23 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -2,13 +2,12 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public abstract class TechnologyRepository : ITechnologyRepository + public abstract class TechnologyRepository : IRepository { private DevHiveContext _context; @@ -27,7 +26,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Read + //Read public async Task GetByIdAsync(Guid id) { return await this._context diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5142b82..e3c1304 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -4,12 +4,11 @@ using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IUserRepository + public class UserRepository : IRepository { private readonly DevHiveContext _context; @@ -18,11 +17,11 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task AddAsync(User entity) { - await this._context - .Set() + await this._context.Users .AddAsync(entity); return await RepositoryMethods.SaveChangesAsync(this._context); @@ -35,12 +34,31 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - - //Read + + public async Task AddLanguageToUserAsync(User user, Language language) + { + this._context.Update(user); + + user.Langauges.Add(language); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddTechnologyToUserAsync(User user, Technology technology) + { + this._context.Update(user); + + user.Technologies.Add(technology); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Read + public IEnumerable QueryAll() { - return this._context - .Set() + return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); @@ -48,8 +66,7 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Users .Include(x => x.Roles) .Include(x => x.Friends) .FirstOrDefaultAsync(x => x.Id == id); @@ -57,13 +74,36 @@ namespace DevHive.Data.Repositories public async Task GetByUsername(string username) { - return await this._context - .Set() + return await this._context.Users .Include(u => u.Roles) .FirstOrDefaultAsync(x => x.UserName == username); } - //Update + public IList GetUserLanguages(User user) + { + return user.Langauges; + } + + public Language GetUserLanguage(User user, Language language) + { + return user.Langauges + .FirstOrDefault(x => x.Id == language.Id); + } + + public IList GetUserTechnologies(User user) + { + return user.Technologies; + } + + public Technology GetUserTechnology(User user, Technology technology) + { + return user.Technologies + .FirstOrDefault(x => x.Id == technology.Id); + } + #endregion + + #region Update + public async Task EditAsync(User newEntity) { User user = await this.GetByIdAsync(newEntity.Id); @@ -76,11 +116,32 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Delete + public async Task EditUserLanguage(User user, Language oldLang, Language newLang) + { + this._context.Update(user); + + user.Langauges.Remove(oldLang); + user.Langauges.Add(newLang); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task EditUserTechnologies(User user, Technology oldTech, Technology newTech) + { + this._context.Update(user); + + user.Technologies.Remove(oldTech); + user.Technologies.Add(newTech); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Delete + public async Task DeleteAsync(User entity) { - this._context - .Set() + this._context.Users .Remove(entity); return await RepositoryMethods.SaveChangesAsync(this._context); @@ -93,37 +154,70 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - - //Validations - public bool DoesUserExist(Guid id) + + public async Task RemoveLanguageFromUserAsync(User user, Language language) { - return this._context - .Set() - .Any(x => x.Id == id); + this._context.Update(user); + + user.Langauges.Remove(language); + + return await RepositoryMethods.SaveChangesAsync(this._context); } - public bool DoesUserHaveThisUsername(Guid id, string username) + public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) { - return this._context - .Set() - .Any(x => x.Id == id && - x.UserName == username); + this._context.Update(user); + + user.Technologies.Remove(technology); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Validations + + public async Task DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AnyAsync(x => x.Id == id); } - public async Task DoesUsernameExist(string username) + public async Task DoesUsernameExistAsync(string username) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.UserName == username); } - public async Task DoesEmailExist(string email) + public async Task DoesEmailExistAsync(string email) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId) + { + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == userId); + + User friend = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == friendId); + + return user.Friends.Contains(friend); + } + + public bool DoesUserHaveThisUsername(Guid id, string username) + { + return this._context.Users + .Any(x => x.Id == id && + x.UserName == username); + } + + public bool DoesUserHaveFriends(User user) + { + return user.Friends.Count >= 1; + } + #endregion } } diff --git a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs index 27f392d..0be9ca2 100644 --- a/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Services/Configurations/Mapping/LanguageMappings.cs @@ -10,7 +10,6 @@ namespace DevHive.Services.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs index 77f2733..74f66b4 100644 --- a/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/RegisterServiceModel.cs @@ -1,7 +1,13 @@ +using System.Collections.Generic; +using DevHive.Services.Models.Language; +using DevHive.Services.Models.Technology; + namespace DevHive.Services.Models.Identity.User { public class RegisterServiceModel : BaseUserServiceModel - { + { + public IList Languages { get; set; } + public IList Technologies { get; set; } public string Password { get; set; } } } diff --git a/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs new file mode 100644 index 0000000..75e7714 --- /dev/null +++ b/src/DevHive.Services/Models/Language/CreateLanguageServiceModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Services.Models.Language +{ + public class CreateLanguageServiceModel : LanguageServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Models/Language/LanguageServiceModel.cs b/src/DevHive.Services/Models/Language/LanguageServiceModel.cs index f06ebb6..a07aa16 100644 --- a/src/DevHive.Services/Models/Language/LanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/LanguageServiceModel.cs @@ -1,7 +1,9 @@ +using System; + namespace DevHive.Services.Models.Language { public class LanguageServiceModel { - public string Name { get; set; } + public Guid Id { get; set; } } } diff --git a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs index 30194dd..fdc309e 100644 --- a/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs +++ b/src/DevHive.Services/Models/Language/UpdateLanguageServiceModel.cs @@ -1,9 +1,4 @@ -using System; - namespace DevHive.Services.Models.Language { - public class UpdateLanguageServiceModel : LanguageServiceModel - { - public Guid Id { get; set; } - } + public class UpdateLanguageServiceModel : CreateLanguageServiceModel { } } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 3c011c6..127bde4 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -2,25 +2,25 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; using DevHive.Services.Models.Language; namespace DevHive.Services.Services { public class LanguageService { - private readonly ILanguageRepository _languageRepository; + private readonly LanguageRepository _languageRepository; private readonly IMapper _languageMapper; - public LanguageService(ILanguageRepository languageRepository, IMapper mapper) + public LanguageService(LanguageRepository languageRepository, IMapper mapper) { this._languageRepository = languageRepository; this._languageMapper = mapper; } - public async Task CreateLanguage(LanguageServiceModel languageServiceModel) + public async Task CreateLanguage(CreateLanguageServiceModel languageServiceModel) { - if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) + if (await this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name)) throw new ArgumentException("Language already exists!"); Language language = this._languageMapper.Map(languageServiceModel); @@ -33,7 +33,7 @@ namespace DevHive.Services.Services { Language language = await this._languageRepository.GetByIdAsync(id); - if(language == null) + if (language == null) throw new ArgumentException("The language does not exist"); return this._languageMapper.Map(language); @@ -41,28 +41,28 @@ namespace DevHive.Services.Services public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { - if (!await this._languageRepository.DoesLanguageExist(languageServiceModel.Id)) - throw new ArgumentException("Language does not exist!"); + Task langExist = this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); + Task newLangNameExists = this._languageRepository.DoesLanguageNameExistAsync(languageServiceModel.Name); - if (await this._languageRepository.DoesLanguageNameExist(languageServiceModel.Name)) - throw new ArgumentException("Language name already exists!"); + await Task.WhenAny(langExist, newLangNameExists); - Language language = this._languageMapper.Map(languageServiceModel); - //language.Id = languageServiceModel.Id; - bool result = await this._languageRepository.EditAsync(language); + if (!langExist.Result) + throw new ArgumentException("Language already exists!"); - return result; + if (newLangNameExists.Result) + throw new ArgumentException("This name is already in our datbase!"); + + Language lang = this._languageMapper.Map(languageServiceModel); + return await this._languageRepository.EditAsync(lang); } - + public async Task DeleteLanguage(Guid id) { - if (!await this._languageRepository.DoesLanguageExist(id)) + if (!await this._languageRepository.DoesLanguageExistAsync(id)) throw new ArgumentException("Language does not exist!"); Language language = await this._languageRepository.GetByIdAsync(id); - bool result = await this._languageRepository.DeleteAsync(language); - - return result; + return await this._languageRepository.DeleteAsync(language); } } } \ No newline at end of file diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index e0a2be9..321c694 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -7,17 +7,17 @@ using DevHive.Services.Models.Post.Comment; using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class PostService { - private readonly IPostRepository _postRepository; - private readonly IUserRepository _userRepository; + private readonly PostRepository _postRepository; + private readonly UserRepository _userRepository; private readonly IMapper _postMapper; - public PostService(IPostRepository postRepository, IUserRepository userRepository , IMapper postMapper) + public PostService(PostRepository postRepository, UserRepository userRepository , IMapper postMapper) { this._postRepository = postRepository; this._userRepository = userRepository; diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 372984d..7ba0a98 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -3,16 +3,16 @@ using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class RoleService { - private readonly IRoleRepository _roleRepository; + private readonly RoleRepository _roleRepository; private readonly IMapper _roleMapper; - public RoleService(IRoleRepository roleRepository, IMapper mapper) + public RoleService(RoleRepository roleRepository, IMapper mapper) { this._roleRepository = roleRepository; this._roleMapper = mapper; diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index e03d4d1..2913a55 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -2,17 +2,17 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Data.Models; -using DevHive.Data.Repositories.Contracts; +using DevHive.Data.Repositories; using DevHive.Services.Models.Technology; namespace DevHive.Services.Services { public class TechnologyService { - private readonly ITechnologyRepository _technologyRepository; + private readonly TechnologyRepository _technologyRepository; private readonly IMapper _technologyMapper; - public TechnologyService(ITechnologyRepository technologyRepository, IMapper technologyMapper) + public TechnologyService(TechnologyRepository technologyRepository, IMapper technologyMapper) { this._technologyRepository = technologyRepository; this._technologyMapper = technologyMapper; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index c8bcab9..e1f925d 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -11,28 +11,40 @@ using System.Security.Cryptography; using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; -using DevHive.Data.Repositories.Contracts; +using DevHive.Services.Models.Language; +using DevHive.Data.Repositories; namespace DevHive.Services.Services { public class UserService { - private readonly IUserRepository _userRepository; - private readonly IRoleRepository _roleRepository; + private readonly UserRepository _userRepository; + private readonly RoleRepository _roleRepository; + private readonly LanguageRepository _languageRepository; + private readonly TechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; - public UserService(IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper, JWTOptions jwtOptions) + public UserService(UserRepository userRepository, + LanguageRepository languageRepository, + RoleRepository roleRepository, + TechnologyRepository technologyRepository, + IMapper mapper, + JWTOptions jwtOptions) { this._userRepository = userRepository; this._roleRepository = roleRepository; this._userMapper = mapper; this._jwtOptions = jwtOptions; + this._languageRepository = languageRepository; + this._technologyRepository = technologyRepository; } + #region Authentication + public async Task LoginUser(LoginServiceModel loginModel) { - if (!await this._userRepository.DoesUsernameExist(loginModel.UserName)) + if (!await this._userRepository.DoesUsernameExistAsync(loginModel.UserName)) throw new ArgumentException("Invalid username!"); User user = await this._userRepository.GetByUsername(loginModel.UserName); @@ -45,10 +57,10 @@ namespace DevHive.Services.Services public async Task RegisterUser(RegisterServiceModel registerModel) { - if (await this._userRepository.DoesUsernameExist(registerModel.UserName)) + if (await this._userRepository.DoesUsernameExistAsync(registerModel.UserName)) throw new ArgumentException("Username already exists!"); - if (await this._userRepository.DoesEmailExist(registerModel.Email)) + if (await this._userRepository.DoesEmailExistAsync(registerModel.Email)) throw new ArgumentException("Email already exists!"); User user = this._userMapper.Map(registerModel); @@ -66,20 +78,58 @@ namespace DevHive.Services.Services return new TokenModel(WriteJWTSecurityToken(user.UserName, user.Roles)); } + #endregion + + #region Create public async Task AddFriend(Guid userId, Guid friendId) { - User user = await this._userRepository.GetByIdAsync(userId); - User friend = await this._userRepository.GetByIdAsync(friendId); + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task friendExists = this._userRepository.DoesUserExistAsync(friendId); + + await Task.WhenAll(userExists, friendExists); + + if (!userExists.Result) + throw new ArgumentException("User doesn't exist!"); - if (DoesUserHaveThisFriend(user, friend)) + 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."); - return user != default(User) && friend != default(User) ? - await this._userRepository.AddFriendAsync(user, friend) : + User user = await this._userRepository.GetByIdAsync(userId); + User friend = await this._userRepository.GetByIdAsync(friendId); + + return user != default(User) && friend != default(User) ? + await this._userRepository.AddFriendAsync(user, friend) : throw new ArgumentException("Invalid user!"); } + public async Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel) + { + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task languageExists = this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); + + await Task.WhenAll(userExists, languageExists); + + if (!userExists.Result) + throw new ArgumentException("User does not exist!"); + + if (!languageExists.Result) + throw new ArgumentException("Language does not exist!"); + + Task user = this._userRepository.GetByIdAsync(userId); + Task language = this._languageRepository.GetByIdAsync(languageServiceModel.Id); + + await Task.WhenAll(user, language); + + return await this._userRepository.AddLanguageToUserAsync(user.Result, language.Result); + } + #endregion + + #region Read + public async Task GetUserById(Guid id) { User user = await this._userRepository.GetByIdAsync(id) @@ -90,21 +140,24 @@ namespace DevHive.Services.Services public async Task GetFriendById(Guid friendId) { - if(!_userRepository.DoesUserExist(friendId)) + if (!await _userRepository.DoesUserExistAsync(friendId)) throw new ArgumentException("User does not exist!"); User friend = await this._userRepository.GetByIdAsync(friendId); return this._userMapper.Map(friend); } + #endregion + + #region Update public async Task UpdateUser(UpdateUserServiceModel updateModel) { - if (!this._userRepository.DoesUserExist(updateModel.Id)) + if (!await this._userRepository.DoesUserExistAsync(updateModel.Id)) throw new ArgumentException("User does not exist!"); if (!this._userRepository.DoesUserHaveThisUsername(updateModel.Id, updateModel.UserName) - && await this._userRepository.DoesUsernameExist(updateModel.UserName)) + && await this._userRepository.DoesUsernameExistAsync(updateModel.UserName)) throw new ArgumentException("Username already exists!"); User user = this._userMapper.Map(updateModel); @@ -113,12 +166,15 @@ namespace DevHive.Services.Services if (!result) throw new InvalidOperationException("Unable to edit user!"); - return this._userMapper.Map(user);; + return this._userMapper.Map(user); ; } + #endregion + + #region Delete public async Task DeleteUser(Guid id) { - if (!this._userRepository.DoesUserExist(id)) + if (!await this._userRepository.DoesUserExistAsync(id)) throw new ArgumentException("User does not exist!"); User user = await this._userRepository.GetByIdAsync(id); @@ -130,21 +186,25 @@ namespace DevHive.Services.Services public async Task RemoveFriend(Guid userId, Guid friendId) { - if(!this._userRepository.DoesUserExist(userId) && - !this._userRepository.DoesUserExist(friendId)) - throw new ArgumentException("Invalid user!"); + Task userExists = this._userRepository.DoesUserExistAsync(userId); + Task friendExists = this._userRepository.DoesUserExistAsync(friendId); - User user = await this._userRepository.GetByIdAsync(userId); - User friend = await this._userRepository.GetByIdAsync(friendId); + await Task.WhenAll(userExists, friendExists); + + if (!userExists.Result) + throw new ArgumentException("User doesn't exist!"); - if(!this.DoesUserHaveFriends(user)) - throw new ArgumentException("User does not have any friends."); + if (!friendExists.Result) + throw new ArgumentException("Friend doesn't exist!"); - if (!DoesUserHaveThisFriend(user, friend)) + if (!await this._userRepository.DoesUserHaveThisFriendAsync(userId, friendId)) throw new ArgumentException("This ain't your friend, amigo."); - return await this.RemoveFriend(user.Id, friendId); + return await this.RemoveFriend(userId, friendId); } + #endregion + + #region Validations public async Task ValidJWT(Guid id, string rawTokenData) { @@ -153,7 +213,7 @@ namespace DevHive.Services.Services string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - + User user = await this._userRepository.GetByUsername(jwtUserName) ?? throw new ArgumentException("User does not exist!"); @@ -164,9 +224,9 @@ namespace DevHive.Services.Services return false; /* Check roles */ - + // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) - foreach(var role in user.Roles) + foreach (var role in user.Roles) { if (!jwtRoleNames.Contains(role.Name)) return false; @@ -179,26 +239,11 @@ namespace DevHive.Services.Services return true; } - private string GeneratePasswordHash(string password) - { - return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); - } - - private bool DoesUserHaveThisFriend(User user, User friend) - { - return user.Friends.Contains(friend); - } - - private bool DoesUserHaveFriends(User user) - { - return user.Friends.Count >= 1; - } - private List GetClaimTypeValues(string type, IEnumerable claims) { List toReturn = new(); - - foreach(var claim in claims) + + foreach (var claim in claims) if (claim.Type == type) toReturn.Add(claim.Value); @@ -214,7 +259,7 @@ namespace DevHive.Services.Services new Claim(ClaimTypes.Name, userName), }; - foreach(var role in roles) + foreach (var role in roles) { claims.Add(new Claim(ClaimTypes.Role, role.Name)); } @@ -232,5 +277,12 @@ 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 } } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index ee0ca4b..beac798 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -34,7 +34,7 @@ namespace DevHive.Data.Tests this.Context.Database.EnsureDeleted(); } - #region AddAync + #region AddAsync [Test] public void AddAsync_AddsTheGivenTechnologyToTheDatabase() { @@ -127,7 +127,7 @@ namespace DevHive.Data.Tests { bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExist returns true when tehcnology name does not exist"); + Assert.False(result, "DoesTechnologyNameExist returns true when technology name does not exist"); }).GetAwaiter().GetResult(); } #endregion diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 5715b13..bae8562 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -9,9 +9,12 @@ namespace DevHive.Web.Configurations.Mapping public LanguageMappings() { CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); + CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/src/DevHive.Web/Controllers/LanguageController.cs b/src/DevHive.Web/Controllers/LanguageController.cs index d71d622..29c1e99 100644 --- a/src/DevHive.Web/Controllers/LanguageController.cs +++ b/src/DevHive.Web/Controllers/LanguageController.cs @@ -23,9 +23,9 @@ namespace DevHive.Web.Controllers } [HttpPost] - public async Task Create([FromBody] LanguageWebModel languageWebModel) + public async Task Create([FromBody] CreateLanguageWebModel createLanguageWebModel) { - LanguageServiceModel languageServiceModel = this._languageMapper.Map(languageWebModel); + CreateLanguageServiceModel languageServiceModel = this._languageMapper.Map(createLanguageWebModel); bool result = await this._languageService.CreateLanguage(languageServiceModel); diff --git a/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs new file mode 100644 index 0000000..111beed --- /dev/null +++ b/src/DevHive.Web/Models/Language/CreateLanguageWebModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Web.Models.Language +{ + public class CreateLanguageWebModel : LanguageWebModel + { + public string Name { get; set; } + } +} \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/LanguageWebModel.cs b/src/DevHive.Web/Models/Language/LanguageWebModel.cs index 1ec38f3..455b559 100644 --- a/src/DevHive.Web/Models/Language/LanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/LanguageWebModel.cs @@ -1,7 +1,9 @@ +using System; + namespace DevHive.Web.Models.Language { public class LanguageWebModel { - public string Name { get; set; } + public Guid Id { get; set; } } } \ No newline at end of file diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index 26eb6c2..2da8217 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,8 +2,5 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : LanguageWebModel - { - public Guid Id { get; set; } - } + public class UpdateLanguageWebModel : CreateLanguageWebModel {} } \ No newline at end of file -- cgit v1.2.3 From 6deaa6edcd8e347d5ed28ee3389cb8712cc64ea3 Mon Sep 17 00:00:00 2001 From: transtrike Date: Wed, 13 Jan 2021 11:05:26 +0200 Subject: The return of the interfaces --- src/DevHive.Data/Interfaces/ILanguageRepository.cs | 13 + src/DevHive.Data/Interfaces/IPostRepository.cs | 20 ++ src/DevHive.Data/Interfaces/IRepository.cs | 21 ++ src/DevHive.Data/Interfaces/IRoleRepository.cs | 15 ++ .../Interfaces/ITechnologyRepository.cs | 13 + src/DevHive.Data/Interfaces/IUserRepository.cs | 37 +++ src/DevHive.Data/Repositories/IRepository.cs | 21 -- .../Repositories/LanguageRepository.cs | 4 +- src/DevHive.Data/Repositories/PostRepository.cs | 13 +- src/DevHive.Data/Repositories/RoleRepository.cs | 5 +- .../Repositories/TechnologyRepository.cs | 9 +- src/DevHive.Data/Repositories/UserRepository.cs | 7 +- .../Interfaces/ILanguageService.cs | 17 ++ src/DevHive.Services/Interfaces/IPostService.cs | 24 ++ src/DevHive.Services/Interfaces/IRoleService.cs | 17 ++ .../Interfaces/ITechnologyService.cs | 17 ++ src/DevHive.Services/Interfaces/IUserService.cs | 31 +++ src/DevHive.Services/Services/LanguageService.cs | 22 +- src/DevHive.Services/Services/PostService.cs | 27 +- src/DevHive.Services/Services/RoleService.cs | 11 +- src/DevHive.Services/Services/TechnologyService.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 22 +- .../TechnologyService.Tests.cs | 273 --------------------- .../TechnologyServices.Tests.cs | 270 ++++++++++++++++++++ src/DevHive.code-workspace | 3 +- 25 files changed, 573 insertions(+), 350 deletions(-) create mode 100644 src/DevHive.Data/Interfaces/ILanguageRepository.cs create mode 100644 src/DevHive.Data/Interfaces/IPostRepository.cs create mode 100644 src/DevHive.Data/Interfaces/IRepository.cs create mode 100644 src/DevHive.Data/Interfaces/IRoleRepository.cs create mode 100644 src/DevHive.Data/Interfaces/ITechnologyRepository.cs create mode 100644 src/DevHive.Data/Interfaces/IUserRepository.cs delete mode 100644 src/DevHive.Data/Repositories/IRepository.cs create mode 100644 src/DevHive.Services/Interfaces/ILanguageService.cs create mode 100644 src/DevHive.Services/Interfaces/IPostService.cs create mode 100644 src/DevHive.Services/Interfaces/IRoleService.cs create mode 100644 src/DevHive.Services/Interfaces/ITechnologyService.cs create mode 100644 src/DevHive.Services/Interfaces/IUserService.cs delete mode 100644 src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/DevHive.Data/Interfaces/ILanguageRepository.cs new file mode 100644 index 0000000..40dd461 --- /dev/null +++ b/src/DevHive.Data/Interfaces/ILanguageRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces +{ + public interface ILanguageRepository : IRepository + { + Task DoesLanguageExistAsync(Guid id); + Task DoesLanguageNameExistAsync(string languageName); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/IPostRepository.cs b/src/DevHive.Data/Interfaces/IPostRepository.cs new file mode 100644 index 0000000..9c75ecc --- /dev/null +++ b/src/DevHive.Data/Interfaces/IPostRepository.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces +{ + public interface IPostRepository : IRepository + { + Task AddCommentAsync(Comment entity); + + Task GetCommentByIdAsync(Guid id); + + Task EditCommentAsync(Comment newEntity); + + Task DeleteCommentAsync(Comment entity); + Task DoesCommentExist(Guid id); + Task DoesPostExist(Guid postId); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/IRepository.cs b/src/DevHive.Data/Interfaces/IRepository.cs new file mode 100644 index 0000000..40a78de --- /dev/null +++ b/src/DevHive.Data/Interfaces/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Interfaces +{ + public interface IRepository + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Find entity by id + Task GetByIdAsync(Guid id); + + //Modify Entity from database + Task EditAsync(TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(TEntity entity); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/IRoleRepository.cs b/src/DevHive.Data/Interfaces/IRoleRepository.cs new file mode 100644 index 0000000..48761db --- /dev/null +++ b/src/DevHive.Data/Interfaces/IRoleRepository.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces +{ + public interface IRoleRepository : IRepository + { + Task GetByNameAsync(string name); + + Task DoesNameExist(string name); + Task DoesRoleExist(Guid id); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/DevHive.Data/Interfaces/ITechnologyRepository.cs new file mode 100644 index 0000000..7c126a4 --- /dev/null +++ b/src/DevHive.Data/Interfaces/ITechnologyRepository.cs @@ -0,0 +1,13 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces +{ + public interface ITechnologyRepository : IRepository + { + Task DoesTechnologyExistAsync(Guid id); + Task DoesTechnologyNameExist(string technologyName); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/IUserRepository.cs b/src/DevHive.Data/Interfaces/IUserRepository.cs new file mode 100644 index 0000000..8ee5054 --- /dev/null +++ b/src/DevHive.Data/Interfaces/IUserRepository.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces +{ + public interface IUserRepository : IRepository + { + Task AddFriendAsync(User user, User friend); + Task AddLanguageToUserAsync(User user, Language language); + Task AddTechnologyToUserAsync(User user, Technology technology); + + Task GetByUsername(string username); + Language GetUserLanguage(User user, Language language); + IList GetUserLanguages(User user); + IList GetUserTechnologies(User user); + Technology GetUserTechnology(User user, Technology technology); + IEnumerable QueryAll(); + + Task EditUserLanguage(User user, Language oldLang, Language newLang); + Task EditUserTechnologies(User user, Technology oldTech, Technology newTech); + + Task RemoveFriendAsync(User user, User friend); + Task RemoveLanguageFromUserAsync(User user, Language language); + Task RemoveTechnologyFromUserAsync(User user, Technology technology); + + bool DoesUserHaveThisLanguage(User user, Language language); + bool DoesUserHaveThisUsername(Guid id, string username); + bool DoesUserHaveFriends(User user); + Task DoesEmailExistAsync(string email); + Task DoesUserExistAsync(Guid id); + Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId); + Task DoesUsernameExistAsync(string username); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs deleted file mode 100644 index 920ba13..0000000 --- a/src/DevHive.Data/Repositories/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories -{ - public interface IRepository - where TEntity : class - { - //Add Entity to database - Task AddAsync(TEntity entity); - - //Find entity by id - Task GetByIdAsync(Guid id); - - //Modify Entity from database - Task EditAsync(TEntity newEntity); - - //Delete Entity from database - Task DeleteAsync(TEntity entity); - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 5d8217a..c30d3bb 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,13 +1,13 @@ using System; -using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; +using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : IRepository + public class LanguageRepository : ILanguageRepository { private readonly DevHiveContext _context; diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 002fb17..f5e9b7b 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,12 +1,13 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; +using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IRepository + public class PostRepository : IPostRepository { private readonly DevHiveContext _context; @@ -52,9 +53,9 @@ namespace DevHive.Data.Repositories //Update public async Task EditAsync(Post newPost) { - this._context - .Set() - .Update(newPost); + this._context + .Set() + .Update(newPost); return await RepositoryMethods.SaveChangesAsync(this._context); } @@ -77,7 +78,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - + public async Task DeleteCommentAsync(Comment entity) { this._context @@ -86,7 +87,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - + #region Validations public async Task DoesPostExist(Guid postId) diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 0ca1646..4cd5b79 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,12 +1,13 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; +using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRepository + public class RoleRepository : IRoleRepository { private readonly DevHiveContext _context; @@ -52,7 +53,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - + //Delete public async Task DeleteAsync(Role entity) { diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 21d69a3..a8208b6 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,13 +1,14 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; +using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class TechnologyRepository : IRepository + public class TechnologyRepository : ITechnologyRepository { private readonly DevHiveContext _context; @@ -42,9 +43,9 @@ namespace DevHive.Data.Repositories public async Task EditAsync(Technology newEntity) { - this._context - .Set() - .Update(newEntity); + this._context + .Set() + .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 64a81ae..1f29bb5 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; +using DevHive.Data.Interfaces; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IRepository + public class UserRepository : IUserRepository { private readonly DevHiveContext _context; @@ -91,7 +92,7 @@ namespace DevHive.Data.Repositories return user.Langauges .FirstOrDefault(x => x.Id == language.Id); } - + public IList GetUserTechnologies(User user) { return user.Technologies; @@ -220,7 +221,7 @@ namespace DevHive.Data.Repositories { return user.Friends.Count >= 1; } - + public bool DoesUserHaveThisLanguage(User user, Language language) { return user.Langauges.Contains(language); diff --git a/src/DevHive.Services/Interfaces/ILanguageService.cs b/src/DevHive.Services/Interfaces/ILanguageService.cs new file mode 100644 index 0000000..f62bce7 --- /dev/null +++ b/src/DevHive.Services/Interfaces/ILanguageService.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Language; + +namespace DevHive.Services.Interfaces +{ + public interface ILanguageService + { + Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel); + + Task GetLanguageById(Guid id); + + Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel); + + Task DeleteLanguage(Guid id); + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Interfaces/IPostService.cs b/src/DevHive.Services/Interfaces/IPostService.cs new file mode 100644 index 0000000..9cb21ed --- /dev/null +++ b/src/DevHive.Services/Interfaces/IPostService.cs @@ -0,0 +1,24 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Post.Comment; +using DevHive.Services.Models.Post.Post; + +namespace DevHive.Services.Interfaces +{ + public interface IPostService + { + Task CreatePost(CreatePostServiceModel postServiceModel); + Task AddComment(CreateCommentServiceModel commentServiceModel); + + Task GetCommentById(Guid id); + Task GetPostById(Guid id); + + Task UpdateComment(UpdateCommentServiceModel commentServiceModel); + Task UpdatePost(UpdatePostServiceModel postServiceModel); + + Task DeleteComment(Guid id); + Task DeletePost(Guid id); + + Task ValidateJwtForComment(Guid commentId, string rawTokenData); + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs new file mode 100644 index 0000000..3bf236c --- /dev/null +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using DevHive.Common.Models.Identity; + +namespace DevHive.Services.Interfaces +{ + public interface IRoleService + { + Task CreateRole(RoleModel roleServiceModel); + + Task GetRoleById(Guid id); + + Task UpdateRole(RoleModel roleServiceModel); + + Task DeleteRole(Guid id); + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Interfaces/ITechnologyService.cs b/src/DevHive.Services/Interfaces/ITechnologyService.cs new file mode 100644 index 0000000..33032e2 --- /dev/null +++ b/src/DevHive.Services/Interfaces/ITechnologyService.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using DevHive.Services.Models.Technology; + +namespace DevHive.Services.Interfaces +{ + public interface ITechnologyService + { + Task Create(CreateTechnologyServiceModel technologyServiceModel); + + Task GetTechnologyById(Guid id); + + Task UpdateTechnology(UpdateTechnologyServiceModel updateTechnologyServiceModel); + + Task DeleteTechnology(Guid id); + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs new file mode 100644 index 0000000..ba53563 --- /dev/null +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -0,0 +1,31 @@ +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 +{ + public interface IUserService + { + Task LoginUser(LoginServiceModel loginModel); + Task RegisterUser(RegisterServiceModel registerModel); + + Task AddFriend(Guid userId, Guid friendId); + Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel); + Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel); + + Task GetFriendById(Guid friendId); + Task GetUserById(Guid id); + + Task UpdateUser(UpdateUserServiceModel updateModel); + + Task DeleteUser(Guid id); + Task RemoveFriend(Guid userId, Guid friendId); + Task RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel); + Task RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel); + + Task ValidJWT(Guid id, string rawTokenData); + } +} \ No newline at end of file diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index ccc64fd..ac7652b 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -1,34 +1,39 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Data.Interfaces; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; using DevHive.Services.Models.Language; namespace DevHive.Services.Services { - public class LanguageService + public class LanguageService : ILanguageService { - private readonly LanguageRepository _languageRepository; + private readonly ILanguageRepository _languageRepository; private readonly IMapper _languageMapper; - public LanguageService(LanguageRepository languageRepository, IMapper mapper) + public LanguageService(ILanguageRepository languageRepository, IMapper mapper) { this._languageRepository = languageRepository; this._languageMapper = mapper; } + #region Create + public async Task CreateLanguage(CreateLanguageServiceModel createLanguageServiceModel) { if (await this._languageRepository.DoesLanguageNameExistAsync(createLanguageServiceModel.Name)) throw new ArgumentException("Language already exists!"); - //TODO: Fix, cuz it breaks Language language = this._languageMapper.Map(createLanguageServiceModel); bool result = await this._languageRepository.AddAsync(language); return result; } + #endregion + + #region Read public async Task GetLanguageById(Guid id) { @@ -39,6 +44,9 @@ namespace DevHive.Services.Services return this._languageMapper.Map(language); } + #endregion + + #region Update public async Task UpdateLanguage(UpdateLanguageServiceModel languageServiceModel) { @@ -56,6 +64,9 @@ namespace DevHive.Services.Services Language lang = this._languageMapper.Map(languageServiceModel); return await this._languageRepository.EditAsync(lang); } + #endregion + + #region Delete public async Task DeleteLanguage(Guid id) { @@ -65,5 +76,6 @@ namespace DevHive.Services.Services Language language = await this._languageRepository.GetByIdAsync(id); return await this._languageRepository.DeleteAsync(language); } + #endregion } } \ No newline at end of file diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 321c694..da9e76b 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -7,17 +7,18 @@ using DevHive.Services.Models.Post.Comment; using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; +using DevHive.Data.Interfaces; namespace DevHive.Services.Services { - public class PostService + public class PostService : IPostService { - private readonly PostRepository _postRepository; - private readonly UserRepository _userRepository; + private readonly IPostRepository _postRepository; + private readonly IUserRepository _userRepository; private readonly IMapper _postMapper; - public PostService(PostRepository postRepository, UserRepository userRepository , IMapper postMapper) + public PostService(IPostRepository postRepository, IUserRepository userRepository, IMapper postMapper) { this._postRepository = postRepository; this._userRepository = userRepository; @@ -36,16 +37,16 @@ namespace DevHive.Services.Services { commentServiceModel.TimeCreated = DateTime.Now; Comment comment = this._postMapper.Map(commentServiceModel); - + bool result = await this._postRepository.AddCommentAsync(comment); return result; } - + //Read public async Task GetPostById(Guid id) { - Post post = await this._postRepository.GetByIdAsync(id) + Post post = await this._postRepository.GetByIdAsync(id) ?? throw new ArgumentException("Post does not exist!"); return this._postMapper.Map(post); @@ -55,7 +56,7 @@ namespace DevHive.Services.Services { Comment comment = await this._postRepository.GetCommentByIdAsync(id); - if(comment == null) + if (comment == null) throw new ArgumentException("The comment does not exist"); return this._postMapper.Map(comment); @@ -88,7 +89,7 @@ namespace DevHive.Services.Services Post post = await this._postRepository.GetByIdAsync(id); return await this._postRepository.DeleteAsync(post); } - + public async Task DeleteComment(Guid id) { if (!await this._postRepository.DoesCommentExist(id)) @@ -118,7 +119,7 @@ namespace DevHive.Services.Services string jwtUserName = this.GetClaimTypeValues("unique_name", jwt.Claims)[0]; //List jwtRoleNames = this.GetClaimTypeValues("role", jwt.Claims); - + User user = await this._userRepository.GetByUsername(jwtUserName) ?? throw new ArgumentException("User does not exist!"); @@ -128,8 +129,8 @@ namespace DevHive.Services.Services private List GetClaimTypeValues(string type, IEnumerable claims) { List toReturn = new(); - - foreach(var claim in claims) + + foreach (var claim in claims) if (claim.Type == type) toReturn.Add(claim.Value); diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 7ba0a98..fd56c2c 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -2,17 +2,18 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; +using DevHive.Data.Interfaces; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; namespace DevHive.Services.Services { - public class RoleService + public class RoleService : IRoleService { - private readonly RoleRepository _roleRepository; + private readonly IRoleRepository _roleRepository; private readonly IMapper _roleMapper; - public RoleService(RoleRepository roleRepository, IMapper mapper) + public RoleService(IRoleRepository roleRepository, IMapper mapper) { this._roleRepository = roleRepository; this._roleMapper = mapper; @@ -30,7 +31,7 @@ namespace DevHive.Services.Services public async Task GetRoleById(Guid id) { - Role role = await this._roleRepository.GetByIdAsync(id) + Role role = await this._roleRepository.GetByIdAsync(id) ?? throw new ArgumentException("Role does not exist!"); return this._roleMapper.Map(role); diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 883b8c5..cb8fdfc 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -1,18 +1,19 @@ using System; using System.Threading.Tasks; using AutoMapper; +using DevHive.Data.Interfaces; using DevHive.Data.Models; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; using DevHive.Services.Models.Technology; namespace DevHive.Services.Services { - public class TechnologyService + public class TechnologyService : ITechnologyService { - private readonly TechnologyRepository _technologyRepository; + private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _technologyMapper; - public TechnologyService(TechnologyRepository technologyRepository, IMapper technologyMapper) + public TechnologyService(ITechnologyRepository technologyRepository, IMapper technologyMapper) { this._technologyRepository = technologyRepository; this._technologyMapper = technologyMapper; @@ -38,7 +39,7 @@ namespace DevHive.Services.Services { Technology technology = await this._technologyRepository.GetByIdAsync(id); - if(technology == null) + if (technology == null) throw new ArgumentException("The technology does not exist"); return this._technologyMapper.Map(technology); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index c1de741..6a6662d 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -12,24 +12,26 @@ using System.Text; using System.Collections.Generic; using DevHive.Common.Models.Identity; using DevHive.Services.Models.Language; -using DevHive.Data.Repositories; +using DevHive.Services.Interfaces; using DevHive.Services.Models.Technology; +using DevHive.Data.Repositories; +using DevHive.Data.Interfaces; namespace DevHive.Services.Services { - public class UserService + public class UserService : IUserService { - private readonly UserRepository _userRepository; - private readonly RoleRepository _roleRepository; - private readonly LanguageRepository _languageRepository; - private readonly TechnologyRepository _technologyRepository; + private readonly IUserRepository _userRepository; + private readonly IRoleRepository _roleRepository; + private readonly ILanguageRepository _languageRepository; + private readonly ITechnologyRepository _technologyRepository; private readonly IMapper _userMapper; private readonly JWTOptions _jwtOptions; - public UserService(UserRepository userRepository, - LanguageRepository languageRepository, - RoleRepository roleRepository, - TechnologyRepository technologyRepository, + public UserService(IUserRepository userRepository, + ILanguageRepository languageRepository, + IRoleRepository roleRepository, + ITechnologyRepository technologyRepository, IMapper mapper, JWTOptions jwtOptions) { diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs deleted file mode 100644 index acb5e64..0000000 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyService.Tests.cs +++ /dev/null @@ -1,273 +0,0 @@ -using AutoMapper; -using DevHive.Data; -using DevHive.Data.Models; -using DevHive.Data.Repositories; -using DevHive.Data.Repositories.Contracts; -using DevHive.Services.Models.Technology; -using DevHive.Services.Services; -using Microsoft.EntityFrameworkCore; -using Moq; -using NUnit.Framework; -using System; -using System.Threading.Tasks; - -namespace DevHive.Services.Tests -{ - [TestFixture] - public class TechnologyServiceTests - { - protected Mock TechnologyRepositoryMock { get; set; } - protected Mock MapperMock { get; set; } - protected TechnologyService TechnologyService { get; set; } - - [SetUp] - public void Setup() - { - this.TechnologyRepositoryMock = new Mock(); - this.MapperMock = new Mock(); - this.TechnologyService = new TechnologyService(this.TechnologyRepositoryMock.Object, this.MapperMock.Object); - } - - #region Create - [Test] - [TestCase(true)] - [TestCase(false)] - public void Create_ReturnsTrue_WhenEntityIsAddedSuccessfully(bool shouldFail) - { - Task.Run(async () => - { - string technologyName = "Gosho Trapov"; - - TechnologyServiceModel technologyServiceModel = new TechnologyServiceModel - { - Name = technologyName - }; - Technology technology = new Technology - { - Name = technologyName - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(shouldFail)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - bool result = await this.TechnologyService.Create(technologyServiceModel); - - Assert.AreEqual(shouldFail, result); - }).GetAwaiter().GetResult(); - } - - [Test] - public void Create_ThrowsArgumentException_WhenEntityAlreadyExists() - { - Task.Run(async () => - { - string expectedExceptionMessage = "Technology already exists!"; - string technologyName = "Gosho Trapov"; - - TechnologyServiceModel technologyServiceModel = new TechnologyServiceModel - { - Name = technologyName - }; - Technology technology = new Technology - { - Name = technologyName - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); - - try - { - await this.TechnologyService.Create(technologyServiceModel); - Assert.Fail("Create does not throw exception when technology already exists"); - } - catch(ArgumentException ex) - { - Assert.AreEqual(expectedExceptionMessage, ex.Message); - } - }).GetAwaiter().GetResult(); - } - #endregion - - #region GetById - [Test] - public void GetTechnologyById_ReturnsTheTechnology_WhenItExists() - { - Task.Run(async () => - { - Guid id = new Guid(); - string name = "Gosho Trapov"; - Technology technology = new Technology - { - Name = name - }; - TechnologyServiceModel technologyServiceModel = new TechnologyServiceModel - { - Name = name - }; - - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technologyServiceModel); - - TechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); - - Assert.AreEqual(name, result.Name); - }).GetAwaiter().GetResult(); - } - - [Test] - public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() - { - Task.Run(async () => - { - string exceptionMessage = "The technology does not exist"; - Guid id = new Guid(); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); - - try - { - await this.TechnologyService.GetTechnologyById(id); - Assert.Fail("GetTechnologyById does not throw exception when technology does not exist"); - } - catch(ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception messege is nto correct"); - } - }).GetAwaiter().GetResult(); - } - #endregion - - #region Update - [Test] - [TestCase(true)] - [TestCase(false)] - public void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) - { - Task.Run(async () => - { - Guid id = new Guid(); - string name = "Gosho Trapov"; - Technology technology = new Technology - { - Name = name - }; - UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel - { - Name = name, - Id = id - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); - - Assert.AreEqual(shouldPass, result); - }).GetAwaiter().GetResult(); - } - - [Test] - public void UpdateTechnology_ThrowsException_WhenTechnologyDoesNotExist() - { - Task.Run(async () => - { - string exceptionMessage = "Technology does not exist!"; - Guid id = new Guid(); - UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel - { - Id = id - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - - try - { - await this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel); - Assert.Fail("UpdateTechnology does not throw exception when technology does not exist"); - } - catch(ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); - } - }).GetAwaiter().GetResult(); - } - - [Test] - public void UpdateTechnology_ThrowsException_WhenTechnologyNameAlreadyExists() - { - Task.Run(async () => - { - string exceptionMessage = "Technology name already exists!"; - Guid id = new Guid(); - UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel - { - Id = id - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); - - try - { - await this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel); - Assert.Fail("UpdateTechnology does not throw exception when technology name already exist"); - } - catch(ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); - } - }).GetAwaiter().GetResult(); - } - #endregion - - #region Delete - [Test] - [TestCase(true)] - [TestCase(false)] - public void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) - { - Task.Run(async () => - { - Guid id = new Guid(); - Technology technology = new Technology(); - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - - bool result = await this.TechnologyService.DeleteTechnology(id); - - Assert.AreEqual(shouldPass, result); - }).GetAwaiter().GetResult(); - } - - [Test] - public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist() - { - Task.Run(async () => - { - string exceptionMessage = "Technology does not exist!"; - Guid id = new Guid(); - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - - try - { - await this.TechnologyService.DeleteTechnology(id); - Assert.Fail("DeleteTechnology does not throw exception when technology does not exist"); - } - catch(ArgumentException ex) - { - Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); - } - }).GetAwaiter().GetResult(); - } - #endregion - //Task.Run(async () => - //{ - // - //}).GetAwaiter().GetResult(); - } -} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs new file mode 100644 index 0000000..f335aba --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -0,0 +1,270 @@ +using AutoMapper; +using DevHive.Data.Interfaces; +using DevHive.Data.Models; +using DevHive.Services.Models.Technology; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class TechnologyServicesTests + { + private Mock TechnologyRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private TechnologyService TechnologyService { get; set; } + + [SetUp] + public void Setup() + { + this.TechnologyRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.TechnologyService = new TechnologyService(this.TechnologyRepositoryMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + [TestCase(true)] + [TestCase(false)] + public void Create_ReturnsTrue_WhenEntityIsAddedSuccessfully(bool shouldFail) + { + Task.Run(async () => + { + string technologyName = "Gosho Trapov"; + + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(shouldFail)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + bool result = await this.TechnologyService.Create(createTechnologyServiceModel); + + Assert.AreEqual(shouldFail, result); + }).GetAwaiter().GetResult(); + } + + [Test] + public void Create_ThrowsArgumentException_WhenEntityAlreadyExists() + { + Task.Run(async () => + { + string expectedExceptionMessage = "Technology already exists!"; + string technologyName = "Gosho Trapov"; + + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); + + try + { + await this.TechnologyService.Create(createTechnologyServiceModel); + Assert.Fail("Create does not throw exception when technology already exists"); + } + catch (ArgumentException ex) + { + Assert.AreEqual(expectedExceptionMessage, ex.Message); + } + }).GetAwaiter().GetResult(); + } + #endregion + + // #region GetById + // [Test] + // public void GetTechnologyById_ReturnsTheTechnology_WhenItExists() + // { + // Task.Run(async () => + // { + // Guid id = new Guid(); + // string name = "Gosho Trapov"; + // Technology technology = new() + // { + // Name = name + // }; + // TechnologyServiceModel technologyServiceModel = new()) + // { + // Name = name + // }; + + // this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technologyServiceModel); + + // TechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); + + // Assert.AreEqual(name, result.Name); + // }).GetAwaiter().GetResult(); + // } + + // [Test] + // public void GetTechnologyById_ThrowsException_WhenTechnologyDoesNotExist() + // { + // Task.Run(async () => + // { + // string exceptionMessage = "The technology does not exist"; + // Guid id = new Guid(); + // this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(null)); + + // try + // { + // await this.TechnologyService.GetTechnologyById(id); + // Assert.Fail("GetTechnologyById does not throw exception when technology does not exist"); + // } + // catch (ArgumentException ex) + // { + // Assert.AreEqual(exceptionMessage, ex.Message, "Exception messege is nto correct"); + // } + // }).GetAwaiter().GetResult(); + // } + // #endregion + + // #region Update + // [Test] + // [TestCase(true)] + // [TestCase(false)] + // public void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) + // { + // Task.Run(async () => + // { + // Guid id = new Guid(); + // string name = "Gosho Trapov"; + // Technology technology = new Technology + // { + // Name = name + // }; + // UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel + // { + // Name = name, + // Id = id + // }; + + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); + // this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + // bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); + + // Assert.AreEqual(shouldPass, result); + // }).GetAwaiter().GetResult(); + // } + + // [Test] + // public void UpdateTechnology_ThrowsException_WhenTechnologyDoesNotExist() + // { + // Task.Run(async () => + // { + // string exceptionMessage = "Technology does not exist!"; + // Guid id = new Guid(); + // UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel + // { + // Id = id + // }; + + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + + // try + // { + // await this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel); + // Assert.Fail("UpdateTechnology does not throw exception when technology does not exist"); + // } + // catch (ArgumentException ex) + // { + // Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); + // } + // }).GetAwaiter().GetResult(); + // } + + // [Test] + // public void UpdateTechnology_ThrowsException_WhenTechnologyNameAlreadyExists() + // { + // Task.Run(async () => + // { + // string exceptionMessage = "Technology name already exists!"; + // Guid id = new Guid(); + // UpdateTechnologyServiceModel updateTechnologyServiceModel = new UpdateTechnologyServiceModel + // { + // Id = id + // }; + + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); + + // try + // { + // await this.TechnologyService.UpdateTechnology(updateTechnologyServiceModel); + // Assert.Fail("UpdateTechnology does not throw exception when technology name already exist"); + // } + // catch (ArgumentException ex) + // { + // Assert.AreEqual(exceptionMessage, ex.Message, "Exception Message is not correct"); + // } + // }).GetAwaiter().GetResult(); + // } + // #endregion + + // #region Delete + // [Test] + // [TestCase(true)] + // [TestCase(false)] + // public void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) + // { + // Task.Run(async () => + // { + // Guid id = new Guid(); + // Technology technology = new Technology(); + + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + // this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + // this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + + // bool result = await this.TechnologyService.DeleteTechnology(id); + + // Assert.AreEqual(shouldPass, result); + // }).GetAwaiter().GetResult(); + // } + + // [Test] + // public void DeleteTechnology_ThrowsException_WhenTechnologyDoesNotExist() + // { + // Task.Run(async () => + // { + // string exceptionMessage = "Technology does not exist!"; + // Guid id = new Guid(); + + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + + // try + // { + // await this.TechnologyService.DeleteTechnology(id); + // Assert.Fail("DeleteTechnology does not throw exception when technology does not exist"); + // } + // catch (ArgumentException ex) + // { + // Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + // } + // }).GetAwaiter().GetResult(); + // } + // #endregion + // //Task.Run(async () => + // //{ + // // + // //}).GetAwaiter().GetResult(); + } +} diff --git a/src/DevHive.code-workspace b/src/DevHive.code-workspace index 04a9a2f..675cfea 100644 --- a/src/DevHive.code-workspace +++ b/src/DevHive.code-workspace @@ -37,7 +37,8 @@ "e2e" : true, }, "code-runner.fileDirectoryAsCwd": true, - "compile-hero.disable-compile-files-on-did-save-code": true + "dotnet-test-explorer.runInParallel": true, + "dotnet-test-explorer.testProjectPath": "**/*.Tests.csproj", }, "launch": { "configurations": [ -- cgit v1.2.3 From 2948a92492141f4d807449191901f499530d8465 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 14 Jan 2021 00:12:48 +0200 Subject: Fixed issues in Language & Technology and their interactions with User --- src/DevHive.Data/Interfaces/ILanguageRepository.cs | 3 +- .../Interfaces/ITechnologyRepository.cs | 5 +- src/DevHive.Data/Interfaces/IUserRepository.cs | 11 +-- .../Repositories/LanguageRepository.cs | 10 ++- .../Repositories/TechnologyRepository.cs | 10 ++- src/DevHive.Data/Repositories/UserRepository.cs | 5 ++ src/DevHive.Services/Interfaces/IUserService.cs | 6 +- .../Technology/CreateTechnologyServiceModel.cs | 4 +- .../Technology/UpdateTechnologyServiceModel.cs | 7 +- src/DevHive.Services/Services/TechnologyService.cs | 6 +- src/DevHive.Services/Services/UserService.cs | 100 ++++++++++++--------- .../TechnologyRepository.Tests.cs | 14 +-- .../TechnologyServices.Tests.cs | 8 +- src/DevHive.Web/Controllers/UserController.cs | 1 - .../Models/Language/UpdateLanguageWebModel.cs | 5 +- .../Models/Technology/CreateTechnologyWebModel.cs | 4 +- .../Models/Technology/UpdateTechnologyWebModel.cs | 4 +- 17 files changed, 120 insertions(+), 83 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/DevHive.Data/Interfaces/ILanguageRepository.cs index 40dd461..0612116 100644 --- a/src/DevHive.Data/Interfaces/ILanguageRepository.cs +++ b/src/DevHive.Data/Interfaces/ILanguageRepository.cs @@ -9,5 +9,6 @@ namespace DevHive.Data.Interfaces { Task DoesLanguageExistAsync(Guid id); Task DoesLanguageNameExistAsync(string languageName); + Task GetByNameAsync(string name); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/DevHive.Data/Interfaces/ITechnologyRepository.cs index 7c126a4..d0de096 100644 --- a/src/DevHive.Data/Interfaces/ITechnologyRepository.cs +++ b/src/DevHive.Data/Interfaces/ITechnologyRepository.cs @@ -8,6 +8,7 @@ namespace DevHive.Data.Interfaces public interface ITechnologyRepository : IRepository { Task DoesTechnologyExistAsync(Guid id); - Task DoesTechnologyNameExist(string technologyName); + Task DoesTechnologyNameExistAsync(string technologyName); + Task GetByNameAsync(string name); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Interfaces/IUserRepository.cs b/src/DevHive.Data/Interfaces/IUserRepository.cs index 8ee5054..bca8f71 100644 --- a/src/DevHive.Data/Interfaces/IUserRepository.cs +++ b/src/DevHive.Data/Interfaces/IUserRepository.cs @@ -11,7 +11,7 @@ namespace DevHive.Data.Interfaces Task AddFriendAsync(User user, User friend); Task AddLanguageToUserAsync(User user, Language language); Task AddTechnologyToUserAsync(User user, Technology technology); - + Task GetByUsername(string username); Language GetUserLanguage(User user, Language language); IList GetUserLanguages(User user); @@ -26,12 +26,13 @@ namespace DevHive.Data.Interfaces Task RemoveLanguageFromUserAsync(User user, Language language); Task RemoveTechnologyFromUserAsync(User user, Technology technology); - bool DoesUserHaveThisLanguage(User user, Language language); - bool DoesUserHaveThisUsername(Guid id, string username); - bool DoesUserHaveFriends(User user); 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); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index c30d3bb..b867a93 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -36,6 +36,12 @@ namespace DevHive.Data.Repositories .Set() .FindAsync(id); } + + public async Task GetByNameAsync(string languageName) + { + return await this._context.Languages + .FirstOrDefaultAsync(x => x.Name == languageName); + } #endregion #region Update @@ -62,7 +68,7 @@ namespace DevHive.Data.Repositories } #endregion - #region Validations + #region Validations public async Task DoesLanguageNameExistAsync(string languageName) { @@ -77,4 +83,4 @@ namespace DevHive.Data.Repositories } #endregion } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index a8208b6..d81433c 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -27,6 +27,12 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + + public async Task GetByNameAsync(string technologyName) + { + return await this._context.Technologies + .FirstOrDefaultAsync(x => x.Name == technologyName); + } #endregion #region Read @@ -65,7 +71,7 @@ namespace DevHive.Data.Repositories #region Validations - public async Task DoesTechnologyNameExist(string technologyName) + public async Task DoesTechnologyNameExistAsync(string technologyName) { return await this._context .Set() @@ -80,4 +86,4 @@ namespace DevHive.Data.Repositories } #endregion } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 1f29bb5..b4deacd 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -226,6 +226,11 @@ namespace DevHive.Data.Repositories { return user.Langauges.Contains(language); } + + public bool DoesUserHaveThisTechnology(User user, Technology technology) + { + return user.Technologies.Contains(technology); + } #endregion } } diff --git a/src/DevHive.Services/Interfaces/IUserService.cs b/src/DevHive.Services/Interfaces/IUserService.cs index ba53563..5ef141f 100644 --- a/src/DevHive.Services/Interfaces/IUserService.cs +++ b/src/DevHive.Services/Interfaces/IUserService.cs @@ -20,12 +20,12 @@ namespace DevHive.Services.Interfaces Task GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - + Task DeleteUser(Guid id); Task RemoveFriend(Guid userId, Guid friendId); Task RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel); Task RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel); - + Task ValidJWT(Guid id, string rawTokenData); } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Models/Technology/CreateTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/CreateTechnologyServiceModel.cs index ca848f9..a31d160 100644 --- a/src/DevHive.Services/Models/Technology/CreateTechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/CreateTechnologyServiceModel.cs @@ -2,8 +2,8 @@ using System; namespace DevHive.Services.Models.Technology { - public class CreateTechnologyServiceModel : TechnologyServiceModel + public class CreateTechnologyServiceModel { public string Name { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs index bfeae51..a18e286 100644 --- a/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs +++ b/src/DevHive.Services/Models/Technology/UpdateTechnologyServiceModel.cs @@ -2,5 +2,8 @@ using System; namespace DevHive.Services.Models.Technology { - public class UpdateTechnologyServiceModel : CreateTechnologyServiceModel {} -} \ No newline at end of file + public class UpdateTechnologyServiceModel : TechnologyServiceModel + { + public string Name { get; set; } + } +} diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index cb8fdfc..2b24ed6 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -23,7 +23,7 @@ namespace DevHive.Services.Services public async Task Create(CreateTechnologyServiceModel technologyServiceModel) { - if (await this._technologyRepository.DoesTechnologyNameExist(technologyServiceModel.Name)) + if (await this._technologyRepository.DoesTechnologyNameExistAsync(technologyServiceModel.Name)) throw new ArgumentException("Technology already exists!"); Technology technology = this._technologyMapper.Map(technologyServiceModel); @@ -53,7 +53,7 @@ namespace DevHive.Services.Services if (!await this._technologyRepository.DoesTechnologyExistAsync(updateTechnologyServiceModel.Id)) throw new ArgumentException("Technology does not exist!"); - if (await this._technologyRepository.DoesTechnologyNameExist(updateTechnologyServiceModel.Name)) + if (await this._technologyRepository.DoesTechnologyNameExistAsync(updateTechnologyServiceModel.Name)) throw new ArgumentException("Technology name already exists!"); Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); @@ -77,4 +77,4 @@ namespace DevHive.Services.Services } #endregion } -} \ No newline at end of file +} diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 6a6662d..012ec1b 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -111,19 +111,42 @@ namespace DevHive.Services.Services public async Task AddLanguageToUser(Guid userId, LanguageServiceModel languageServiceModel) { - Tuple tuple = await ValidateUserAndLanguage(userId, 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 noy exist!"); + + User user = await this._userRepository.GetByIdAsync(userId); + Language language = await this._languageRepository.GetByIdAsync(languageServiceModel.Id); - if (this._userRepository.DoesUserHaveThisLanguage(tuple.Item1, tuple.Item2)) + if (this._userRepository.DoesUserHaveThisLanguage(user, language)) throw new ArgumentException("User already has this language!"); - return await this._userRepository.AddLanguageToUserAsync(tuple.Item1, tuple.Item2); + return await this._userRepository.AddLanguageToUserAsync(user, language); } public async Task AddTechnologyToUser(Guid userId, TechnologyServiceModel technologyServiceModel) { - Tuple tuple = await ValidateUserAndTechnology(userId, 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("Technology does not exist!"); + + Technology technology = await this._technologyRepository.GetByIdAsync(technologyServiceModel.Id); + User user = await this._userRepository.GetByIdAsync(userId); + + if (this._userRepository.DoesUserHaveThisTechnology(user, technology)) + throw new ArgumentException("User already has this language!"); - return await this._userRepository.AddTechnologyToUserAsync(tuple.Item1, tuple.Item2); + return await this._userRepository.AddTechnologyToUserAsync(user, technology); } #endregion @@ -204,19 +227,42 @@ namespace DevHive.Services.Services public async Task RemoveLanguageFromUser(Guid userId, LanguageServiceModel languageServiceModel) { - Tuple tuple = await ValidateUserAndLanguage(userId, languageServiceModel); + bool userExists = await this._userRepository.DoesUserExistAsync(userId); + bool languageExists = await this._languageRepository.DoesLanguageExistAsync(languageServiceModel.Id); - if (!this._userRepository.DoesUserHaveThisLanguage(tuple.Item1, tuple.Item2)) + 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(tuple.Item1, tuple.Item2); + return await this._userRepository.RemoveLanguageFromUserAsync(user, language); } public async Task RemoveTechnologyFromUser(Guid userId, TechnologyServiceModel technologyServiceModel) { - Tuple tuple = await ValidateUserAndTechnology(userId, 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!"); - return await this._userRepository.RemoveTechnologyFromUserAsync(tuple.Item1, tuple.Item2); + 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 @@ -298,40 +344,6 @@ namespace DevHive.Services.Services { return string.Join(string.Empty, SHA512.HashData(Encoding.ASCII.GetBytes(password))); } - - private async Task> ValidateUserAndLanguage(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); - - return new Tuple(user, language); - } - - private async Task> ValidateUserAndTechnology(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); - - return new Tuple(user, technology); - } #endregion } } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 54cf7c0..6ff5fa2 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -106,7 +106,7 @@ namespace DevHive.Data.Tests } #endregion - #region DoesTechnologyNameExist + #region DoesTechnologyNameExistAsync [Test] public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() { @@ -114,20 +114,20 @@ namespace DevHive.Data.Tests { AddEntity(); - bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME); + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); }).GetAwaiter().GetResult(); - } + } [Test] public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() { Task.Run(async () => { - bool result = await this.TechnologyRepository.DoesTechnologyNameExist(TECHNOLOGY_NAME); + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExist returns true when technology name does not exist"); + Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); }).GetAwaiter().GetResult(); } #endregion @@ -162,7 +162,7 @@ namespace DevHive.Data.Tests #region DeleteAsync [Test] public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() - { + { Task.Run(async () => { AddEntity(); @@ -173,7 +173,7 @@ namespace DevHive.Data.Tests Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); }).GetAwaiter().GetResult(); - } + } #endregion #region HelperMethods diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index f2232a8..e56c508 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -45,7 +45,7 @@ namespace DevHive.Services.Tests Name = technologyName }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(shouldFail)); this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); @@ -72,7 +72,7 @@ namespace DevHive.Services.Tests Name = technologyName }; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); try { @@ -158,7 +158,7 @@ namespace DevHive.Services.Tests // }; // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(false)); + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); // this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); // this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); @@ -207,7 +207,7 @@ namespace DevHive.Services.Tests // }; // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExist(It.IsAny())).Returns(Task.FromResult(true)); + // this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); // try // { diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 26271b2..5ba382f 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -176,7 +176,6 @@ namespace DevHive.Web.Controllers new OkResult() : new BadRequestResult(); } - #endregion } } diff --git a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs index deca0fc..ed3b37c 100644 --- a/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs +++ b/src/DevHive.Web/Models/Language/UpdateLanguageWebModel.cs @@ -2,5 +2,8 @@ using System; namespace DevHive.Web.Models.Language { - public class UpdateLanguageWebModel : CreateLanguageWebModel { } + public class UpdateLanguageWebModel : LanguageWebModel + { + public string Name { get; set; } + } } diff --git a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs index 27da4a0..13029ee 100644 --- a/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/CreateTechnologyWebModel.cs @@ -1,7 +1,7 @@ namespace DevHive.Web.Models.Technology { - public class CreateTechnologyWebModel : TechnologyWebModel + public class CreateTechnologyWebModel { public string Name { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs index d395c9f..8bf48bf 100644 --- a/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs +++ b/src/DevHive.Web/Models/Technology/UpdateTechnologyWebModel.cs @@ -2,8 +2,8 @@ using System; namespace DevHive.Web.Models.Technology { - public class UpdateTechnologyWebModel + public class UpdateTechnologyWebModel : TechnologyWebModel { public string Name { get; set; } } -} \ No newline at end of file +} -- cgit v1.2.3 From 5514f1109cb3689fa81b29bb2d7dcf84cc05f65f Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 15 Jan 2021 16:45:30 +0200 Subject: Extracted Interfaces from every DevHive.Data class; Tidied up the DevHive.Interfaces --- src/DevHive.Data/Interfaces/ILanguageRepository.cs | 14 -------- src/DevHive.Data/Interfaces/IPostRepository.cs | 20 ------------ src/DevHive.Data/Interfaces/IRepository.cs | 21 ------------ src/DevHive.Data/Interfaces/IRoleRepository.cs | 15 --------- .../Interfaces/ITechnologyRepository.cs | 14 -------- src/DevHive.Data/Interfaces/IUserRepository.cs | 38 ---------------------- src/DevHive.Data/Interfaces/Models/IComment.cs | 11 +++++++ src/DevHive.Data/Interfaces/Models/ILanguage.cs | 7 ++++ src/DevHive.Data/Interfaces/Models/IModel.cs | 9 +++++ src/DevHive.Data/Interfaces/Models/IPost.cs | 13 ++++++++ src/DevHive.Data/Interfaces/Models/IRole.cs | 10 ++++++ src/DevHive.Data/Interfaces/Models/ITechnology.cs | 7 ++++ src/DevHive.Data/Interfaces/Models/IUser.cs | 16 +++++++++ .../Interfaces/Repositories/ILanguageRepository.cs | 14 ++++++++ .../Interfaces/Repositories/IPostRepository.cs | 20 ++++++++++++ .../Interfaces/Repositories/IRepository.cs | 21 ++++++++++++ .../Interfaces/Repositories/IRoleRepository.cs | 15 +++++++++ .../Repositories/ITechnologyRepository.cs | 14 ++++++++ .../Interfaces/Repositories/IUserRepository.cs | 38 ++++++++++++++++++++++ src/DevHive.Data/Models/Comment.cs | 6 ++-- src/DevHive.Data/Models/IModel.cs | 9 ----- src/DevHive.Data/Models/Language.cs | 4 ++- src/DevHive.Data/Models/Post.cs | 3 +- src/DevHive.Data/Models/Role.cs | 7 ++-- src/DevHive.Data/Models/Technology.cs | 3 +- src/DevHive.Data/Models/User.cs | 3 +- .../Repositories/LanguageRepository.cs | 2 +- src/DevHive.Data/Repositories/PostRepository.cs | 4 +-- src/DevHive.Data/Repositories/RoleRepository.cs | 2 +- .../Repositories/TechnologyRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 2 +- src/DevHive.Services/Services/LanguageService.cs | 2 +- src/DevHive.Services/Services/PostService.cs | 2 +- src/DevHive.Services/Services/RoleService.cs | 2 +- src/DevHive.Services/Services/TechnologyService.cs | 2 +- src/DevHive.Services/Services/UserService.cs | 2 +- .../DevHive.Data.Tests/DevHive.Data.Tests.csproj | 1 + .../DevHive.Data.Tests/UserRepositoryTests.cs | 17 +++++++++- .../TechnologyServices.Tests.cs | 2 +- .../Extensions/ConfigureDependencyInjection.cs | 2 +- src/DevHive.Web/DevHive.Web.csproj | 31 +++++++++--------- 41 files changed, 257 insertions(+), 170 deletions(-) delete mode 100644 src/DevHive.Data/Interfaces/ILanguageRepository.cs delete mode 100644 src/DevHive.Data/Interfaces/IPostRepository.cs delete mode 100644 src/DevHive.Data/Interfaces/IRepository.cs delete mode 100644 src/DevHive.Data/Interfaces/IRoleRepository.cs delete mode 100644 src/DevHive.Data/Interfaces/ITechnologyRepository.cs delete mode 100644 src/DevHive.Data/Interfaces/IUserRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Models/IComment.cs create mode 100644 src/DevHive.Data/Interfaces/Models/ILanguage.cs create mode 100644 src/DevHive.Data/Interfaces/Models/IModel.cs create mode 100644 src/DevHive.Data/Interfaces/Models/IPost.cs create mode 100644 src/DevHive.Data/Interfaces/Models/IRole.cs create mode 100644 src/DevHive.Data/Interfaces/Models/ITechnology.cs create mode 100644 src/DevHive.Data/Interfaces/Models/IUser.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/IRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/IRoleRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs create mode 100644 src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs delete mode 100644 src/DevHive.Data/Models/IModel.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Interfaces/ILanguageRepository.cs b/src/DevHive.Data/Interfaces/ILanguageRepository.cs deleted file mode 100644 index 0612116..0000000 --- a/src/DevHive.Data/Interfaces/ILanguageRepository.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - -namespace DevHive.Data.Interfaces -{ - public interface ILanguageRepository : IRepository - { - Task DoesLanguageExistAsync(Guid id); - Task DoesLanguageNameExistAsync(string languageName); - Task GetByNameAsync(string name); - } -} diff --git a/src/DevHive.Data/Interfaces/IPostRepository.cs b/src/DevHive.Data/Interfaces/IPostRepository.cs deleted file mode 100644 index a02fd08..0000000 --- a/src/DevHive.Data/Interfaces/IPostRepository.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - -namespace DevHive.Data.Interfaces -{ - public interface IPostRepository : IRepository - { - Task AddCommentAsync(Comment entity); - - Task GetCommentByIdAsync(Guid id); - - Task EditCommentAsync(Comment newEntity); - - Task DeleteCommentAsync(Comment entity); - Task DoesCommentExist(Guid id); - Task DoesPostExist(Guid postId); - } -} diff --git a/src/DevHive.Data/Interfaces/IRepository.cs b/src/DevHive.Data/Interfaces/IRepository.cs deleted file mode 100644 index 40a78de..0000000 --- a/src/DevHive.Data/Interfaces/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Interfaces -{ - public interface IRepository - where TEntity : class - { - //Add Entity to database - Task AddAsync(TEntity entity); - - //Find entity by id - Task GetByIdAsync(Guid id); - - //Modify Entity from database - Task EditAsync(TEntity newEntity); - - //Delete Entity from database - Task DeleteAsync(TEntity entity); - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/IRoleRepository.cs b/src/DevHive.Data/Interfaces/IRoleRepository.cs deleted file mode 100644 index a1080bb..0000000 --- a/src/DevHive.Data/Interfaces/IRoleRepository.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - -namespace DevHive.Data.Interfaces -{ - public interface IRoleRepository : IRepository - { - Task GetByNameAsync(string name); - - Task DoesNameExist(string name); - Task DoesRoleExist(Guid id); - } -} diff --git a/src/DevHive.Data/Interfaces/ITechnologyRepository.cs b/src/DevHive.Data/Interfaces/ITechnologyRepository.cs deleted file mode 100644 index d0de096..0000000 --- a/src/DevHive.Data/Interfaces/ITechnologyRepository.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - -namespace DevHive.Data.Interfaces -{ - public interface ITechnologyRepository : IRepository - { - Task DoesTechnologyExistAsync(Guid id); - Task DoesTechnologyNameExistAsync(string technologyName); - Task GetByNameAsync(string name); - } -} diff --git a/src/DevHive.Data/Interfaces/IUserRepository.cs b/src/DevHive.Data/Interfaces/IUserRepository.cs deleted file mode 100644 index a4a6fdd..0000000 --- a/src/DevHive.Data/Interfaces/IUserRepository.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading.Tasks; -using DevHive.Data.Models; -using DevHive.Data.Repositories.Interfaces; - -namespace DevHive.Data.Interfaces -{ - public interface IUserRepository : IRepository - { - Task AddFriendAsync(User user, User friend); - Task AddLanguageToUserAsync(User user, Language language); - Task AddTechnologyToUserAsync(User user, Technology technology); - - Task GetByUsernameAsync(string username); - Language GetUserLanguage(User user, Language language); - IList GetUserLanguages(User user); - IList GetUserTechnologies(User user); - Technology GetUserTechnology(User user, Technology technology); - IEnumerable QueryAll(); - - Task EditUserLanguageAsync(User user, Language oldLang, Language newLang); - Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech); - - Task RemoveFriendAsync(User user, User friend); - Task RemoveLanguageFromUserAsync(User user, Language language); - Task RemoveTechnologyFromUserAsync(User user, Technology technology); - - 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); - } -} diff --git a/src/DevHive.Data/Interfaces/Models/IComment.cs b/src/DevHive.Data/Interfaces/Models/IComment.cs new file mode 100644 index 0000000..f6afb3f --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IComment.cs @@ -0,0 +1,11 @@ +using System; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IComment : IModel + { + Guid IssuerId { get; set; } + string Message { get; set; } + DateTime TimeCreated { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/ILanguage.cs b/src/DevHive.Data/Interfaces/Models/ILanguage.cs new file mode 100644 index 0000000..f757a3f --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/ILanguage.cs @@ -0,0 +1,7 @@ +namespace DevHive.Data.Interfaces.Models +{ + public interface ILanguage : IModel + { + string Name { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IModel.cs b/src/DevHive.Data/Interfaces/Models/IModel.cs new file mode 100644 index 0000000..f903af3 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IModel.cs @@ -0,0 +1,9 @@ +using System; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IModel + { + Guid Id { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IPost.cs b/src/DevHive.Data/Interfaces/Models/IPost.cs new file mode 100644 index 0000000..117d859 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IPost.cs @@ -0,0 +1,13 @@ +using System; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IPost : IModel + { + Guid IssuerId { get; set; } + DateTime TimeCreated { get; set; } + string Message { get; set; } + Comment[] Comments { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IRole.cs b/src/DevHive.Data/Interfaces/Models/IRole.cs new file mode 100644 index 0000000..0623f07 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IRole.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IRole + { + List Users { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/ITechnology.cs b/src/DevHive.Data/Interfaces/Models/ITechnology.cs new file mode 100644 index 0000000..9bd97f9 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/ITechnology.cs @@ -0,0 +1,7 @@ +namespace DevHive.Data.Interfaces.Models +{ + public interface ITechnology : IModel + { + string Name { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Models/IUser.cs b/src/DevHive.Data/Interfaces/Models/IUser.cs new file mode 100644 index 0000000..0a770f0 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Models/IUser.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using DevHive.Data.Models; + +namespace DevHive.Data.Interfaces.Models +{ + public interface IUser : IModel + { + string FirstName { get; set; } + string LastName { get; set; } + string ProfilePictureUrl { get; set; } + IList Langauges { get; set; } + IList Technologies { get; set; } + IList Roles { get; set; } + IList Friends { get; set; } + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs new file mode 100644 index 0000000..f1d7248 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/ILanguageRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface ILanguageRepository : IRepository + { + Task DoesLanguageExistAsync(Guid id); + Task DoesLanguageNameExistAsync(string languageName); + Task GetByNameAsync(string name); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs new file mode 100644 index 0000000..913d8c4 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IPostRepository.cs @@ -0,0 +1,20 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface IPostRepository : IRepository + { + Task AddCommentAsync(Comment entity); + + Task GetCommentByIdAsync(Guid id); + + Task EditCommentAsync(Comment newEntity); + + Task DeleteCommentAsync(Comment entity); + Task DoesCommentExist(Guid id); + Task DoesPostExist(Guid postId); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs new file mode 100644 index 0000000..40a78de --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories.Interfaces +{ + public interface IRepository + where TEntity : class + { + //Add Entity to database + Task AddAsync(TEntity entity); + + //Find entity by id + Task GetByIdAsync(Guid id); + + //Modify Entity from database + Task EditAsync(TEntity newEntity); + + //Delete Entity from database + Task DeleteAsync(TEntity entity); + } +} \ No newline at end of file diff --git a/src/DevHive.Data/Interfaces/Repositories/IRoleRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRoleRepository.cs new file mode 100644 index 0000000..e834369 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IRoleRepository.cs @@ -0,0 +1,15 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface IRoleRepository : IRepository + { + Task GetByNameAsync(string name); + + Task DoesNameExist(string name); + Task DoesRoleExist(Guid id); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs b/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs new file mode 100644 index 0000000..fb0ba20 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/ITechnologyRepository.cs @@ -0,0 +1,14 @@ +using System; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface ITechnologyRepository : IRepository + { + Task DoesTechnologyExistAsync(Guid id); + Task DoesTechnologyNameExistAsync(string technologyName); + Task GetByNameAsync(string name); + } +} diff --git a/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs new file mode 100644 index 0000000..3a22911 --- /dev/null +++ b/src/DevHive.Data/Interfaces/Repositories/IUserRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Models; +using DevHive.Data.Repositories.Interfaces; + +namespace DevHive.Data.Interfaces.Repositories +{ + public interface IUserRepository : IRepository + { + Task AddFriendAsync(User user, User friend); + Task AddLanguageToUserAsync(User user, Language language); + Task AddTechnologyToUserAsync(User user, Technology technology); + + Task GetByUsernameAsync(string username); + Language GetUserLanguage(User user, Language language); + IList GetUserLanguages(User user); + IList GetUserTechnologies(User user); + Technology GetUserTechnology(User user, Technology technology); + IEnumerable QueryAll(); + + Task EditUserLanguageAsync(User user, Language oldLang, Language newLang); + Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech); + + Task RemoveFriendAsync(User user, User friend); + Task RemoveLanguageFromUserAsync(User user, Language language); + Task RemoveTechnologyFromUserAsync(User user, Technology technology); + + 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); + } +} diff --git a/src/DevHive.Data/Models/Comment.cs b/src/DevHive.Data/Models/Comment.cs index 8cf848f..a07bd59 100644 --- a/src/DevHive.Data/Models/Comment.cs +++ b/src/DevHive.Data/Models/Comment.cs @@ -1,11 +1,13 @@ using System; +using DevHive.Data.Interfaces.Models; + namespace DevHive.Data.Models { - public class Comment : IModel + public class Comment : IComment { public Guid Id { get; set; } public Guid IssuerId { get; set; } public string Message { get; set; } public DateTime TimeCreated { get; set; } } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Models/IModel.cs b/src/DevHive.Data/Models/IModel.cs deleted file mode 100644 index 64942ee..0000000 --- a/src/DevHive.Data/Models/IModel.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System; - -namespace DevHive.Data.Models -{ - interface IModel - { - Guid Id { get; set; } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Models/Language.cs b/src/DevHive.Data/Models/Language.cs index 556d019..2983107 100644 --- a/src/DevHive.Data/Models/Language.cs +++ b/src/DevHive.Data/Models/Language.cs @@ -1,7 +1,9 @@ using System; +using DevHive.Data.Interfaces.Models; + namespace DevHive.Data.Models { - public class Language : IModel + public class Language : ILanguage { public Guid Id { get; set; } public string Name { get; set; } diff --git a/src/DevHive.Data/Models/Post.cs b/src/DevHive.Data/Models/Post.cs index a5abf12..54576b7 100644 --- a/src/DevHive.Data/Models/Post.cs +++ b/src/DevHive.Data/Models/Post.cs @@ -1,10 +1,11 @@ using System; using System.ComponentModel.DataAnnotations.Schema; +using DevHive.Data.Interfaces.Models; namespace DevHive.Data.Models { [Table("Posts")] - public class Post + public class Post : IPost { public Guid Id { get; set; } diff --git a/src/DevHive.Data/Models/Role.cs b/src/DevHive.Data/Models/Role.cs index 63e6c7c..e63f007 100644 --- a/src/DevHive.Data/Models/Role.cs +++ b/src/DevHive.Data/Models/Role.cs @@ -1,12 +1,13 @@ -using System; -using Microsoft.AspNetCore.Identity; using System.ComponentModel.DataAnnotations.Schema; using System.Collections.Generic; +using DevHive.Data.Interfaces.Models; +using Microsoft.AspNetCore.Identity; +using System; namespace DevHive.Data.Models { [Table("Roles")] - public class Role : IdentityRole + public class Role : IdentityRole, IRole { public const string DefaultRole = "User"; public const string AdminRole = "Admin"; diff --git a/src/DevHive.Data/Models/Technology.cs b/src/DevHive.Data/Models/Technology.cs index a462d20..36cec32 100644 --- a/src/DevHive.Data/Models/Technology.cs +++ b/src/DevHive.Data/Models/Technology.cs @@ -1,8 +1,9 @@ using System; +using DevHive.Data.Interfaces.Models; namespace DevHive.Data.Models { - public class Technology : IModel + public class Technology : ITechnology { public Guid Id { get; set; } public string Name { get; set; } diff --git a/src/DevHive.Data/Models/User.cs b/src/DevHive.Data/Models/User.cs index 7a213c7..944bf6a 100644 --- a/src/DevHive.Data/Models/User.cs +++ b/src/DevHive.Data/Models/User.cs @@ -1,12 +1,13 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; +using DevHive.Data.Interfaces.Models; using Microsoft.AspNetCore.Identity; namespace DevHive.Data.Models { [Table("Users")] - public class User : IdentityUser, IModel + public class User : IdentityUser, IUser { public string FirstName { get; set; } diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index b867a93..e644fc4 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index f5e9b7b..3be14e3 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -107,4 +107,4 @@ namespace DevHive.Data.Repositories } #endregion } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 4cd5b79..ca3fb8b 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index d81433c..1631972 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using DevHive.Common.Models.Misc; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index da08a5a..c06fef6 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using DevHive.Common.Models.Misc; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index c34537f..be035c2 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; using DevHive.Services.Models.Language; diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 24ca8f3..6e83ad4 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -8,7 +8,7 @@ using DevHive.Services.Models.Post.Post; using System.IdentityModel.Tokens.Jwt; using System.Security.Claims; using DevHive.Services.Interfaces; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; namespace DevHive.Services.Services { diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index fd56c2c..c38ac74 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -2,7 +2,7 @@ using System; using System.Threading.Tasks; using AutoMapper; using DevHive.Common.Models.Identity; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 7fd0b2f..d8b7262 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; using AutoMapper; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Interfaces; using DevHive.Services.Models.Technology; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 6619f60..ae657cc 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -15,7 +15,7 @@ using DevHive.Services.Models.Language; using DevHive.Services.Interfaces; using DevHive.Services.Models.Technology; using DevHive.Data.Repositories; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; namespace DevHive.Services.Services { diff --git a/src/DevHive.Tests/DevHive.Data.Tests/DevHive.Data.Tests.csproj b/src/DevHive.Tests/DevHive.Data.Tests/DevHive.Data.Tests.csproj index 509ceef..81e7b2b 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/DevHive.Data.Tests.csproj +++ b/src/DevHive.Tests/DevHive.Data.Tests/DevHive.Data.Tests.csproj @@ -8,6 +8,7 @@ + diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index a88de7f..81f62db 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -1,5 +1,10 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using DevHive.Data.Models; using DevHive.Data.Repositories; using Microsoft.EntityFrameworkCore; +using Moq; using NUnit.Framework; namespace DevHive.Data.Tests @@ -9,19 +14,29 @@ namespace DevHive.Data.Tests { private DevHiveContext _context; private UserRepository _userRepository; + private User _dummyUser; [SetUp] public void Setup() { + //Naming convention: MethodName_ExpectedBehavior_StateUnderTest var options = new DbContextOptionsBuilder() .UseInMemoryDatabase("DevHive_UserRepository_Database"); this._context = new DevHiveContext(options.Options); this._userRepository = new UserRepository(_context); + + this._dummyUser = new Mock().Object; + + foreach (var item in _dummyUser.Langauges) + System.Console.WriteLine(item); + + foreach (var item in _dummyUser.Technologies) + System.Console.WriteLine(item); } [Test] - public void Test() + public void AddAsync_ShouldAddUserToDatabase() { } diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index 1f0b01e..d028957 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -1,5 +1,5 @@ using AutoMapper; -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Services.Models.Technology; using DevHive.Services.Services; diff --git a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs index e1601e7..f93f801 100644 --- a/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs +++ b/src/DevHive.Web/Configurations/Extensions/ConfigureDependencyInjection.cs @@ -1,4 +1,4 @@ -using DevHive.Data.Interfaces; +using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using DevHive.Data.Repositories; using DevHive.Services.Interfaces; diff --git a/src/DevHive.Web/DevHive.Web.csproj b/src/DevHive.Web/DevHive.Web.csproj index 84cd92f..3bfa507 100644 --- a/src/DevHive.Web/DevHive.Web.csproj +++ b/src/DevHive.Web/DevHive.Web.csproj @@ -2,28 +2,27 @@ net5.0 + + true + latest + - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - - - - - - + + + + + + - - + + - - - true - latest - \ No newline at end of file -- cgit v1.2.3 From 33b5f6297c2c975bec8a74a8facc208261c03c9e Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 19 Jan 2021 20:36:39 +0200 Subject: Replaced RepoMethods with BaseClass and inherited it where needed --- .../Models/Data/RepositoryMethods.cs | 15 -------------- src/DevHive.Data/Repositories/BaseRepository.cs | 15 ++++++++++++++ .../Repositories/LanguageRepository.cs | 8 ++++---- src/DevHive.Data/Repositories/PostRepository.cs | 14 ++++++------- src/DevHive.Data/Repositories/RoleRepository.cs | 8 ++++---- .../Repositories/TechnologyRepository.cs | 8 ++++---- src/DevHive.Data/Repositories/UserRepository.cs | 24 +++++++++++----------- 7 files changed, 46 insertions(+), 46 deletions(-) delete mode 100644 src/DevHive.Common/Models/Data/RepositoryMethods.cs create mode 100644 src/DevHive.Data/Repositories/BaseRepository.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Common/Models/Data/RepositoryMethods.cs b/src/DevHive.Common/Models/Data/RepositoryMethods.cs deleted file mode 100644 index bfd057f..0000000 --- a/src/DevHive.Common/Models/Data/RepositoryMethods.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System.Threading.Tasks; -using Microsoft.EntityFrameworkCore; - -namespace DevHive.Common.Models.Misc -{ - public static class RepositoryMethods - { - public static async Task SaveChangesAsync(DbContext context) - { - int result = await context.SaveChangesAsync(); - - return result >= 1; - } - } -} \ No newline at end of file diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs new file mode 100644 index 0000000..b0f0f3e --- /dev/null +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class BaseRepository + { + public async Task SaveChangesAsync(DbContext context) + { + int result = await context.SaveChangesAsync(); + + return result >= 1; + } + } +} diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index e644fc4..108b307 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : ILanguageRepository + public class LanguageRepository : BaseRepository, ILanguageRepository { private readonly DevHiveContext _context; @@ -24,7 +24,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -52,7 +52,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -64,7 +64,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index c5e8569..db2c4af 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : IPostRepository + public class PostRepository : BaseRepository, IPostRepository { private readonly DevHiveContext _context; @@ -23,7 +23,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(post); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddCommentAsync(Comment entity) @@ -32,7 +32,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Read @@ -71,7 +71,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newPost); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditCommentAsync(Comment newEntity) @@ -80,7 +80,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Delete @@ -90,7 +90,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(post); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task DeleteCommentAsync(Comment entity) @@ -99,7 +99,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #region Validations diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index ca3fb8b..684fbd7 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : IRoleRepository + public class RoleRepository : BaseRepository, IRoleRepository { private readonly DevHiveContext _context; @@ -23,7 +23,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Read @@ -51,7 +51,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } //Delete @@ -61,7 +61,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task DoesNameExist(string name) diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 73827a7..390ad3f 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class TechnologyRepository : ITechnologyRepository + public class TechnologyRepository : BaseRepository, ITechnologyRepository { private readonly DevHiveContext _context; @@ -25,7 +25,7 @@ namespace DevHive.Data.Repositories .Set() .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -52,7 +52,7 @@ namespace DevHive.Data.Repositories .Set() .Update(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -64,7 +64,7 @@ namespace DevHive.Data.Repositories .Set() .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 6d4a0bf..81c974c 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : IUserRepository + public class UserRepository : BaseRepository, IUserRepository { private readonly DevHiveContext _context; @@ -25,7 +25,7 @@ namespace DevHive.Data.Repositories await this._context.Users .AddAsync(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddFriendToUserAsync(User user, User friend) @@ -33,7 +33,7 @@ namespace DevHive.Data.Repositories this._context.Update(user); user.Friends.Add(friend); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddLanguageToUserAsync(User user, Language language) @@ -42,7 +42,7 @@ namespace DevHive.Data.Repositories user.Languages.Add(language); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task AddTechnologyToUserAsync(User user, Technology technology) @@ -51,7 +51,7 @@ namespace DevHive.Data.Repositories user.Technologies.Add(technology); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -116,7 +116,7 @@ namespace DevHive.Data.Repositories .CurrentValues .SetValues(newEntity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditUserLanguageAsync(User user, Language oldLang, Language newLang) @@ -126,7 +126,7 @@ namespace DevHive.Data.Repositories user.Languages.Remove(oldLang); user.Languages.Add(newLang); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task EditUserTechnologyAsync(User user, Technology oldTech, Technology newTech) @@ -136,7 +136,7 @@ namespace DevHive.Data.Repositories user.Technologies.Remove(oldTech); user.Technologies.Add(newTech); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion @@ -147,7 +147,7 @@ namespace DevHive.Data.Repositories this._context.Users .Remove(entity); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveFriendAsync(User user, User friend) @@ -155,7 +155,7 @@ namespace DevHive.Data.Repositories this._context.Update(user); user.Friends.Remove(friend); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveLanguageFromUserAsync(User user, Language language) @@ -164,7 +164,7 @@ namespace DevHive.Data.Repositories user.Languages.Remove(language); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } public async Task RemoveTechnologyFromUserAsync(User user, Technology technology) @@ -173,7 +173,7 @@ namespace DevHive.Data.Repositories user.Technologies.Remove(technology); - return await RepositoryMethods.SaveChangesAsync(this._context); + return await this.SaveChangesAsync(this._context); } #endregion -- cgit v1.2.3 From 9e86699c9b3aff17e0c4d19850b41b792a9625ef Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:12:04 +0200 Subject: Removed HTTP Patch; Refactored HTTP Put; Fixed Update bug --- .../Repositories/LanguageRepository.cs | 7 +- .../Repositories/TechnologyRepository.cs | 1 + src/DevHive.Data/Repositories/UserRepository.cs | 15 ++- .../Configurations/Mapping/RoleMapings.cs | 2 +- .../Configurations/Mapping/TechnologyMappings.cs | 2 + .../Mapping/UserCollectionMappings.cs | 22 ---- src/DevHive.Services/Interfaces/IUserService.cs | 1 - .../Models/Identity/Role/CreateRoleServiceModel.cs | 14 +++ .../Models/Identity/User/FriendServiceModel.cs | 3 + .../Identity/User/UpdateFriendServiceModel.cs | 10 ++ .../User/UpdateUserCollectionServiceModel.cs | 7 -- .../Models/Identity/User/UpdateUserServiceModel.cs | 11 +- src/DevHive.Services/Services/UserService.cs | 135 +++++++++------------ .../Configurations/Mapping/LanguageMappings.cs | 7 +- .../Configurations/Mapping/RoleMappings.cs | 9 +- .../Configurations/Mapping/TechnologyMappings.cs | 3 +- .../Configurations/Mapping/UserMappings.cs | 13 +- src/DevHive.Web/Controllers/UserController.cs | 14 --- .../Models/Identity/User/UpdateUserWebModel.cs | 5 + 19 files changed, 132 insertions(+), 149 deletions(-) delete mode 100644 src/DevHive.Services/Configurations/Mapping/UserCollectionMappings.cs create mode 100644 src/DevHive.Services/Models/Identity/Role/CreateRoleServiceModel.cs create mode 100644 src/DevHive.Services/Models/Identity/User/UpdateFriendServiceModel.cs delete mode 100644 src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 108b307..4c51cf3 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -20,8 +20,7 @@ namespace DevHive.Data.Repositories public async Task AddAsync(Language entity) { - await this._context - .Set() + await this._context.Languages .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -32,14 +31,14 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Languages .FindAsync(id); } public async Task GetByNameAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 390ad3f..a41d4fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -40,6 +40,7 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies + .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 3f9af70..c769f7e 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -6,6 +6,7 @@ using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking; namespace DevHive.Data.Repositories { @@ -78,7 +79,9 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .Include(u => u.Roles) + .AsNoTracking() + .Include(x => x.Languages) + .Include(x => x.Technologies) .FirstOrDefaultAsync(x => x.UserName == username); } @@ -107,9 +110,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(User newEntity) + public async Task EditAsync(User entity) { - this._context.Update(newEntity); + User user = await this._context.Users + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(user); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } @@ -177,6 +184,7 @@ namespace DevHive.Data.Repositories public async Task DoesUserExistAsync(Guid id) { return await this._context.Users + .AsNoTracking() .AnyAsync(x => x.Id == id); } @@ -208,6 +216,7 @@ namespace DevHive.Data.Repositories public bool DoesUserHaveThisUsername(Guid id, string username) { return this._context.Users + .AsNoTracking() .Any(x => x.Id == id && x.UserName == username); } 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(); CreateMap(); - CreateMap(); + CreateMap(); } } } 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(); CreateMap(); CreateMap(); + CreateMap(); CreateMap(); + CreateMap(); } } } 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(); - CreateMap(); - CreateMap(); - CreateMap(); - - CreateMap(); - CreateMap(); - CreateMap(); - CreateMap(); - } - } -} 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 GetUserById(Guid id); Task UpdateUser(UpdateUserServiceModel updateModel); - Task PatchUser(Guid id, List patch); Task DeleteUser(Guid id); Task 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/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/UpdateUserCollectionServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs deleted file mode 100644 index c40a980..0000000 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserCollectionServiceModel.cs +++ /dev/null @@ -1,7 +0,0 @@ -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 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 Roles { get; set; } = new HashSet(); + public HashSet Roles { get; set; } = new HashSet(); - public HashSet Friends { get; set; } = new HashSet(); + public HashSet Friends { get; set; } = new HashSet(); - public HashSet Languages { get; set; } = new HashSet(); + public HashSet Languages { get; set; } = new HashSet(); - public HashSet Technologies { get; set; } = new HashSet(); + public HashSet Technologies { get; set; } = new HashSet(); } } 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 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 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 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(role); - User user = this._userMapper.Map(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(language); - if (!successful) - throw new InvalidOperationException("Unable to edit user!"); + updateUserServiceModel.Languages.Add(updateLanguageServiceModel); + } - return this._userMapper.Map(user); ; - } + //Clean the already replaced languages + updateUserServiceModel.Languages.RemoveWhere(x => x.Id == Guid.Empty); - public async Task PatchUser(Guid id, List 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(user); + UpdateTechnologyServiceModel updateTechnologyServiceModel = this._userMapper.Map(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(user); - } - else - return null; + //Clean the already replaced technologies + updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); + + User user = this._userMapper.Map(updateUserServiceModel); + bool successful = await this._userRepository.EditAsync(user); + + if (!successful) + throw new InvalidOperationException("Unable to edit user!"); + + return this._userMapper.Map(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 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 } } diff --git a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs index 8cac3ca..eca0d1a 100644 --- a/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/LanguageMappings.cs @@ -8,10 +8,11 @@ namespace DevHive.Web.Configurations.Mapping { public LanguageMappings() { - CreateMap(); - CreateMap(); CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs index 66ae8e3..2ea2742 100644 --- a/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/RoleMappings.cs @@ -8,11 +8,14 @@ namespace DevHive.Web.Configurations.Mapping { public RoleMappings() { - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap(); + CreateMap(); + CreateMap(); CreateMap(); - CreateMap(); } } } diff --git a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs index 4ecd5f3..708b6ac 100644 --- a/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/TechnologyMappings.cs @@ -10,7 +10,8 @@ namespace DevHive.Web.Configurations.Mapping { CreateMap(); CreateMap(); - CreateMap(); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); CreateMap(); CreateMap(); diff --git a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs index 5faf4b5..9dbf613 100644 --- a/src/DevHive.Web/Configurations/Mapping/UserMappings.cs +++ b/src/DevHive.Web/Configurations/Mapping/UserMappings.cs @@ -20,13 +20,14 @@ namespace DevHive.Web.Configurations.Mapping CreateMap(); - CreateMap(); - CreateMap(); + //Update + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); + CreateMap() + .ForMember(src => src.Id, dest => dest.Ignore()); - CreateMap() - .ForMember(f => f.Name, u => u.MapFrom(src => src.UserName)); - CreateMap(); - CreateMap(); + CreateMap(); + CreateMap(); } } } diff --git a/src/DevHive.Web/Controllers/UserController.cs b/src/DevHive.Web/Controllers/UserController.cs index 7121ac8..fbbbbff 100644 --- a/src/DevHive.Web/Controllers/UserController.cs +++ b/src/DevHive.Web/Controllers/UserController.cs @@ -96,20 +96,6 @@ namespace DevHive.Web.Controllers return new AcceptedResult("UpdateUser", userWebModel); } - - [HttpPatch] - public async Task Patch(Guid id, [FromBody] List patch, [FromHeader] string authorization) - { - if (!await this._userService.ValidJWT(id, authorization)) - return new UnauthorizedResult(); - - UserServiceModel userServiceModel = await this._userService.PatchUser(id, patch); - - if (userServiceModel == null) - return new BadRequestObjectResult("Wrong patch properties"); - else - return new OkObjectResult(this._userMapper.Map(userServiceModel)); - } #endregion #region Delete diff --git a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs index 3c38ab6..30c66fb 100644 --- a/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs +++ b/src/DevHive.Web/Models/Identity/User/UpdateUserWebModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using DevHive.Web.Attributes; +using DevHive.Web.Models.Identity.Role; using DevHive.Web.Models.Language; using DevHive.Web.Models.Technology; @@ -18,6 +19,10 @@ namespace DevHive.Web.Models.Identity.User [Required] public HashSet Friends { get; set; } + [NotNull] + [Required] + public HashSet Roles { get; set; } + [NotNull] [Required] public HashSet Languages { get; set; } -- cgit v1.2.3 From 58178d5036f65bb4896fea08bbec9388a8ab8c20 Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 19:41:28 +0200 Subject: Code cleanup & consistency --- .../Repositories/LanguageRepository.cs | 12 +---- src/DevHive.Data/Repositories/PostRepository.cs | 43 +++++++---------- src/DevHive.Data/Repositories/RoleRepository.cs | 33 +++++++------ .../Repositories/TechnologyRepository.cs | 20 ++------ .../DevHive.Data.Tests/UserRepositoryTests.cs | 56 ---------------------- 5 files changed, 41 insertions(+), 123 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 4c51cf3..491019c 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -17,7 +17,6 @@ namespace DevHive.Data.Repositories } #region Create - public async Task AddAsync(Language entity) { await this._context.Languages @@ -28,7 +27,6 @@ namespace DevHive.Data.Repositories #endregion #region Read - public async Task GetByIdAsync(Guid id) { return await this._context.Languages @@ -47,28 +45,22 @@ namespace DevHive.Data.Repositories public async Task EditAsync(Language newEntity) { - this._context - .Set() - .Update(newEntity); + this._context.Languages.Update(newEntity); return await this.SaveChangesAsync(this._context); } #endregion #region Delete - public async Task DeleteAsync(Language entity) { - this._context - .Set() - .Remove(entity); + this._context.Languages.Remove(entity); return await this.SaveChangesAsync(this._context); } #endregion #region Validations - public async Task DoesLanguageNameExistAsync(string languageName) { return await this._context.Languages diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index db2c4af..71602e7 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -16,11 +16,10 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create public async Task AddAsync(Post post) { - await this._context - .Set() + await this._context.Posts .AddAsync(post); return await this.SaveChangesAsync(this._context); @@ -28,18 +27,17 @@ namespace DevHive.Data.Repositories public async Task AddCommentAsync(Comment entity) { - await this._context - .Set() + await this._context.Comments .AddAsync(entity); return await this.SaveChangesAsync(this._context); } + #endregion - //Read + #region Read public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Posts .FindAsync(id); } @@ -52,8 +50,7 @@ namespace DevHive.Data.Repositories public async Task GetCommentByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Comments .FindAsync(id); } @@ -63,12 +60,12 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(p => p.IssuerId == issuerId && p.TimeCreated == timeCreated); } + #endregion - //Update + #region Update public async Task EditAsync(Post newPost) { - this._context - .Set() + this._context.Posts .Update(newPost); return await this.SaveChangesAsync(this._context); @@ -76,18 +73,17 @@ namespace DevHive.Data.Repositories public async Task EditCommentAsync(Comment newEntity) { - this._context - .Set() + this._context.Comments .Update(newEntity); return await this.SaveChangesAsync(this._context); } + #endregion - //Delete + #region Delete public async Task DeleteAsync(Post post) { - this._context - .Set() + this._context.Posts .Remove(post); return await this.SaveChangesAsync(this._context); @@ -95,27 +91,24 @@ namespace DevHive.Data.Repositories public async Task DeleteCommentAsync(Comment entity) { - this._context - .Set() + this._context.Comments .Remove(entity); return await this.SaveChangesAsync(this._context); } + #endregion #region Validations - public async Task DoesPostExist(Guid postId) { - return await this._context - .Set() + return await this._context.Posts .AsNoTracking() .AnyAsync(r => r.Id == postId); } public async Task DoesCommentExist(Guid id) { - return await this._context - .Set() + return await this._context.Comments .AsNoTracking() .AnyAsync(r => r.Id == id); } diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 684fbd7..25549bf 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -1,6 +1,5 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; @@ -16,32 +15,31 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create public async Task AddAsync(Role entity) { - await this._context - .Set() + await this._context.Roles .AddAsync(entity); return await this.SaveChangesAsync(this._context); } + #endregion - //Read + #region Read public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Roles .FindAsync(id); } public async Task GetByNameAsync(string name) { - return await this._context - .Set() + return await this._context.Roles .FirstOrDefaultAsync(x => x.Name == name); } + #endregion - //Update + #region Update public async Task EditAsync(Role newEntity) { Role role = await this.GetByIdAsync(newEntity.Id); @@ -53,31 +51,32 @@ namespace DevHive.Data.Repositories return await this.SaveChangesAsync(this._context); } + #endregion - //Delete + #region Delete public async Task DeleteAsync(Role entity) { - this._context - .Set() + this._context.Roles .Remove(entity); return await this.SaveChangesAsync(this._context); } + #endregion + #region Validations public async Task DoesNameExist(string name) { - return await this._context - .Set() + return await this._context.Roles .AsNoTracking() .AnyAsync(r => r.Name == name); } public async Task DoesRoleExist(Guid id) { - return await this._context - .Set() + return await this._context.Roles .AsNoTracking() .AnyAsync(r => r.Id == id); } + #endregion } } diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index a41d4fb..597a532 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -18,11 +18,9 @@ namespace DevHive.Data.Repositories } #region Create - public async Task AddAsync(Technology entity) { - await this._context - .Set() + await this._context.Technologies .AddAsync(entity); return await this.SaveChangesAsync(this._context); @@ -30,11 +28,9 @@ namespace DevHive.Data.Repositories #endregion #region Read - public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + return await this._context.Technologies .FindAsync(id); } public async Task GetByNameAsync(string technologyName) @@ -46,11 +42,9 @@ namespace DevHive.Data.Repositories #endregion #region Edit - public async Task EditAsync(Technology newEntity) { - this._context - .Set() + this._context.Technologies .Update(newEntity); return await this.SaveChangesAsync(this._context); @@ -58,11 +52,9 @@ namespace DevHive.Data.Repositories #endregion #region Delete - public async Task DeleteAsync(Technology entity) { - this._context - .Set() + this._context.Technologies .Remove(entity); return await this.SaveChangesAsync(this._context); @@ -70,11 +62,9 @@ namespace DevHive.Data.Repositories #endregion #region Validations - public async Task DoesTechnologyNameExistAsync(string technologyName) { - return await this._context - .Set() + return await this._context.Technologies .AsNoTracking() .AnyAsync(r => r.Name == technologyName); } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs index be116b0..d4daae5 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/UserRepositoryTests.cs @@ -51,62 +51,6 @@ namespace DevHive.Data.Tests //Assert Assert.True(result, "User int' inserted properly into the database"); } - - [Test] - public async Task AddFriendToUserAsync_ShouldAddFriendToUsersList() - { - //Arrange - User dummyUserOne = CreateDummyUser(); - User dummyUserTwo = CreateAnotherDummyUser(); - - await this._userRepository.AddAsync(dummyUserOne); - await this._userRepository.AddAsync(dummyUserTwo); - - //Act - bool result = await this._userRepository.AddFriendToUserAsync(dummyUserOne, dummyUserTwo); - - //Assert - Assert.True(result, "Friend didn't save properly in the database"); - Assert.True(dummyUserOne.Friends.Contains(dummyUserTwo), "Friend doesn't get added to user properly"); - } - - [Test] - public async Task AddLanguageToUserAsync_ShouldAddLanguageToUser() - { - //Arrange - User dummyUser = CreateDummyUser(); - await this._userRepository.AddAsync(dummyUser); - Language language = new() - { - Name = "typescript" - }; - - //Act - bool result = await this._userRepository.AddLanguageToUserAsync(dummyUser, language); - - //Assert - Assert.True(result, "The language isn't inserted properly to the database"); - Assert.True(dummyUser.Languages.Contains(language), "The language doesn't get added properly to the user"); - } - - [Test] - public async Task AddTechnologyToUserAsync_ShouldAddTechnologyToUser() - { - //Arrange - User dummyUser = CreateDummyUser(); - await this._userRepository.AddAsync(dummyUser); - Technology technology = new() - { - Name = "Angular" - }; - - //Act - bool result = await this._userRepository.AddTechnologyToUserAsync(dummyUser, technology); - - //Assert - Assert.True(result, "The technology isn't inserted properly to the database"); - Assert.True(dummyUser.Technologies.Contains(technology), "The technology doesn't get added properly to the user"); - } #endregion #region Read -- cgit v1.2.3 From 5e5e2eb2edef88840edbb072597f81f8da3ae929 Mon Sep 17 00:00:00 2001 From: Danail Dimitrov Date: Thu, 21 Jan 2021 20:51:11 +0200 Subject: Made all Technology Layer Tests not asymc again --- .../Repositories/LanguageRepository.cs | 10 +- .../DevHive.Data.Tests/LenguageRepository.Tests.cs | 1 + .../TechnologyRepository.Tests.cs | 134 +++++++++------- .../LanguageService.Tests.cs | 100 ++++++++++++ .../TechnologyServices.Tests.cs | 176 ++++++++++++--------- 5 files changed, 286 insertions(+), 135 deletions(-) create mode 100644 src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 4c51cf3..808fa9a 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -45,11 +45,13 @@ namespace DevHive.Data.Repositories #region Update - public async Task EditAsync(Language newEntity) + public async Task EditAsync(Language entity) { - this._context - .Set() - .Update(newEntity); + Language language = await this._context.Languages + .FirstOrDefaultAsync(x => x.Id == entity.Id); + + this._context.Update(language); + this._context.Entry(entity).CurrentValues.SetValues(entity); return await this.SaveChangesAsync(this._context); } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs index 0a99bf1..aefeddd 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/LenguageRepository.Tests.cs @@ -6,5 +6,6 @@ namespace DevHive.Data.Tests [TestFixture] public class LenguageRepositoryTests { + // pending repo refactoring } } diff --git a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs index 9c95c6d..c0aaa93 100644 --- a/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs +++ b/src/DevHive.Tests/DevHive.Data.Tests/TechnologyRepository.Tests.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using NUnit.Framework; using System; using System.Linq; +using System.Threading.Tasks; namespace DevHive.Data.Tests { @@ -46,124 +47,151 @@ namespace DevHive.Data.Tests #endregion #region GetByIdAsync - [Test] - public async void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() + public void GetByIdAsync_ReturnsTheCorrectTechnology_IfIdExists() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); + Assert.AreEqual(TECHNOLOGY_NAME, technologyReturned.Name, "GetByIdAsync does not return the correct Technology when id is valid"); + }).GetAwaiter().GetResult(); } [Test] - public async void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() + public void GetByIdAsync_ReturnsNull_IfIdDoesNotExists() { + Task.Run(async () => + { + Guid id = Guid.NewGuid(); - Guid id = Guid.NewGuid(); - - Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); + Technology technologyReturned = await this.TechnologyRepository.GetByIdAsync(id); - Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); + Assert.IsNull(technologyReturned, "GetByIdAsync returns Technology when it should be null"); + }).GetAwaiter().GetResult(); } #endregion #region DoesTechnologyExistAsync [Test] - public async void DoesTechnologyExist_ReturnsTrue_IfIdExists() + public void DoesTechnologyExist_ReturnsTrue_IfIdExists() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - Guid id = technology.Id; + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Guid id = technology.Id; - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); + Assert.IsTrue(result, "DoesTechnologyExistAsync returns flase hwen technology exists"); + }).GetAwaiter().GetResult(); } [Test] - public async void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() + public void DoesTechnologyExist_ReturnsFalse_IfIdDoesNotExists() { - Guid id = Guid.NewGuid(); + Task.Run(async () => + { + Guid id = Guid.NewGuid(); - bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); + bool result = await this.TechnologyRepository.DoesTechnologyExistAsync(id); - Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); + Assert.IsFalse(result, "DoesTechnologyExistAsync returns true when technology does not exist"); + }).GetAwaiter().GetResult(); } #endregion #region DoesTechnologyNameExistAsync [Test] - public async void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() + public void DoesTechnologyNameExist_ReturnsTrue_IfTechnologyExists() { - AddEntity(); + Task.Run(async () => + { + AddEntity(); - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); + Assert.IsTrue(result, "DoesTechnologyNameExists returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); } [Test] - public async void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() + public void DoesTechnologyNameExist_ReturnsFalse_IfTechnologyDoesNotExists() { - bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); + Task.Run(async () => + { + bool result = await this.TechnologyRepository.DoesTechnologyNameExistAsync(TECHNOLOGY_NAME); - Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); + Assert.False(result, "DoesTechnologyNameExistAsync returns true when technology name does not exist"); + }).GetAwaiter().GetResult(); } #endregion #region EditAsync //TO DO fix: check UserRepo [Test] - public async void EditAsync_UpdatesEntity() + public void EditAsync_UpdatesEntity() { - string newName = "New name"; - Guid id = Guid.NewGuid(); - Technology technology = new Technology - { - Name = TECHNOLOGY_NAME, - Id = id - }; Technology newTechnology = new Technology + Task.Run(async () => { - Name = newName, - Id = id - }; + string newName = "New name"; + Technology technology = new Technology + { + Name = TECHNOLOGY_NAME + }; Technology newTechnology = new Technology + { + Name = newName + }; - await this.TechnologyRepository.AddAsync(technology); + await this.TechnologyRepository.AddAsync(technology); - bool result = await this.TechnologyRepository.EditAsync(newTechnology); + bool result = await this.TechnologyRepository.EditAsync(newTechnology); - Assert.IsTrue(result); + Assert.IsTrue(result); + }).GetAwaiter().GetResult(); } #endregion #region DeleteAsync [Test] - public async void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() + public void DeleteAsync_ReturnsTrue_IfDeletionIsSuccesfull() { - AddEntity(); - Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); + Task.Run(async () => + { + AddEntity(); + Technology technology = this.Context.Technologies.Where(x => x.Name == TECHNOLOGY_NAME).ToList().FirstOrDefault(); - bool result = await this.TechnologyRepository.DeleteAsync(technology); + bool result = await this.TechnologyRepository.DeleteAsync(technology); - Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + Assert.IsTrue(result, "DeleteAsync returns false when deletion is successfull"); + }).GetAwaiter().GetResult(); } #endregion #region HelperMethods - private async void AddEntity(string name = TECHNOLOGY_NAME) + private void AddEntity(string name = TECHNOLOGY_NAME) { - Technology technology = new Technology + Task.Run(async () => { - Name = name - }; + Technology technology = new Technology + { + Name = name + }; - await this.TechnologyRepository.AddAsync(technology); + await this.TechnologyRepository.AddAsync(technology); + }).GetAwaiter().GetResult(); } #endregion + + //Task.Run(async () => + //{ + + //}).GetAwaiter().GetResult(); } } diff --git a/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs new file mode 100644 index 0000000..2ebff58 --- /dev/null +++ b/src/DevHive.Tests/DevHive.Services.Tests/LanguageService.Tests.cs @@ -0,0 +1,100 @@ +using System; +using System.Threading.Tasks; +using AutoMapper; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Services.Models.Language; +using DevHive.Services.Services; +using Moq; +using NUnit.Framework; + +namespace DevHive.Services.Tests +{ + [TestFixture] + public class LanguageServiceTests + { + private Mock LanguageRepositoryMock { get; set; } + private Mock MapperMock { get; set; } + private LanguageService LanguageService { get; set; } + + [SetUp] + public void SetUp() + { + this.LanguageRepositoryMock = new Mock(); + this.MapperMock = new Mock(); + this.LanguageService = new LanguageService(this.LanguageRepositoryMock.Object, this.MapperMock.Object); + } + + #region Create + [Test] + public async void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + { + string technologyName = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = technologyName + }; + Language language = new() + { + Name = technologyName, + Id = id + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.LanguageRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(language)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(language); + + Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel); + + Assert.AreEqual(id, result); + } + + [Test] + public async void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + { + string languageName = "Gosho Trapov"; + + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = languageName + }; + Language language = new() + { + Name = languageName + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.LanguageRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(language); + + Guid result = await this.LanguageService.CreateLanguage(createLanguageServiceModel); + + Assert.IsTrue(result == Guid.Empty); + } + + [Test] + public void Create_ThrowsArgumentException_WhenEntityAlreadyExists() + { + string exceptionMessage = "Technology already exists!"; + string languageName = "Gosho Trapov"; + + CreateLanguageServiceModel createLanguageServiceModel = new() + { + Name = languageName + }; + Language language = new() + { + Name = languageName + }; + + this.LanguageRepositoryMock.Setup(p => p.DoesLanguageNameExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + + Exception ex = Assert.ThrowsAsync(() => this.LanguageService.CreateLanguage(createLanguageServiceModel)); + + Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); + } + #endregion + } +} diff --git a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs index 20aceb5..b04e213 100644 --- a/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs +++ b/src/DevHive.Tests/DevHive.Services.Tests/TechnologyServices.Tests.cs @@ -27,51 +27,57 @@ namespace DevHive.Services.Tests #region Create [Test] - public async void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() + public void Create_ReturnsNonEmptyGuid_WhenEntityIsAddedSuccessfully() { - string technologyName = "Gosho Trapov"; - Guid id = Guid.NewGuid(); - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = technologyName - }; - Technology technology = new() + Task.Run(async () => { - Name = technologyName, - Id = id - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); - - Assert.AreEqual(id, result); + string technologyName = "Gosho Trapov"; + Guid id = Guid.NewGuid(); + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName, + Id = id + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.GetByNameAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + + Assert.AreEqual(id, result); + }).GetAwaiter().GetResult(); } [Test] - public async void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() + public void Create_ReturnsEmptyGuid_WhenEntityIsNotAddedSuccessfully() { - string technologyName = "Gosho Trapov"; - - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = technologyName - }; - Technology technology = new() + Task.Run(async () => { - Name = technologyName - }; + string technologyName = "Gosho Trapov"; - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = technologyName + }; + Technology technology = new() + { + Name = technologyName + }; - Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.AddAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - Assert.IsTrue(result == Guid.Empty); + Guid result = await this.TechnologyService.Create(createTechnologyServiceModel); + + Assert.IsTrue(result == Guid.Empty); + }).GetAwaiter().GetResult(); } [Test] @@ -99,25 +105,28 @@ namespace DevHive.Services.Tests #region Read [Test] - public async void GetTechnologyById_ReturnsTheTechnology_WhenItExists() + public void GetTechnologyById_ReturnsTheTechnology_WhenItExists() { - Guid id = new Guid(); - string name = "Gosho Trapov"; - Technology technology = new() + Task.Run(async () => { - Name = name - }; - CreateTechnologyServiceModel createTechnologyServiceModel = new() - { - Name = name - }; - - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); - - CreateTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); - - Assert.AreEqual(name, result.Name); + Guid id = new Guid(); + string name = "Gosho Trapov"; + Technology technology = new() + { + Name = name + }; + CreateTechnologyServiceModel createTechnologyServiceModel = new() + { + Name = name + }; + + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(createTechnologyServiceModel); + + CreateTechnologyServiceModel result = await this.TechnologyService.GetTechnologyById(id); + + Assert.AreEqual(name, result.Name); + }).GetAwaiter().GetResult(); } [Test] @@ -137,26 +146,29 @@ namespace DevHive.Services.Tests [Test] [TestCase(true)] [TestCase(false)] - public async void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) + public void UpdateTechnology_ReturnsIfUpdateIsSuccessfull_WhenTechnologyExistsy(bool shouldPass) { - string name = "Gosho Trapov"; - Technology technology = new Technology - { - Name = name - }; - UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel + Task.Run(async () => { - Name = name, - }; - - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); - this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); - - bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); - - Assert.AreEqual(shouldPass, result); + string name = "Gosho Trapov"; + Technology technology = new Technology + { + Name = name + }; + UpdateTechnologyServiceModel updatetechnologyServiceModel = new UpdateTechnologyServiceModel + { + Name = name, + }; + + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyNameExistAsync(It.IsAny())).Returns(Task.FromResult(false)); + this.TechnologyRepositoryMock.Setup(p => p.EditAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.MapperMock.Setup(p => p.Map(It.IsAny())).Returns(technology); + + bool result = await this.TechnologyService.UpdateTechnology(updatetechnologyServiceModel); + + Assert.AreEqual(shouldPass, result); + }).GetAwaiter().GetResult(); } [Test] @@ -196,18 +208,21 @@ namespace DevHive.Services.Tests [Test] [TestCase(true)] [TestCase(false)] - public async void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) + public void DeleteTechnology_ShouldReturnIfDeletionIsSuccessfull_WhenTechnologyExists(bool shouldPass) { - Guid id = new Guid(); - Technology technology = new Technology(); + Task.Run(async () => + { + Guid id = new Guid(); + Technology technology = new Technology(); - this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); - this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); - this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); + this.TechnologyRepositoryMock.Setup(p => p.DoesTechnologyExistAsync(It.IsAny())).Returns(Task.FromResult(true)); + this.TechnologyRepositoryMock.Setup(p => p.GetByIdAsync(It.IsAny())).Returns(Task.FromResult(technology)); + this.TechnologyRepositoryMock.Setup(p => p.DeleteAsync(It.IsAny())).Returns(Task.FromResult(shouldPass)); - bool result = await this.TechnologyService.DeleteTechnology(id); + bool result = await this.TechnologyService.DeleteTechnology(id); - Assert.AreEqual(shouldPass, result); + Assert.AreEqual(shouldPass, result); + }).GetAwaiter().GetResult(); } [Test] @@ -223,5 +238,10 @@ namespace DevHive.Services.Tests Assert.AreEqual(exceptionMessage, ex.Message, "Incorecct exception message"); } #endregion + + //Task.Run(async () => + //{ + + //}).GetAwaiter().GetResult(); } } -- cgit v1.2.3 From bda98b96433d7a9952524fab4ec65f96998b55de Mon Sep 17 00:00:00 2001 From: transtrike Date: Thu, 21 Jan 2021 22:08:50 +0200 Subject: Generic base repo class refactored; All repos inherit generic base repo --- .../Interfaces/Repositories/IRepository.cs | 2 +- src/DevHive.Data/Repositories/BaseRepository.cs | 54 +++++++++++++++++++++- .../Repositories/LanguageRepository.cs | 43 +---------------- src/DevHive.Data/Repositories/PostRepository.cs | 34 +------------- src/DevHive.Data/Repositories/RoleRepository.cs | 43 +---------------- .../Repositories/TechnologyRepository.cs | 39 +--------------- src/DevHive.Data/Repositories/UserRepository.cs | 41 ++-------------- 7 files changed, 64 insertions(+), 192 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs index 40a78de..d9f7c7a 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs @@ -18,4 +18,4 @@ namespace DevHive.Data.Repositories.Interfaces //Delete Entity from database Task DeleteAsync(TEntity entity); } -} \ No newline at end of file +} diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs index b0f0f3e..dabb35b 100644 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -1,11 +1,61 @@ +using System; using System.Threading.Tasks; +using DevHive.Data.Repositories.Interfaces; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class BaseRepository + public class BaseRepository : IRepository + where TEntity : class { - public async Task SaveChangesAsync(DbContext context) + private readonly DbContext _context; + + public BaseRepository(DbContext context) + { + this._context = context; + this._context.ChangeTracker.AutoDetectChangesEnabled = false; + } + + public virtual async Task AddAsync(TEntity entity) + { + await this._context + .Set() + .AddAsync(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task GetByIdAsync(Guid id) + { + return await this._context + .Set() + .FindAsync(id); + } + + public virtual async Task EditAsync(TEntity newEntity) + { + // Old way(backup) + // User user = await this._context.Users + // .FirstOrDefaultAsync(x => x.Id == entity.Id); + + // this._context.Update(user); + // this._context.Entry(entity).CurrentValues.SetValues(entity); + + this._context + .Set() + .Update(newEntity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task DeleteAsync(TEntity entity) + { + this._context.Remove(entity); + + return await this.SaveChangesAsync(_context); + } + + public virtual async Task SaveChangesAsync(DbContext context) { int result = await context.SaveChangesAsync(); diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 59c88a6..d7ee609 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -1,38 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class LanguageRepository : BaseRepository, ILanguageRepository + public class LanguageRepository : BaseRepository, ILanguageRepository { private readonly DevHiveContext _context; public LanguageRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Language entity) - { - await this._context.Languages - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Languages - .FindAsync(id); - } - public async Task GetByNameAsync(string languageName) { return await this._context.Languages @@ -41,29 +25,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - - public async Task EditAsync(Language entity) - { - Language language = await this._context.Languages - .FirstOrDefaultAsync(x => x.Id == entity.Id); - - this._context.Update(language); - this._context.Entry(entity).CurrentValues.SetValues(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Language entity) - { - this._context.Languages.Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesLanguageNameExistAsync(string languageName) { diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 71602e7..9230a2e 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -1,30 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class PostRepository : BaseRepository, IPostRepository + public class PostRepository : BaseRepository, IPostRepository { private readonly DevHiveContext _context; public PostRepository(DevHiveContext context) + : base(context) { this._context = context; } #region Create - public async Task AddAsync(Post post) - { - await this._context.Posts - .AddAsync(post); - - return await this.SaveChangesAsync(this._context); - } - public async Task AddCommentAsync(Comment entity) { await this._context.Comments @@ -35,12 +27,6 @@ namespace DevHive.Data.Repositories #endregion #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Posts - .FindAsync(id); - } - public async Task GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Posts @@ -63,14 +49,6 @@ namespace DevHive.Data.Repositories #endregion #region Update - public async Task EditAsync(Post newPost) - { - this._context.Posts - .Update(newPost); - - return await this.SaveChangesAsync(this._context); - } - public async Task EditCommentAsync(Comment newEntity) { this._context.Comments @@ -81,14 +59,6 @@ namespace DevHive.Data.Repositories #endregion #region Delete - public async Task DeleteAsync(Post post) - { - this._context.Posts - .Remove(post); - - return await this.SaveChangesAsync(this._context); - } - public async Task DeleteCommentAsync(Comment entity) { this._context.Comments diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 25549bf..156792d 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -6,32 +6,17 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class RoleRepository : BaseRepository, IRoleRepository + public class RoleRepository : BaseRepository, IRoleRepository { private readonly DevHiveContext _context; public RoleRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Role entity) - { - await this._context.Roles - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Roles - .FindAsync(id); - } - public async Task GetByNameAsync(string name) { return await this._context.Roles @@ -39,30 +24,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - public async Task EditAsync(Role newEntity) - { - Role role = await this.GetByIdAsync(newEntity.Id); - - this._context - .Entry(role) - .CurrentValues - .SetValues(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Role entity) - { - this._context.Roles - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesNameExist(string name) { diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 35ee567..83cc7aa 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -1,38 +1,22 @@ using System; using System.Threading.Tasks; -using DevHive.Common.Models.Misc; using DevHive.Data.Interfaces.Repositories; using DevHive.Data.Models; using Microsoft.EntityFrameworkCore; - namespace DevHive.Data.Repositories { - public class TechnologyRepository : BaseRepository, ITechnologyRepository + public class TechnologyRepository : BaseRepository, ITechnologyRepository { private readonly DevHiveContext _context; public TechnologyRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - public async Task AddAsync(Technology entity) - { - await this._context.Technologies - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read - public async Task GetByIdAsync(Guid id) - { - return await this._context.Technologies - .FindAsync(id); - } public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies @@ -41,25 +25,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Edit - public async Task EditAsync(Technology newEntity) - { - this._context.Technologies.Update(newEntity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - public async Task DeleteAsync(Technology entity) - { - this._context.Technologies - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesTechnologyNameExistAsync(string technologyName) { diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index f0c28f1..1511c63 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -8,26 +8,16 @@ using Microsoft.EntityFrameworkCore; namespace DevHive.Data.Repositories { - public class UserRepository : BaseRepository, IUserRepository + public class UserRepository : BaseRepository, IUserRepository { private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) + :base(context) { this._context = context; } - #region Create - - public async Task AddAsync(User entity) - { - await this._context.Users - .AddAsync(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Read public IEnumerable QueryAll() @@ -38,7 +28,7 @@ namespace DevHive.Data.Repositories .AsEnumerable(); } - public async Task GetByIdAsync(Guid id) + public override async Task GetByIdAsync(Guid id) { return await this._context.Users .Include(x => x.Friends) @@ -80,31 +70,6 @@ namespace DevHive.Data.Repositories } #endregion - #region Update - - public async Task EditAsync(User entity) - { - User user = await this._context.Users - .FirstOrDefaultAsync(x => x.Id == entity.Id); - - this._context.Update(user); - this._context.Entry(entity).CurrentValues.SetValues(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - - #region Delete - - public async Task DeleteAsync(User entity) - { - this._context.Users - .Remove(entity); - - return await this.SaveChangesAsync(this._context); - } - #endregion - #region Validations public async Task DoesUserExistAsync(Guid id) -- cgit v1.2.3 From 9d7489b4958a5e6e5b0cd38a61e5bf4ff12f23da Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 23 Jan 2021 16:03:59 +0200 Subject: Fixed formatting --- .../Migrations/20210121083441_UserRefactor.cs | 808 ++++++++++----------- .../Repositories/LanguageRepository.cs | 2 +- src/DevHive.Data/Repositories/RoleRepository.cs | 2 +- .../Repositories/TechnologyRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 2 +- .../Configurations/Mapping/RoleMapings.cs | 2 +- src/DevHive.Services/Interfaces/IRoleService.cs | 2 +- .../Models/Identity/User/UpdateUserServiceModel.cs | 2 +- .../Models/Identity/User/UserServiceModel.cs | 2 +- src/DevHive.Services/Services/PostService.cs | 4 +- src/DevHive.Services/Services/RoleService.cs | 4 +- src/DevHive.Services/Services/UserService.cs | 2 +- 12 files changed, 417 insertions(+), 417 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs index ea1af2e..6eb1e38 100644 --- a/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs +++ b/src/DevHive.Data/Migrations/20210121083441_UserRefactor.cs @@ -4,408 +4,408 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace DevHive.Data.Migrations { - public partial class UserRefactor : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "AspNetRoles", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoles", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetUsers", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - FirstName = table.Column(type: "text", nullable: true), - LastName = table.Column(type: "text", nullable: true), - ProfilePictureUrl = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: true), - UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), - EmailConfirmed = table.Column(type: "boolean", nullable: false), - PasswordHash = table.Column(type: "text", nullable: true), - SecurityStamp = table.Column(type: "text", nullable: true), - ConcurrencyStamp = table.Column(type: "text", nullable: true), - PhoneNumber = table.Column(type: "text", nullable: true), - PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), - TwoFactorEnabled = table.Column(type: "boolean", nullable: false), - LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), - LockoutEnabled = table.Column(type: "boolean", nullable: false), - AccessFailedCount = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUsers", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUsers_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "Languages", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Languages", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Posts", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - IssuerId = table.Column(type: "uuid", nullable: false), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), - Message = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Posts", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Technologies", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Name = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Technologies", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "AspNetRoleClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - RoleId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserClaims", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - UserId = table.Column(type: "uuid", nullable: false), - ClaimType = table.Column(type: "text", nullable: true), - ClaimValue = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); - table.ForeignKey( - name: "FK_AspNetUserClaims_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserLogins", - columns: table => new - { - LoginProvider = table.Column(type: "text", nullable: false), - ProviderKey = table.Column(type: "text", nullable: false), - ProviderDisplayName = table.Column(type: "text", nullable: true), - UserId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); - table.ForeignKey( - name: "FK_AspNetUserLogins_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserRoles", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - RoleId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetRoles_RoleId", - column: x => x.RoleId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_AspNetUserRoles_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "AspNetUserTokens", - columns: table => new - { - UserId = table.Column(type: "uuid", nullable: false), - LoginProvider = table.Column(type: "text", nullable: false), - Name = table.Column(type: "text", nullable: false), - Value = table.Column(type: "text", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); - table.ForeignKey( - name: "FK_AspNetUserTokens_AspNetUsers_UserId", - column: x => x.UserId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "RoleUser", - columns: table => new - { - RolesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); - table.ForeignKey( - name: "FK_RoleUser_AspNetRoles_RolesId", - column: x => x.RolesId, - principalTable: "AspNetRoles", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_RoleUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "LanguageUser", - columns: table => new - { - LanguagesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); - table.ForeignKey( - name: "FK_LanguageUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_LanguageUser_Languages_LanguagesId", - column: x => x.LanguagesId, - principalTable: "Languages", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateTable( - name: "Comments", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - IssuerId = table.Column(type: "uuid", nullable: false), - Message = table.Column(type: "text", nullable: true), - TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), - PostId = table.Column(type: "uuid", nullable: true) - }, - constraints: table => - { - table.PrimaryKey("PK_Comments", x => x.Id); - table.ForeignKey( - name: "FK_Comments_Posts_PostId", - column: x => x.PostId, - principalTable: "Posts", - principalColumn: "Id", - onDelete: ReferentialAction.Restrict); - }); - - migrationBuilder.CreateTable( - name: "TechnologyUser", - columns: table => new - { - TechnologiesId = table.Column(type: "uuid", nullable: false), - UsersId = table.Column(type: "uuid", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); - table.ForeignKey( - name: "FK_TechnologyUser_AspNetUsers_UsersId", - column: x => x.UsersId, - principalTable: "AspNetUsers", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_TechnologyUser_Technologies_TechnologiesId", - column: x => x.TechnologiesId, - principalTable: "Technologies", - principalColumn: "Id", - onDelete: ReferentialAction.Cascade); - }); - - migrationBuilder.CreateIndex( - name: "IX_AspNetRoleClaims_RoleId", - table: "AspNetRoleClaims", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "RoleNameIndex", - table: "AspNetRoles", - column: "NormalizedName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserClaims_UserId", - table: "AspNetUserClaims", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserLogins_UserId", - table: "AspNetUserLogins", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUserRoles_RoleId", - table: "AspNetUserRoles", - column: "RoleId"); - - migrationBuilder.CreateIndex( - name: "EmailIndex", - table: "AspNetUsers", - column: "NormalizedEmail"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserId", - table: "AspNetUsers", - column: "UserId"); - - migrationBuilder.CreateIndex( - name: "IX_AspNetUsers_UserName", - table: "AspNetUsers", - column: "UserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "UserNameIndex", - table: "AspNetUsers", - column: "NormalizedUserName", - unique: true); - - migrationBuilder.CreateIndex( - name: "IX_Comments_PostId", - table: "Comments", - column: "PostId"); - - migrationBuilder.CreateIndex( - name: "IX_LanguageUser_UsersId", - table: "LanguageUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_RoleUser_UsersId", - table: "RoleUser", - column: "UsersId"); - - migrationBuilder.CreateIndex( - name: "IX_TechnologyUser_UsersId", - table: "TechnologyUser", - column: "UsersId"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "AspNetRoleClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserClaims"); - - migrationBuilder.DropTable( - name: "AspNetUserLogins"); - - migrationBuilder.DropTable( - name: "AspNetUserRoles"); - - migrationBuilder.DropTable( - name: "AspNetUserTokens"); - - migrationBuilder.DropTable( - name: "Comments"); - - migrationBuilder.DropTable( - name: "LanguageUser"); - - migrationBuilder.DropTable( - name: "RoleUser"); - - migrationBuilder.DropTable( - name: "TechnologyUser"); - - migrationBuilder.DropTable( - name: "Posts"); - - migrationBuilder.DropTable( - name: "Languages"); - - migrationBuilder.DropTable( - name: "AspNetRoles"); - - migrationBuilder.DropTable( - name: "AspNetUsers"); - - migrationBuilder.DropTable( - name: "Technologies"); - } - } + public partial class UserRefactor : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "AspNetRoles", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoles", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetUsers", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + FirstName = table.Column(type: "text", nullable: true), + LastName = table.Column(type: "text", nullable: true), + ProfilePictureUrl = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: true), + UserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedUserName = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + Email = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + NormalizedEmail = table.Column(type: "character varying(256)", maxLength: 256, nullable: true), + EmailConfirmed = table.Column(type: "boolean", nullable: false), + PasswordHash = table.Column(type: "text", nullable: true), + SecurityStamp = table.Column(type: "text", nullable: true), + ConcurrencyStamp = table.Column(type: "text", nullable: true), + PhoneNumber = table.Column(type: "text", nullable: true), + PhoneNumberConfirmed = table.Column(type: "boolean", nullable: false), + TwoFactorEnabled = table.Column(type: "boolean", nullable: false), + LockoutEnd = table.Column(type: "timestamp with time zone", nullable: true), + LockoutEnabled = table.Column(type: "boolean", nullable: false), + AccessFailedCount = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUsers", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUsers_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "Languages", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Languages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Posts", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + IssuerId = table.Column(type: "uuid", nullable: false), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), + Message = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Posts", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Technologies", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + Name = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Technologies", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "AspNetRoleClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + RoleId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetRoleClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetRoleClaims_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserClaims", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + UserId = table.Column(type: "uuid", nullable: false), + ClaimType = table.Column(type: "text", nullable: true), + ClaimValue = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserClaims", x => x.Id); + table.ForeignKey( + name: "FK_AspNetUserClaims_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserLogins", + columns: table => new + { + LoginProvider = table.Column(type: "text", nullable: false), + ProviderKey = table.Column(type: "text", nullable: false), + ProviderDisplayName = table.Column(type: "text", nullable: true), + UserId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserLogins", x => new { x.LoginProvider, x.ProviderKey }); + table.ForeignKey( + name: "FK_AspNetUserLogins_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserRoles", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + RoleId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId }); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetRoles_RoleId", + column: x => x.RoleId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_AspNetUserRoles_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "AspNetUserTokens", + columns: table => new + { + UserId = table.Column(type: "uuid", nullable: false), + LoginProvider = table.Column(type: "text", nullable: false), + Name = table.Column(type: "text", nullable: false), + Value = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_AspNetUserTokens", x => new { x.UserId, x.LoginProvider, x.Name }); + table.ForeignKey( + name: "FK_AspNetUserTokens_AspNetUsers_UserId", + column: x => x.UserId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "RoleUser", + columns: table => new + { + RolesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_RoleUser", x => new { x.RolesId, x.UsersId }); + table.ForeignKey( + name: "FK_RoleUser_AspNetRoles_RolesId", + column: x => x.RolesId, + principalTable: "AspNetRoles", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_RoleUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "LanguageUser", + columns: table => new + { + LanguagesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_LanguageUser", x => new { x.LanguagesId, x.UsersId }); + table.ForeignKey( + name: "FK_LanguageUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_LanguageUser_Languages_LanguagesId", + column: x => x.LanguagesId, + principalTable: "Languages", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Comments", + columns: table => new + { + Id = table.Column(type: "uuid", nullable: false), + IssuerId = table.Column(type: "uuid", nullable: false), + Message = table.Column(type: "text", nullable: true), + TimeCreated = table.Column(type: "timestamp without time zone", nullable: false), + PostId = table.Column(type: "uuid", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Comments", x => x.Id); + table.ForeignKey( + name: "FK_Comments_Posts_PostId", + column: x => x.PostId, + principalTable: "Posts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + }); + + migrationBuilder.CreateTable( + name: "TechnologyUser", + columns: table => new + { + TechnologiesId = table.Column(type: "uuid", nullable: false), + UsersId = table.Column(type: "uuid", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_TechnologyUser", x => new { x.TechnologiesId, x.UsersId }); + table.ForeignKey( + name: "FK_TechnologyUser_AspNetUsers_UsersId", + column: x => x.UsersId, + principalTable: "AspNetUsers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_TechnologyUser_Technologies_TechnologiesId", + column: x => x.TechnologiesId, + principalTable: "Technologies", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_AspNetRoleClaims_RoleId", + table: "AspNetRoleClaims", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "RoleNameIndex", + table: "AspNetRoles", + column: "NormalizedName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserClaims_UserId", + table: "AspNetUserClaims", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserLogins_UserId", + table: "AspNetUserLogins", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUserRoles_RoleId", + table: "AspNetUserRoles", + column: "RoleId"); + + migrationBuilder.CreateIndex( + name: "EmailIndex", + table: "AspNetUsers", + column: "NormalizedEmail"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserId", + table: "AspNetUsers", + column: "UserId"); + + migrationBuilder.CreateIndex( + name: "IX_AspNetUsers_UserName", + table: "AspNetUsers", + column: "UserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "UserNameIndex", + table: "AspNetUsers", + column: "NormalizedUserName", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Comments_PostId", + table: "Comments", + column: "PostId"); + + migrationBuilder.CreateIndex( + name: "IX_LanguageUser_UsersId", + table: "LanguageUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_RoleUser_UsersId", + table: "RoleUser", + column: "UsersId"); + + migrationBuilder.CreateIndex( + name: "IX_TechnologyUser_UsersId", + table: "TechnologyUser", + column: "UsersId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "AspNetRoleClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserClaims"); + + migrationBuilder.DropTable( + name: "AspNetUserLogins"); + + migrationBuilder.DropTable( + name: "AspNetUserRoles"); + + migrationBuilder.DropTable( + name: "AspNetUserTokens"); + + migrationBuilder.DropTable( + name: "Comments"); + + migrationBuilder.DropTable( + name: "LanguageUser"); + + migrationBuilder.DropTable( + name: "RoleUser"); + + migrationBuilder.DropTable( + name: "TechnologyUser"); + + migrationBuilder.DropTable( + name: "Posts"); + + migrationBuilder.DropTable( + name: "Languages"); + + migrationBuilder.DropTable( + name: "AspNetRoles"); + + migrationBuilder.DropTable( + name: "AspNetUsers"); + + migrationBuilder.DropTable( + name: "Technologies"); + } + } } diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index d7ee609..c28ef31 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -11,7 +11,7 @@ namespace DevHive.Data.Repositories private readonly DevHiveContext _context; public LanguageRepository(DevHiveContext context) - :base(context) + : base(context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/RoleRepository.cs b/src/DevHive.Data/Repositories/RoleRepository.cs index 156792d..e5cb959 100644 --- a/src/DevHive.Data/Repositories/RoleRepository.cs +++ b/src/DevHive.Data/Repositories/RoleRepository.cs @@ -11,7 +11,7 @@ namespace DevHive.Data.Repositories private readonly DevHiveContext _context; public RoleRepository(DevHiveContext context) - :base(context) + : base(context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 83cc7aa..87540fb 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -11,7 +11,7 @@ namespace DevHive.Data.Repositories private readonly DevHiveContext _context; public TechnologyRepository(DevHiveContext context) - :base(context) + : base(context) { this._context = context; } diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 258c010..a2298db 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -13,7 +13,7 @@ namespace DevHive.Data.Repositories private readonly DevHiveContext _context; public UserRepository(DevHiveContext context) - :base(context) + : base(context) { this._context = context; } diff --git a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs index 5f9452f..e61a107 100644 --- a/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs +++ b/src/DevHive.Services/Configurations/Mapping/RoleMapings.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Configurations.Mapping { - public class RoleMappings : Profile + public class RoleMappings : Profile { public RoleMappings() { diff --git a/src/DevHive.Services/Interfaces/IRoleService.cs b/src/DevHive.Services/Interfaces/IRoleService.cs index 3a498d2..d47728c 100644 --- a/src/DevHive.Services/Interfaces/IRoleService.cs +++ b/src/DevHive.Services/Interfaces/IRoleService.cs @@ -4,7 +4,7 @@ using DevHive.Services.Models.Identity.Role; namespace DevHive.Services.Interfaces { - public interface IRoleService + public interface IRoleService { Task CreateRole(CreateRoleServiceModel roleServiceModel); diff --git a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs index 9277e3e..5b197e4 100644 --- a/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UpdateUserServiceModel.cs @@ -6,7 +6,7 @@ using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { - public class UpdateUserServiceModel : BaseUserServiceModel + public class UpdateUserServiceModel : BaseUserServiceModel { public Guid Id { get; set; } diff --git a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs index 5fcd494..3aa0d44 100644 --- a/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs +++ b/src/DevHive.Services/Models/Identity/User/UserServiceModel.cs @@ -5,7 +5,7 @@ using DevHive.Services.Models.Technology; namespace DevHive.Services.Models.Identity.User { - public class UserServiceModel : BaseUserServiceModel + public class UserServiceModel : BaseUserServiceModel { public HashSet Roles { get; set; } = new HashSet(); diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 4757937..2df3b41 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -33,7 +33,7 @@ namespace DevHive.Services.Services bool success = await this._postRepository.AddAsync(post); - if(success) + if (success) { Post newPost = await this._postRepository.GetPostByIssuerAndTimeCreatedAsync(postServiceModel.IssuerId, postServiceModel.TimeCreated); return newPost.Id; @@ -49,7 +49,7 @@ namespace DevHive.Services.Services bool success = await this._postRepository.AddCommentAsync(comment); - if(success) + if (success) { Comment newComment = await this._postRepository.GetCommentByIssuerAndTimeCreatedAsync(commentServiceModel.IssuerId, commentServiceModel.TimeCreated); return newComment.Id; diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 91a8c73..a8b8e17 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -9,7 +9,7 @@ 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; @@ -28,7 +28,7 @@ namespace DevHive.Services.Services Role role = this._roleMapper.Map(roleServiceModel); bool success = await this._roleRepository.AddAsync(role); - if(success) + if (success) { Role newRole = await this._roleRepository.GetByNameAsync(roleServiceModel.Name); return newRole.Id; diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index cf33644..d7013e1 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -194,7 +194,7 @@ namespace DevHive.Services.Services return false; /* Check roles */ - if(jwtRoleNames.Contains(Role.AdminRole)) + if (jwtRoleNames.Contains(Role.AdminRole)) return true; // Check if jwt contains all user roles (if it doesn't, jwt is either old or tampered with) -- 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.Data/Repositories/LanguageRepository.cs') 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 From d59ac14fa58a3c1171442e09a5a96b95e5bf40b8 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 26 Jan 2021 19:49:13 +0200 Subject: Fixed ChangeTracker; Optimized default role insertion --- src/DevHive.Data/DevHiveContext.cs | 6 ++++++ src/DevHive.Data/Repositories/FeedRepository.cs | 1 + src/DevHive.Data/Repositories/LanguageRepository.cs | 3 ++- src/DevHive.Data/Repositories/TechnologyRepository.cs | 2 +- src/DevHive.Data/Repositories/UserRepository.cs | 3 ++- src/DevHive.Services/Services/TechnologyService.cs | 1 + src/DevHive.Services/Services/UserService.cs | 2 +- 7 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/DevHiveContext.cs b/src/DevHive.Data/DevHiveContext.cs index 17e16e7..48a6789 100644 --- a/src/DevHive.Data/DevHiveContext.cs +++ b/src/DevHive.Data/DevHiveContext.cs @@ -30,9 +30,15 @@ namespace DevHive.Data builder.Entity() .HasMany(x => x.Languages); + builder.Entity() + .HasMany(x => x.Users); + builder.Entity() .HasMany(x => x.Technologies); + builder.Entity() + .HasMany(x => x.Users); + builder.Entity() .HasChangeTrackingStrategy(ChangeTrackingStrategy.Snapshot); diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs index 8bf1f9a..1b7518d 100644 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -17,6 +17,7 @@ namespace DevHive.Data.Repositories { this._context = context; } + public async Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize) { List friendsIds = friendsList.Select(f => f.Id).ToList(); diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index f2cc67f..7f4b946 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -22,7 +22,6 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string languageName) { return await this._context.Languages - .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == languageName); } @@ -36,12 +35,14 @@ namespace DevHive.Data.Repositories public async Task DoesLanguageNameExistAsync(string languageName) { return await this._context.Languages + .AsNoTracking() .AnyAsync(r => r.Name == languageName); } public async Task DoesLanguageExistAsync(Guid id) { return await this._context.Languages + .AsNoTracking() .AnyAsync(r => r.Id == id); } #endregion diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index e03291d..7bb43cc 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -22,7 +22,6 @@ namespace DevHive.Data.Repositories public async Task GetByNameAsync(string technologyName) { return await this._context.Technologies - .AsNoTracking() .FirstOrDefaultAsync(x => x.Name == technologyName); } @@ -43,6 +42,7 @@ namespace DevHive.Data.Repositories public async Task DoesTechnologyExistAsync(Guid id) { return await this._context.Technologies + .AsNoTracking() .AnyAsync(x => x.Id == id); } #endregion diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 06bafca..57ae146 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -40,7 +40,6 @@ namespace DevHive.Data.Repositories public async Task GetByUsernameAsync(string username) { return await this._context.Users - .AsNoTracking() .Include(x => x.Friends) .Include(x => x.Roles) .Include(x => x.Languages) @@ -74,9 +73,11 @@ namespace DevHive.Data.Repositories public async Task DoesUserHaveThisFriendAsync(Guid userId, Guid friendId) { User user = await this._context.Users + .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == userId); User friend = await this._context.Users + .AsNoTracking() .FirstOrDefaultAsync(x => x.Id == friendId); return user.Friends.Contains(friend); diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 039cd8a..8f37273 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -49,6 +49,7 @@ namespace DevHive.Services.Services return this._technologyMapper.Map(technology); } + public HashSet GetTechnologies() { HashSet technologies = this._technologyRepository.GetTechnologies(); diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index abbecb1..9f4c777 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -77,7 +77,7 @@ namespace DevHive.Services.Services // Set the default role to the user Role defaultRole = await this._roleRepository.GetByNameAsync(Role.DefaultRole); - user.Roles = new HashSet() { defaultRole }; + user.Roles.Add(defaultRole); await this._userRepository.AddAsync(user); -- cgit v1.2.3 From a11d023c0e6557baef6b420771e31f9ac0f4b1e2 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 4 Feb 2021 15:00:39 +0200 Subject: Added some XML documentation in data layer --- src/DevHive.Data/Repositories/CommentRepository.cs | 3 +++ src/DevHive.Data/Repositories/FeedRepository.cs | 18 ++++++++++++++++++ src/DevHive.Data/Repositories/LanguageRepository.cs | 3 +++ src/DevHive.Data/Repositories/PostRepository.cs | 3 +++ src/DevHive.Data/Repositories/TechnologyRepository.cs | 3 +++ 5 files changed, 30 insertions(+) (limited to 'src/DevHive.Data/Repositories/LanguageRepository.cs') diff --git a/src/DevHive.Data/Repositories/CommentRepository.cs b/src/DevHive.Data/Repositories/CommentRepository.cs index 382c666..bee7624 100644 --- a/src/DevHive.Data/Repositories/CommentRepository.cs +++ b/src/DevHive.Data/Repositories/CommentRepository.cs @@ -28,6 +28,9 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.Id == id); } + /// + /// This method returns the comment that is made at exactly the given time and by the given creator + /// public async Task GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated) { return await this._context.Comments diff --git a/src/DevHive.Data/Repositories/FeedRepository.cs b/src/DevHive.Data/Repositories/FeedRepository.cs index 271c3a5..8d3e5e1 100644 --- a/src/DevHive.Data/Repositories/FeedRepository.cs +++ b/src/DevHive.Data/Repositories/FeedRepository.cs @@ -18,6 +18,15 @@ namespace DevHive.Data.Repositories this._context = context; } + /// + /// This returns a given amount of posts of all given friends, created before "firstRequestIssued", + /// ordered from latest to oldest (time created). + /// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize). + /// + /// This method is used in the feed page. + /// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts, + /// that are after the first X posts. + /// public async Task> GetFriendsPosts(List friendsList, DateTime firstRequestIssued, int pageNumber, int pageSize) { List friendsIds = friendsList.Select(f => f.Id).ToList(); @@ -39,6 +48,15 @@ namespace DevHive.Data.Repositories return posts; } + /// + /// This returns a given amount of posts, that a user has made, created before "firstRequestIssued", + /// ordered from latest to oldest (time created). + /// PageSize specifies how many posts to get, and pageNumber specifices how many posts to skip (pageNumber * pageSize). + /// + /// This method is used in the profile page. + /// Posts from friends are meant to be gotten in chunks, meaning you get X posts, and then get another amount of posts, + /// that are after the first X posts. + /// public async Task> GetUsersPosts(User user, DateTime firstRequestIssued, int pageNumber, int pageSize) { List posts = await this._context.Posts diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs index 7f4b946..31d0b86 100644 --- a/src/DevHive.Data/Repositories/LanguageRepository.cs +++ b/src/DevHive.Data/Repositories/LanguageRepository.cs @@ -25,6 +25,9 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.Name == languageName); } + /// + /// Returns all technologies that exist in the database + /// public HashSet GetLanguages() { return this._context.Languages.ToHashSet(); diff --git a/src/DevHive.Data/Repositories/PostRepository.cs b/src/DevHive.Data/Repositories/PostRepository.cs index 0fec435..ed2fa1b 100644 --- a/src/DevHive.Data/Repositories/PostRepository.cs +++ b/src/DevHive.Data/Repositories/PostRepository.cs @@ -39,6 +39,9 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.Id == id); } + /// + /// This method returns the post that is made at exactly the given time and by the given creator + /// public async Task GetPostByCreatorAndTimeCreatedAsync(Guid creatorId, DateTime timeCreated) { return await this._context.Posts diff --git a/src/DevHive.Data/Repositories/TechnologyRepository.cs b/src/DevHive.Data/Repositories/TechnologyRepository.cs index 7bb43cc..6f0d10f 100644 --- a/src/DevHive.Data/Repositories/TechnologyRepository.cs +++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs @@ -25,6 +25,9 @@ namespace DevHive.Data.Repositories .FirstOrDefaultAsync(x => x.Name == technologyName); } + /// + /// Returns all technologies that exist in the database + /// public HashSet GetTechnologies() { return this._context.Technologies.ToHashSet(); -- cgit v1.2.3