From 98e17766b203734a1817eed94338e2d25f4395f7 Mon Sep 17 00:00:00 2001 From: transtrike Date: Sat, 13 Feb 2021 16:20:18 +0200 Subject: Project Restructure P.1 --- .../DevHive.Data/Repositories/UserRepository.cs | 108 +++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/Data/DevHive.Data/Repositories/UserRepository.cs (limited to 'src/Data/DevHive.Data/Repositories/UserRepository.cs') diff --git a/src/Data/DevHive.Data/Repositories/UserRepository.cs b/src/Data/DevHive.Data/Repositories/UserRepository.cs new file mode 100644 index 0000000..6e97e60 --- /dev/null +++ b/src/Data/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, IUserRepository + { + private readonly DevHiveContext _context; + + public UserRepository(DevHiveContext context) + : base(context) + { + this._context = context; + } + + #region Read + public override async Task 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 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 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 DoesUserExistAsync(Guid id) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(x => x.Id == id); + } + + public async Task DoesUsernameExistAsync(string username) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(u => u.UserName == username); + } + + public async Task DoesEmailExistAsync(string email) + { + return await this._context.Users + .AsNoTracking() + .AnyAsync(u => u.Email == email); + } + + public async Task ValidateFriendsCollectionAsync(List 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 + } +} -- cgit v1.2.3