diff options
Diffstat (limited to 'src')
5 files changed, 41 insertions, 123 deletions
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<bool> AddAsync(Language entity) { await this._context.Languages @@ -28,7 +27,6 @@ namespace DevHive.Data.Repositories #endregion #region Read - public async Task<Language> GetByIdAsync(Guid id) { return await this._context.Languages @@ -47,28 +45,22 @@ namespace DevHive.Data.Repositories public async Task<bool> EditAsync(Language newEntity) { - this._context - .Set<Language>() - .Update(newEntity); + this._context.Languages.Update(newEntity); return await this.SaveChangesAsync(this._context); } #endregion #region Delete - public async Task<bool> DeleteAsync(Language entity) { - this._context - .Set<Language>() - .Remove(entity); + this._context.Languages.Remove(entity); return await this.SaveChangesAsync(this._context); } #endregion #region Validations - public async Task<bool> 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<bool> AddAsync(Post post) { - await this._context - .Set<Post>() + await this._context.Posts .AddAsync(post); return await this.SaveChangesAsync(this._context); @@ -28,18 +27,17 @@ namespace DevHive.Data.Repositories public async Task<bool> AddCommentAsync(Comment entity) { - await this._context - .Set<Comment>() + await this._context.Comments .AddAsync(entity); return await this.SaveChangesAsync(this._context); } + #endregion - //Read + #region Read public async Task<Post> GetByIdAsync(Guid id) { - return await this._context - .Set<Post>() + return await this._context.Posts .FindAsync(id); } @@ -52,8 +50,7 @@ namespace DevHive.Data.Repositories public async Task<Comment> GetCommentByIdAsync(Guid id) { - return await this._context - .Set<Comment>() + 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<bool> EditAsync(Post newPost) { - this._context - .Set<Post>() + this._context.Posts .Update(newPost); return await this.SaveChangesAsync(this._context); @@ -76,18 +73,17 @@ namespace DevHive.Data.Repositories public async Task<bool> EditCommentAsync(Comment newEntity) { - this._context - .Set<Comment>() + this._context.Comments .Update(newEntity); return await this.SaveChangesAsync(this._context); } + #endregion - //Delete + #region Delete public async Task<bool> DeleteAsync(Post post) { - this._context - .Set<Post>() + this._context.Posts .Remove(post); return await this.SaveChangesAsync(this._context); @@ -95,27 +91,24 @@ namespace DevHive.Data.Repositories public async Task<bool> DeleteCommentAsync(Comment entity) { - this._context - .Set<Comment>() + this._context.Comments .Remove(entity); return await this.SaveChangesAsync(this._context); } + #endregion #region Validations - public async Task<bool> DoesPostExist(Guid postId) { - return await this._context - .Set<Post>() + return await this._context.Posts .AsNoTracking() .AnyAsync(r => r.Id == postId); } public async Task<bool> DoesCommentExist(Guid id) { - return await this._context - .Set<Comment>() + 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<bool> AddAsync(Role entity) { - await this._context - .Set<Role>() + await this._context.Roles .AddAsync(entity); return await this.SaveChangesAsync(this._context); } + #endregion - //Read + #region Read public async Task<Role> GetByIdAsync(Guid id) { - return await this._context - .Set<Role>() + return await this._context.Roles .FindAsync(id); } public async Task<Role> GetByNameAsync(string name) { - return await this._context - .Set<Role>() + return await this._context.Roles .FirstOrDefaultAsync(x => x.Name == name); } + #endregion - //Update + #region Update public async Task<bool> 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<bool> DeleteAsync(Role entity) { - this._context - .Set<Role>() + this._context.Roles .Remove(entity); return await this.SaveChangesAsync(this._context); } + #endregion + #region Validations public async Task<bool> DoesNameExist(string name) { - return await this._context - .Set<Role>() + return await this._context.Roles .AsNoTracking() .AnyAsync(r => r.Name == name); } public async Task<bool> DoesRoleExist(Guid id) { - return await this._context - .Set<Role>() + 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<bool> AddAsync(Technology entity) { - await this._context - .Set<Technology>() + 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<Technology> GetByIdAsync(Guid id) { - return await this._context - .Set<Technology>() + return await this._context.Technologies .FindAsync(id); } public async Task<Technology> GetByNameAsync(string technologyName) @@ -46,11 +42,9 @@ namespace DevHive.Data.Repositories #endregion #region Edit - public async Task<bool> EditAsync(Technology newEntity) { - this._context - .Set<Technology>() + 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<bool> DeleteAsync(Technology entity) { - this._context - .Set<Technology>() + 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<bool> DoesTechnologyNameExistAsync(string technologyName) { - return await this._context - .Set<Technology>() + 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 |
