aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data
diff options
context:
space:
mode:
authortranstrike <transtrike@gmail.com>2021-01-21 22:08:50 +0200
committertranstrike <transtrike@gmail.com>2021-01-21 22:08:50 +0200
commitbda98b96433d7a9952524fab4ec65f96998b55de (patch)
tree59f8d2bf63b03bacc76f98114d2aed78e420ddcd /src/DevHive.Data
parentfb527cbfb94e2723113d67b83ed8f24e32422e56 (diff)
downloadDevHive-bda98b96433d7a9952524fab4ec65f96998b55de.tar
DevHive-bda98b96433d7a9952524fab4ec65f96998b55de.tar.gz
DevHive-bda98b96433d7a9952524fab4ec65f96998b55de.zip
Generic base repo class refactored; All repos inherit generic base repo
Diffstat (limited to 'src/DevHive.Data')
-rw-r--r--src/DevHive.Data/Interfaces/Repositories/IRepository.cs2
-rw-r--r--src/DevHive.Data/Repositories/BaseRepository.cs54
-rw-r--r--src/DevHive.Data/Repositories/LanguageRepository.cs43
-rw-r--r--src/DevHive.Data/Repositories/PostRepository.cs34
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs43
-rw-r--r--src/DevHive.Data/Repositories/TechnologyRepository.cs39
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs41
7 files changed, 64 insertions, 192 deletions
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<bool> 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<TEntity> : IRepository<TEntity>
+ where TEntity : class
{
- public async Task<bool> SaveChangesAsync(DbContext context)
+ private readonly DbContext _context;
+
+ public BaseRepository(DbContext context)
+ {
+ this._context = context;
+ this._context.ChangeTracker.AutoDetectChangesEnabled = false;
+ }
+
+ public virtual async Task<bool> AddAsync(TEntity entity)
+ {
+ await this._context
+ .Set<TEntity>()
+ .AddAsync(entity);
+
+ return await this.SaveChangesAsync(_context);
+ }
+
+ public virtual async Task<TEntity> GetByIdAsync(Guid id)
+ {
+ return await this._context
+ .Set<TEntity>()
+ .FindAsync(id);
+ }
+
+ public virtual async Task<bool> 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<TEntity>()
+ .Update(newEntity);
+
+ return await this.SaveChangesAsync(_context);
+ }
+
+ public virtual async Task<bool> DeleteAsync(TEntity entity)
+ {
+ this._context.Remove(entity);
+
+ return await this.SaveChangesAsync(_context);
+ }
+
+ public virtual async Task<bool> 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<Language>, ILanguageRepository
{
private readonly DevHiveContext _context;
public LanguageRepository(DevHiveContext context)
+ :base(context)
{
this._context = context;
}
- #region Create
- public async Task<bool> AddAsync(Language entity)
- {
- await this._context.Languages
- .AddAsync(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
- public async Task<Language> GetByIdAsync(Guid id)
- {
- return await this._context.Languages
- .FindAsync(id);
- }
-
public async Task<Language> GetByNameAsync(string languageName)
{
return await this._context.Languages
@@ -41,29 +25,6 @@ namespace DevHive.Data.Repositories
}
#endregion
- #region Update
-
- public async Task<bool> 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<bool> DeleteAsync(Language entity)
- {
- this._context.Languages.Remove(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Validations
public async Task<bool> 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<Post>, IPostRepository
{
private readonly DevHiveContext _context;
public PostRepository(DevHiveContext context)
+ : base(context)
{
this._context = context;
}
#region Create
- public async Task<bool> AddAsync(Post post)
- {
- await this._context.Posts
- .AddAsync(post);
-
- return await this.SaveChangesAsync(this._context);
- }
-
public async Task<bool> AddCommentAsync(Comment entity)
{
await this._context.Comments
@@ -35,12 +27,6 @@ namespace DevHive.Data.Repositories
#endregion
#region Read
- public async Task<Post> GetByIdAsync(Guid id)
- {
- return await this._context.Posts
- .FindAsync(id);
- }
-
public async Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated)
{
return await this._context.Posts
@@ -63,14 +49,6 @@ namespace DevHive.Data.Repositories
#endregion
#region Update
- public async Task<bool> EditAsync(Post newPost)
- {
- this._context.Posts
- .Update(newPost);
-
- return await this.SaveChangesAsync(this._context);
- }
-
public async Task<bool> EditCommentAsync(Comment newEntity)
{
this._context.Comments
@@ -81,14 +59,6 @@ namespace DevHive.Data.Repositories
#endregion
#region Delete
- public async Task<bool> DeleteAsync(Post post)
- {
- this._context.Posts
- .Remove(post);
-
- return await this.SaveChangesAsync(this._context);
- }
-
public async Task<bool> 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<Role>, IRoleRepository
{
private readonly DevHiveContext _context;
public RoleRepository(DevHiveContext context)
+ :base(context)
{
this._context = context;
}
- #region Create
- public async Task<bool> AddAsync(Role entity)
- {
- await this._context.Roles
- .AddAsync(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
- public async Task<Role> GetByIdAsync(Guid id)
- {
- return await this._context.Roles
- .FindAsync(id);
- }
-
public async Task<Role> GetByNameAsync(string name)
{
return await this._context.Roles
@@ -39,30 +24,6 @@ namespace DevHive.Data.Repositories
}
#endregion
- #region Update
- public async Task<bool> 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<bool> DeleteAsync(Role entity)
- {
- this._context.Roles
- .Remove(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Validations
public async Task<bool> 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<Technology>, ITechnologyRepository
{
private readonly DevHiveContext _context;
public TechnologyRepository(DevHiveContext context)
+ :base(context)
{
this._context = context;
}
- #region Create
- public async Task<bool> AddAsync(Technology entity)
- {
- await this._context.Technologies
- .AddAsync(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
- public async Task<Technology> GetByIdAsync(Guid id)
- {
- return await this._context.Technologies
- .FindAsync(id);
- }
public async Task<Technology> GetByNameAsync(string technologyName)
{
return await this._context.Technologies
@@ -41,25 +25,6 @@ namespace DevHive.Data.Repositories
}
#endregion
- #region Edit
- public async Task<bool> EditAsync(Technology newEntity)
- {
- this._context.Technologies.Update(newEntity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
- #region Delete
- public async Task<bool> DeleteAsync(Technology entity)
- {
- this._context.Technologies
- .Remove(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Validations
public async Task<bool> 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<User>, IUserRepository
{
private readonly DevHiveContext _context;
public UserRepository(DevHiveContext context)
+ :base(context)
{
this._context = context;
}
- #region Create
-
- public async Task<bool> AddAsync(User entity)
- {
- await this._context.Users
- .AddAsync(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
public IEnumerable<User> QueryAll()
@@ -38,7 +28,7 @@ namespace DevHive.Data.Repositories
.AsEnumerable();
}
- public async Task<User> GetByIdAsync(Guid id)
+ public override async Task<User> 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<bool> 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<bool> DeleteAsync(User entity)
- {
- this._context.Users
- .Remove(entity);
-
- return await this.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Validations
public async Task<bool> DoesUserExistAsync(Guid id)