From 54d081a513117c732ab4d62312b440d37dfe0d67 Mon Sep 17 00:00:00 2001 From: transtrike Date: Tue, 15 Dec 2020 19:38:50 +0200 Subject: User Controller, Service & Data implemented --- src/DevHive.Data/Repositories/UserRepository.cs | 73 ++++++++++++++++++------- 1 file changed, 53 insertions(+), 20 deletions(-) (limited to 'src/DevHive.Data/Repositories') diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs index 5b30c30..13ee2bc 100644 --- a/src/DevHive.Data/Repositories/UserRepository.cs +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -17,21 +17,7 @@ namespace DevHive.Data.Repositories this._context = context; } - public bool DoesUserExist(Guid id) - { - return this._context - .Set() - .Any(x => x.Id == id); - } - - public bool HasThisUsername(Guid id, string username) - { - return this._context - .Set() - .Any(x => x.Id == id && - x.UserName == username); - } - + //Create public async Task AddAsync(User entity) { await this._context @@ -40,24 +26,31 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } - - public IEnumerable Query(int count) + + //Read + public IEnumerable QueryAll() { return this._context .Set() .AsNoTracking() - .Take(count) .AsEnumerable(); - } - public async Task FindByIdAsync(Guid id) + public async Task GetByIdAsync(Guid id) { return await this._context .Set() .FindAsync(id); } + public async Task GetByUsername(string username) + { + return await this._context + .Set() + .FirstOrDefaultAsync(x => x.UserName == username); + } + + //Update public async Task EditAsync(User newEntity) { this._context @@ -67,6 +60,7 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } + //Delete public async Task DeleteAsync(User entity) { this._context @@ -75,5 +69,44 @@ namespace DevHive.Data.Repositories await this._context.SaveChangesAsync(); } + + //Validations + public bool DoesUserExist(Guid id) + { + return this._context + .Set() + .Any(x => x.Id == id); + } + + public Task IsUsernameValid(string username) + { + return this._context + .Set() + .AnyAsync(u => u.UserName == username); + } + + public bool DoesUserHaveThisUsername(Guid id, string username) + { + return this._context + .Set() + .Any(x => x.Id == id && + x.UserName == username); + } + + public async Task DoesUsernameExist(string username) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(u => u.UserName == username); + } + + public async Task DoesEmailExist(string email) + { + return await this._context + .Set() + .AsNoTracking() + .AnyAsync(u => u.Email == email); + } } } -- cgit v1.2.3