aboutsummaryrefslogtreecommitdiff
path: root/src/DevHive.Data/Repositories
diff options
context:
space:
mode:
Diffstat (limited to 'src/DevHive.Data/Repositories')
-rw-r--r--src/DevHive.Data/Repositories/BaseRepository.cs65
-rw-r--r--src/DevHive.Data/Repositories/LanguageRepository.cs50
-rw-r--r--src/DevHive.Data/Repositories/PostRepository.cs77
-rw-r--r--src/DevHive.Data/Repositories/RoleRepository.cs58
-rw-r--r--src/DevHive.Data/Repositories/TechnologyRepository.cs53
-rw-r--r--src/DevHive.Data/Repositories/UserRepository.cs125
6 files changed, 118 insertions, 310 deletions
diff --git a/src/DevHive.Data/Repositories/BaseRepository.cs b/src/DevHive.Data/Repositories/BaseRepository.cs
new file mode 100644
index 0000000..dabb35b
--- /dev/null
+++ b/src/DevHive.Data/Repositories/BaseRepository.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Threading.Tasks;
+using DevHive.Data.Repositories.Interfaces;
+using Microsoft.EntityFrameworkCore;
+
+namespace DevHive.Data.Repositories
+{
+ public class BaseRepository<TEntity> : IRepository<TEntity>
+ where TEntity : class
+ {
+ 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();
+
+ return result >= 1;
+ }
+ }
+}
diff --git a/src/DevHive.Data/Repositories/LanguageRepository.cs b/src/DevHive.Data/Repositories/LanguageRepository.cs
index e644fc4..d7ee609 100644
--- a/src/DevHive.Data/Repositories/LanguageRepository.cs
+++ b/src/DevHive.Data/Repositories/LanguageRepository.cs
@@ -1,75 +1,31 @@
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 : 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
- .Set<Language>()
- .AddAsync(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
-
- public async Task<Language> GetByIdAsync(Guid id)
- {
- return await this._context
- .Set<Language>()
- .FindAsync(id);
- }
-
public async Task<Language> GetByNameAsync(string languageName)
{
return await this._context.Languages
+ .AsNoTracking()
.FirstOrDefaultAsync(x => x.Name == languageName);
}
#endregion
- #region Update
-
- public async Task<bool> EditAsync(Language newEntity)
- {
- this._context
- .Set<Language>()
- .Update(newEntity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
- #region Delete
-
- public async Task<bool> DeleteAsync(Language entity)
- {
- this._context
- .Set<Language>()
- .Remove(entity);
-
- return await RepositoryMethods.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 3be14e3..9230a2e 100644
--- a/src/DevHive.Data/Repositories/PostRepository.cs
+++ b/src/DevHive.Data/Repositories/PostRepository.cs
@@ -1,107 +1,84 @@
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 : IPostRepository
+ public class PostRepository : BaseRepository<Post>, IPostRepository
{
private readonly DevHiveContext _context;
public PostRepository(DevHiveContext context)
+ : base(context)
{
this._context = context;
}
- //Create
- public async Task<bool> AddAsync(Post post)
- {
- await this._context
- .Set<Post>()
- .AddAsync(post);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
+ #region Create
public async Task<bool> AddCommentAsync(Comment entity)
{
- await this._context
- .Set<Comment>()
+ await this._context.Comments
.AddAsync(entity);
- return await RepositoryMethods.SaveChangesAsync(this._context);
+ return await this.SaveChangesAsync(this._context);
}
+ #endregion
- //Read
- public async Task<Post> GetByIdAsync(Guid id)
+ #region Read
+ public async Task<Post> GetPostByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated)
{
- return await this._context
- .Set<Post>()
- .FindAsync(id);
+ return await this._context.Posts
+ .FirstOrDefaultAsync(p => p.IssuerId == issuerId &&
+ p.TimeCreated == timeCreated);
}
public async Task<Comment> GetCommentByIdAsync(Guid id)
{
- return await this._context
- .Set<Comment>()
+ return await this._context.Comments
.FindAsync(id);
}
- //Update
- public async Task<bool> EditAsync(Post newPost)
+ public async Task<Comment> GetCommentByIssuerAndTimeCreatedAsync(Guid issuerId, DateTime timeCreated)
{
- this._context
- .Set<Post>()
- .Update(newPost);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
+ return await this._context.Comments
+ .FirstOrDefaultAsync(p => p.IssuerId == issuerId &&
+ p.TimeCreated == timeCreated);
}
+ #endregion
+ #region Update
public async Task<bool> EditCommentAsync(Comment newEntity)
{
- this._context
- .Set<Comment>()
+ this._context.Comments
.Update(newEntity);
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- //Delete
- public async Task<bool> DeleteAsync(Post post)
- {
- this._context
- .Set<Post>()
- .Remove(post);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
+ return await this.SaveChangesAsync(this._context);
}
+ #endregion
+ #region Delete
public async Task<bool> DeleteCommentAsync(Comment entity)
{
- this._context
- .Set<Comment>()
+ this._context.Comments
.Remove(entity);
- return await RepositoryMethods.SaveChangesAsync(this._context);
+ 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 ca3fb8b..156792d 100644
--- a/src/DevHive.Data/Repositories/RoleRepository.cs
+++ b/src/DevHive.Data/Repositories/RoleRepository.cs
@@ -1,83 +1,43 @@
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 RoleRepository : IRoleRepository
+ public class RoleRepository : BaseRepository<Role>, IRoleRepository
{
private readonly DevHiveContext _context;
public RoleRepository(DevHiveContext context)
+ :base(context)
{
this._context = context;
}
- //Create
- public async Task<bool> AddAsync(Role entity)
- {
- await this._context
- .Set<Role>()
- .AddAsync(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- //Read
- public async Task<Role> GetByIdAsync(Guid id)
- {
- return await this._context
- .Set<Role>()
- .FindAsync(id);
- }
-
+ #region Read
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
- public async Task<bool> EditAsync(Role newEntity)
- {
- Role role = await this.GetByIdAsync(newEntity.Id);
-
- this._context
- .Entry(role)
- .CurrentValues
- .SetValues(newEntity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- //Delete
- public async Task<bool> DeleteAsync(Role entity)
- {
- this._context
- .Set<Role>()
- .Remove(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
+ #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 73827a7..83cc7aa 100644
--- a/src/DevHive.Data/Repositories/TechnologyRepository.cs
+++ b/src/DevHive.Data/Repositories/TechnologyRepository.cs
@@ -1,79 +1,34 @@
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 : 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
- .Set<Technology>()
- .AddAsync(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
-
- public async Task<Technology> GetByIdAsync(Guid id)
- {
- return await this._context
- .Set<Technology>()
- .FindAsync(id);
- }
public async Task<Technology> GetByNameAsync(string technologyName)
{
return await this._context.Technologies
+ .AsNoTracking()
.FirstOrDefaultAsync(x => x.Name == technologyName);
}
#endregion
- #region Edit
-
- public async Task<bool> EditAsync(Technology newEntity)
- {
- this._context
- .Set<Technology>()
- .Update(newEntity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
- #region Delete
-
- public async Task<bool> DeleteAsync(Technology entity)
- {
- this._context
- .Set<Technology>()
- .Remove(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #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.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs
index 6d4a0bf..1511c63 100644
--- a/src/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/DevHive.Data/Repositories/UserRepository.cs
@@ -2,59 +2,22 @@ using System;
using System.Collections.Generic;
using System.Linq;
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 UserRepository : 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 RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> AddFriendToUserAsync(User user, User friend)
- {
- this._context.Update(user);
- user.Friends.Add(friend);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> AddLanguageToUserAsync(User user, Language language)
- {
- this._context.Update(user);
-
- user.Languages.Add(language);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> AddTechnologyToUserAsync(User user, Technology technology)
- {
- this._context.Update(user);
-
- user.Technologies.Add(technology);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Read
public IEnumerable<User> QueryAll()
@@ -65,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)
@@ -78,11 +41,13 @@ namespace DevHive.Data.Repositories
public async Task<User> 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);
}
- public IList<Language> GetUserLanguages(User user)
+ public HashSet<Language> GetUserLanguages(User user)
{
return user.Languages;
}
@@ -93,7 +58,7 @@ namespace DevHive.Data.Repositories
.FirstOrDefault(x => x.Id == language.Id);
}
- public IList<Technology> GetUserTechnologies(User user)
+ public HashSet<Technology> GetUserTechnologies(User user)
{
return user.Technologies;
}
@@ -105,83 +70,12 @@ namespace DevHive.Data.Repositories
}
#endregion
- #region Update
-
- public async Task<bool> EditAsync(User newEntity)
- {
- User user = await this.GetByIdAsync(newEntity.Id);
-
- this._context
- .Entry(user)
- .CurrentValues
- .SetValues(newEntity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> EditUserLanguageAsync(User user, Language oldLang, Language newLang)
- {
- this._context.Update(user);
-
- user.Languages.Remove(oldLang);
- user.Languages.Add(newLang);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> EditUserTechnologyAsync(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<bool> DeleteAsync(User entity)
- {
- this._context.Users
- .Remove(entity);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> RemoveFriendAsync(User user, User friend)
- {
- this._context.Update(user);
- user.Friends.Remove(friend);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> RemoveLanguageFromUserAsync(User user, Language language)
- {
- this._context.Update(user);
-
- user.Languages.Remove(language);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
-
- public async Task<bool> RemoveTechnologyFromUserAsync(User user, Technology technology)
- {
- this._context.Update(user);
-
- user.Technologies.Remove(technology);
-
- return await RepositoryMethods.SaveChangesAsync(this._context);
- }
- #endregion
-
#region Validations
public async Task<bool> DoesUserExistAsync(Guid id)
{
return await this._context.Users
+ .AsNoTracking()
.AnyAsync(x => x.Id == id);
}
@@ -213,6 +107,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);
}