diff options
| author | Victor S <57849063+transtrike@users.noreply.github.com> | 2021-02-05 10:54:49 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-05 10:54:49 -0800 |
| commit | f4a70c6430db923af9fa9958a11c2d6612cb52cc (patch) | |
| tree | ca0ea403ba5500df20bc8854ec50529a25c64245 /src/DevHive.Data/Repositories/UserRepository.cs | |
| parent | 1ccdefdac025b1b986ad2bd0bc3eda7505d6e7c3 (diff) | |
| parent | 2269b5aa6c8d3dcb407c34fa256200bdc573585a (diff) | |
| download | DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.tar.gz DevHive-f4a70c6430db923af9fa9958a11c2d6612cb52cc.zip | |
Merge pull request #18 from Team-Kaleidoscope/devv0.1
First stage: Complete. Awaiting further progress...
Diffstat (limited to 'src/DevHive.Data/Repositories/UserRepository.cs')
| -rw-r--r-- | src/DevHive.Data/Repositories/UserRepository.cs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/DevHive.Data/Repositories/UserRepository.cs b/src/DevHive.Data/Repositories/UserRepository.cs new file mode 100644 index 0000000..6e97e60 --- /dev/null +++ b/src/DevHive.Data/Repositories/UserRepository.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Threading.Tasks; +using AutoMapper.Mappers; +using DevHive.Data.Interfaces.Repositories; +using DevHive.Data.Models; +using DevHive.Data.RelationModels; +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; + +namespace DevHive.Data.Repositories +{ + public class UserRepository : BaseRepository<User>, IUserRepository + { + private readonly DevHiveContext _context; + + public UserRepository(DevHiveContext context) + : base(context) + { + this._context = context; + } + + #region Read + public override async Task<User> GetByIdAsync(Guid id) + { + return await this._context.Users + .Include(x => x.Roles) + .Include(x => x.Languages) + .Include(x => x.Technologies) + .Include(x => x.Posts) + .Include(x => x.Friends) + .Include(x => x.ProfilePicture) + .FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task<User> GetByUsernameAsync(string username) + { + return await this._context.Users + .Include(x => x.Roles) + .Include(x => x.Languages) + .Include(x => x.Technologies) + .Include(x => x.Posts) + .Include(x => x.Friends) + .Include(x => x.ProfilePicture) + .FirstOrDefaultAsync(x => x.UserName == username); + } + #endregion + + #region Update + public async Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl) + { + User user = await this.GetByIdAsync(userId); + + user.ProfilePicture.PictureURL = pictureUrl; + + return await this.SaveChangesAsync(); + } + #endregion + + #region Validations + public async Task<bool> DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(x => x.Id == id); + } + + public async Task<bool> DoesUsernameExistAsync(string username) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(u => u.UserName == username); + } + + public async Task<bool> DoesEmailExistAsync(string email) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(u => u.Email == email); + } + + public async Task<bool> ValidateFriendsCollectionAsync(List<string> usernames) + { + bool valid = true; + + foreach (var username in usernames) + { + if (!await this._context.Users.AnyAsync(x => x.UserName == username)) + { + valid = false; + break; + } + } + return valid; + } + + public bool DoesUserHaveThisUsername(Guid id, string username) + { + return this._context.Users + .AsNoTracking() + .Any(x => x.Id == id && + x.UserName == username); + } + #endregion + } +} |
