diff options
Diffstat (limited to 'src/DevHive.Data/Repositories')
| -rw-r--r-- | src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs | 13 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/Contracts/IPostRepository.cs | 21 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs | 15 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs | 13 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/Contracts/IUserRepository.cs | 26 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/IRepository.cs (renamed from src/DevHive.Data/Repositories/Contracts/IRepository.cs) | 2 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/LanguageRepository.cs | 55 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/PostRepository.cs | 6 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/RoleRepository.cs | 3 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/TechnologyRepository.cs | 5 | ||||
| -rw-r--r-- | src/DevHive.Data/Repositories/UserRepository.cs | 162 |
11 files changed, 167 insertions, 154 deletions
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<Language> - { - public Task<bool> DoesLanguageNameExist(string languageName); - - public Task<bool> 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<Post> - { - public Task<bool> AddCommentAsync(Comment entity); - - public Task<Comment> GetCommentByIdAsync(Guid id); - - public Task<bool> EditCommentAsync(Comment newEntity); - - public Task<bool> DeleteCommentAsync(Comment entity); - - public Task<bool> DoesPostExist(Guid postId); - - public Task<bool> DoesCommentExist(Guid id); - } -} 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<Role> - { - public Task<Role> GetByNameAsync(string name); - - public Task<bool> DoesNameExist(string name); - - public Task<bool> 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<Technology> - { - public Task<bool> DoesTechnologyNameExist(string technologyName); - - public Task<bool> 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<User> - { - public Task<bool> AddFriendAsync(User user, User friend); - - public IEnumerable<User> QueryAll(); - - public Task<User> GetByUsername(string username); - - public Task<bool> RemoveFriendAsync(User user, User friend); - - public bool DoesUserExist(Guid id); - - public bool DoesUserHaveThisUsername(Guid id, string username); - - public Task<bool> DoesUsernameExist(string username); - - public Task<bool> DoesEmailExist(string email); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs index 37c5170..920ba13 100644 --- a/src/DevHive.Data/Repositories/Contracts/IRepository.cs +++ b/src/DevHive.Data/Repositories/IRepository.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace DevHive.Data.Repositories.Contracts +namespace DevHive.Data.Repositories { public interface IRepository<TEntity> where TEntity : class 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<Language> { private readonly DevHiveContext _context; @@ -16,7 +16,8 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task<bool> 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<Language> GetByIdAsync(Guid id) { return await this._context .Set<Language>() .FindAsync(id); } + #endregion - public async Task<bool> DoesLanguageNameExist(string languageName) - { - return await this._context - .Set<Language>() - .AsNoTracking() - .AnyAsync(r => r.Name == languageName); - } - - public async Task<bool> DoesLanguageExist(Guid id) - { - return await this._context - .Set<Language>() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } + #region Update - //Update public async Task<bool> EditAsync(Language newEntity) { - this._context - .Set<Language>() - .Update(newEntity); + this._context + .Set<Language>() + .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Delete - //Delete public async Task<bool> 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<bool> DoesLanguageNameExistAsync(string languageName) + { + return await this._context.Languages + .AnyAsync(r => r.Name == languageName); + } + + public async Task<bool> 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<Post> { private readonly DevHiveContext _context; @@ -88,6 +87,8 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #region Validations + public async Task<bool> 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<Role> { 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<Technology> { private DevHiveContext _context; @@ -27,7 +26,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Read + //Read public async Task<Technology> 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<User> { private readonly DevHiveContext _context; @@ -18,11 +17,11 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task<bool> AddAsync(User entity) { - await this._context - .Set<User>() + 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<bool> AddLanguageToUserAsync(User user, Language language) + { + this._context.Update(user); + + user.Langauges.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() { - return this._context - .Set<User>() + return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); @@ -48,8 +66,7 @@ namespace DevHive.Data.Repositories public async Task<User> GetByIdAsync(Guid id) { - return await this._context - .Set<User>() + 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<User> GetByUsername(string username) { - return await this._context - .Set<User>() + return await this._context.Users .Include(u => u.Roles) .FirstOrDefaultAsync(x => x.UserName == username); } - //Update + public IList<Language> GetUserLanguages(User user) + { + return user.Langauges; + } + + public Language GetUserLanguage(User user, Language language) + { + return user.Langauges + .FirstOrDefault(x => x.Id == language.Id); + } + + public IList<Technology> 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<bool> 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<bool> 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<bool> 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<bool> DeleteAsync(User entity) { - this._context - .Set<User>() + 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<bool> RemoveLanguageFromUserAsync(User user, Language language) { - return this._context - .Set<User>() - .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<bool> RemoveTechnologyFromUserAsync(User user, Technology technology) { - return this._context - .Set<User>() - .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<bool> DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AnyAsync(x => x.Id == id); } - public async Task<bool> DoesUsernameExist(string username) + public async Task<bool> DoesUsernameExistAsync(string username) { - return await this._context - .Set<User>() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.UserName == username); } - public async Task<bool> DoesEmailExist(string email) + public async Task<bool> DoesEmailExistAsync(string email) { - return await this._context - .Set<User>() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task<bool> 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 } } |
