From 11bd1d9a9760c7bc6a601d78b3d89ec9028647a2 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 12 Jan 2021 13:16:39 +0200 Subject: Language layers refactored; User implements adding & removing Languages; Migrations added --- .../Repositories/Contracts/ILanguageRepository.cs | 13 -- .../Repositories/Contracts/IPostRepository.cs | 21 --- .../Repositories/Contracts/IRepository.cs | 21 --- .../Repositories/Contracts/IRoleRepository.cs | 15 -- .../Contracts/ITechnologyRepository.cs | 13 -- .../Repositories/Contracts/IUserRepository.cs | 26 ---- src/DevHive.Data/Repositories/IRepository.cs | 21 +++ .../Repositories/LanguageRepository.cs | 55 ++++--- src/DevHive.Data/Repositories/PostRepository.cs | 6 +- src/DevHive.Data/Repositories/RoleRepository.cs | 3 +- .../Repositories/TechnologyRepository.cs | 5 +- src/DevHive.Data/Repositories/UserRepository.cs | 162 ++++++++++++++++----- 12 files changed, 187 insertions(+), 174 deletions(-) delete mode 100644 src/DevHive.Data/Repositories/Contracts/ILanguageRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IPostRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IRoleRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/ITechnologyRepository.cs delete mode 100644 src/DevHive.Data/Repositories/Contracts/IUserRepository.cs create mode 100644 src/DevHive.Data/Repositories/IRepository.cs (limited to 'src/DevHive.Data/Repositories') 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 - { - public Task DoesLanguageNameExist(string languageName); - - public Task 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 - { - public Task AddCommentAsync(Comment entity); - - public Task GetCommentByIdAsync(Guid id); - - public Task EditCommentAsync(Comment newEntity); - - public Task DeleteCommentAsync(Comment entity); - - public Task DoesPostExist(Guid postId); - - public Task DoesCommentExist(Guid id); - } -} diff --git a/src/DevHive.Data/Repositories/Contracts/IRepository.cs b/src/DevHive.Data/Repositories/Contracts/IRepository.cs deleted file mode 100644 index 37c5170..0000000 --- a/src/DevHive.Data/Repositories/Contracts/IRepository.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace DevHive.Data.Repositories.Contracts -{ - 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 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 - { - public Task GetByNameAsync(string name); - - public Task DoesNameExist(string name); - - public Task 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 - { - public Task DoesTechnologyNameExist(string technologyName); - - public Task 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 - { - public Task AddFriendAsync(User user, User friend); - - public IEnumerable QueryAll(); - - public Task GetByUsername(string username); - - public Task RemoveFriendAsync(User user, User friend); - - public bool DoesUserExist(Guid id); - - public bool DoesUserHaveThisUsername(Guid id, string username); - - public Task DoesUsernameExist(string username); - - public Task DoesEmailExist(string email); - } -} diff --git a/src/DevHive.Data/Repositories/IRepository.cs b/src/DevHive.Data/Repositories/IRepository.cs new file mode 100644 index 0000000..920ba13 --- /dev/null +++ b/src/DevHive.Data/Repositories/IRepository.cs @@ -0,0 +1,21 @@ +using System; +using System.Threading.Tasks; + +namespace DevHive.Data.Repositories +{ + 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 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 { private readonly DevHiveContext _context; @@ -16,7 +16,8 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task 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 GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } + #endregion - public async Task DoesLanguageNameExist(string languageName) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Name == languageName); - } - - public async Task DoesLanguageExist(Guid id) - { - return await this._context - .Set() - .AsNoTracking() - .AnyAsync(r => r.Id == id); - } + #region Update - //Update public async Task EditAsync(Language newEntity) { - this._context - .Set() - .Update(newEntity); + this._context + .Set() + .Update(newEntity); return await RepositoryMethods.SaveChangesAsync(this._context); } + #endregion + + #region Delete - //Delete public async Task 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 DoesLanguageNameExistAsync(string languageName) + { + return await this._context.Languages + .AnyAsync(r => r.Name == languageName); + } + + public async Task 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 { private readonly DevHiveContext _context; @@ -88,6 +87,8 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } + #region Validations + public async Task 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 { 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 { private DevHiveContext _context; @@ -27,7 +26,7 @@ namespace DevHive.Data.Repositories return await RepositoryMethods.SaveChangesAsync(this._context); } - //Read + //Read public async Task 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 { private readonly DevHiveContext _context; @@ -18,11 +17,11 @@ namespace DevHive.Data.Repositories this._context = context; } - //Create + #region Create + public async Task AddAsync(User entity) { - await this._context - .Set() + 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 AddLanguageToUserAsync(User user, Language language) + { + this._context.Update(user); + + user.Langauges.Add(language); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + + public async Task AddTechnologyToUserAsync(User user, Technology technology) + { + this._context.Update(user); + + user.Technologies.Add(technology); + + return await RepositoryMethods.SaveChangesAsync(this._context); + } + #endregion + + #region Read + public IEnumerable QueryAll() { - return this._context - .Set() + return this._context.Users .Include(x => x.Roles) .AsNoTracking() .AsEnumerable(); @@ -48,8 +66,7 @@ namespace DevHive.Data.Repositories public async Task GetByIdAsync(Guid id) { - return await this._context - .Set() + 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 GetByUsername(string username) { - return await this._context - .Set() + return await this._context.Users .Include(u => u.Roles) .FirstOrDefaultAsync(x => x.UserName == username); } - //Update + public IList GetUserLanguages(User user) + { + return user.Langauges; + } + + public Language GetUserLanguage(User user, Language language) + { + return user.Langauges + .FirstOrDefault(x => x.Id == language.Id); + } + + public IList 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 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 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 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 DeleteAsync(User entity) { - this._context - .Set() + 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 RemoveLanguageFromUserAsync(User user, Language language) { - return this._context - .Set() - .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 RemoveTechnologyFromUserAsync(User user, Technology technology) { - return this._context - .Set() - .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 DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AnyAsync(x => x.Id == id); } - public async Task DoesUsernameExist(string username) + public async Task DoesUsernameExistAsync(string username) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.UserName == username); } - public async Task DoesEmailExist(string email) + public async Task DoesEmailExistAsync(string email) { - return await this._context - .Set() + return await this._context.Users .AsNoTracking() .AnyAsync(u => u.Email == email); } + + public async Task 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 } } -- cgit v1.2.3