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 --- .../Interfaces/Repositories/IRepository.cs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/DevHive.Data/Interfaces/Repositories/IRepository.cs (limited to 'src/DevHive.Data/Interfaces/Repositories/IRepository.cs') 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 -- 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/Interfaces/Repositories/IRepository.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 1c91e52c91a1a94616eca632ffdd930ffeb616f4 Mon Sep 17 00:00:00 2001 From: transtrike Date: Fri, 22 Jan 2021 22:39:23 +0200 Subject: Kamen's proposal in Update() at BaseRepo merged --- src/DevHive.Data/Interfaces/Repositories/IRepository.cs | 2 +- src/DevHive.Data/Repositories/BaseRepository.cs | 15 +++++---------- 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 +- 7 files changed, 11 insertions(+), 16 deletions(-) (limited to 'src/DevHive.Data/Interfaces/Repositories/IRepository.cs') diff --git a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs index d9f7c7a..0d11cd3 100644 --- a/src/DevHive.Data/Interfaces/Repositories/IRepository.cs +++ b/src/DevHive.Data/Interfaces/Repositories/IRepository.cs @@ -13,7 +13,7 @@ namespace DevHive.Data.Repositories.Interfaces Task GetByIdAsync(Guid id); //Modify Entity from database - Task EditAsync(TEntity newEntity); + Task EditAsync(Guid id, TEntity newEntity); //Delete Entity from database Task DeleteAsync(TEntity entity); diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs index dabb35b..0a97ac1 100644 --- a/src/DevHive.Data/Repositories/BaseRepository.cs +++ b/src/DevHive.Data/Repositories/BaseRepository.cs @@ -32,18 +32,13 @@ namespace DevHive.Data.Repositories .FindAsync(id); } - public virtual async Task EditAsync(TEntity newEntity) + public virtual async Task EditAsync(Guid id, 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); - + TEntity currEnt = await this.GetByIdAsync(id); this._context - .Set() - .Update(newEntity); + .Entry(currEnt) + .CurrentValues + .SetValues(newEntity); return await this.SaveChangesAsync(_context); } diff --git a/src/DevHive.Services/Services/LanguageService.cs b/src/DevHive.Services/Services/LanguageService.cs index 89df654..a602d43 100644 --- a/src/DevHive.Services/Services/LanguageService.cs +++ b/src/DevHive.Services/Services/LanguageService.cs @@ -66,7 +66,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Language name already exists in our data base!"); Language lang = this._languageMapper.Map(languageServiceModel); - return await this._languageRepository.EditAsync(lang); + return await this._languageRepository.EditAsync(languageServiceModel.Id, lang); } #endregion diff --git a/src/DevHive.Services/Services/PostService.cs b/src/DevHive.Services/Services/PostService.cs index 9503b8a..4757937 100644 --- a/src/DevHive.Services/Services/PostService.cs +++ b/src/DevHive.Services/Services/PostService.cs @@ -84,7 +84,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Comment does not exist!"); Post post = this._postMapper.Map(postServiceModel); - return await this._postRepository.EditAsync(post); + return await this._postRepository.EditAsync(postServiceModel.Id, post); } public async Task UpdateComment(UpdateCommentServiceModel commentServiceModel) diff --git a/src/DevHive.Services/Services/RoleService.cs b/src/DevHive.Services/Services/RoleService.cs index 3ebb7c8..896946d 100644 --- a/src/DevHive.Services/Services/RoleService.cs +++ b/src/DevHive.Services/Services/RoleService.cs @@ -56,7 +56,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Role name already exists!"); Role role = this._roleMapper.Map(updateRoleServiceModel); - return await this._roleRepository.EditAsync(role); + return await this._roleRepository.EditAsync(updateRoleServiceModel.Id, role); } public async Task DeleteRole(Guid id) diff --git a/src/DevHive.Services/Services/TechnologyService.cs b/src/DevHive.Services/Services/TechnologyService.cs index 88ed702..c879ad7 100644 --- a/src/DevHive.Services/Services/TechnologyService.cs +++ b/src/DevHive.Services/Services/TechnologyService.cs @@ -60,7 +60,7 @@ namespace DevHive.Services.Services throw new ArgumentException("Technology name already exists!"); Technology technology = this._technologyMapper.Map(updateTechnologyServiceModel); - bool result = await this._technologyRepository.EditAsync(technology); + bool result = await this._technologyRepository.EditAsync(updateTechnologyServiceModel.Id, technology); return result; } diff --git a/src/DevHive.Services/Services/UserService.cs b/src/DevHive.Services/Services/UserService.cs index 217154e..533f422 100644 --- a/src/DevHive.Services/Services/UserService.cs +++ b/src/DevHive.Services/Services/UserService.cs @@ -152,7 +152,7 @@ namespace DevHive.Services.Services updateUserServiceModel.Technologies.RemoveWhere(x => x.Id == Guid.Empty); User user = this._userMapper.Map(updateUserServiceModel); - bool successful = await this._userRepository.EditAsync(user); + bool successful = await this._userRepository.EditAsync(updateUserServiceModel.Id, user); if (!successful) throw new InvalidOperationException("Unable to edit user!"); -- cgit v1.2.3