aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data/Repositories/UserRepository.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/DevHive.Data/Repositories/UserRepository.cs')
-rw-r--r--src/Data/DevHive.Data/Repositories/UserRepository.cs82
1 files changed, 63 insertions, 19 deletions
diff --git a/src/Data/DevHive.Data/Repositories/UserRepository.cs b/src/Data/DevHive.Data/Repositories/UserRepository.cs
index 9fa6eca..d570480 100644
--- a/src/Data/DevHive.Data/Repositories/UserRepository.cs
+++ b/src/Data/DevHive.Data/Repositories/UserRepository.cs
@@ -1,12 +1,8 @@
using System;
using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
using System.Threading.Tasks;
-using AutoMapper.Mappers;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
-using DevHive.Data.Models.Relational;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -14,18 +10,42 @@ namespace DevHive.Data.Repositories
{
public class UserRepository : BaseRepository<User>, IUserRepository
{
- private readonly DevHiveContext _context;
+ private readonly UserManager<User> _userManager;
+ private readonly RoleManager<Role> _roleManager;
- public UserRepository(DevHiveContext context)
+ public UserRepository(DevHiveContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
: base(context)
{
- this._context = context;
+ this._userManager = userManager;
+ this._roleManager = roleManager;
}
+ #region Create
+ public override async Task<bool> AddAsync(User entity)
+ {
+ entity.PasswordHash = this._userManager.PasswordHasher.HashPassword(entity, entity.PasswordHash).ToString();
+ IdentityResult result = await this._userManager.CreateAsync(entity);
+
+ return result.Succeeded;
+ }
+
+ public async Task<bool> AddRoleToUser(User user, string roleName)
+ {
+ bool succeeded = (await this._userManager.AddToRoleAsync(user, roleName)).Succeeded;
+ if (succeeded)
+ {
+ user.Roles.Add(await this._roleManager.FindByNameAsync(roleName));
+ succeeded = await this.SaveChangesAsync();
+ }
+
+ return succeeded;
+ }
+ #endregion
+
#region Read
public override async Task<User> GetByIdAsync(Guid id)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -37,7 +57,7 @@ namespace DevHive.Data.Repositories
public async Task<User> GetByUsernameAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.Include(x => x.Roles)
.Include(x => x.Languages)
.Include(x => x.Technologies)
@@ -49,6 +69,14 @@ namespace DevHive.Data.Repositories
#endregion
#region Update
+ public override async Task<bool> EditAsync(Guid id, User newEntity)
+ {
+ newEntity.Id = id;
+ IdentityResult result = await this._userManager.UpdateAsync(newEntity);
+
+ return result.Succeeded;
+ }
+
public async Task<bool> UpdateProfilePicture(Guid userId, string pictureUrl)
{
User user = await this.GetByIdAsync(userId);
@@ -59,24 +87,41 @@ namespace DevHive.Data.Repositories
}
#endregion
+ #region Delete
+ public override async Task<bool> DeleteAsync(User entity)
+ {
+ IdentityResult result = await this._userManager.DeleteAsync(entity);
+
+ return result.Succeeded;
+ }
+ #endregion
+
#region Validations
+ public async Task<bool> VerifyPassword(User user, string password)
+ {
+ return await this._userManager.CheckPasswordAsync(user, password);
+ }
+
+ public async Task<bool> IsInRoleAsync(User user, string roleName)
+ {
+ return await this._userManager.IsInRoleAsync(user, roleName);
+ }
+
public async Task<bool> DoesUserExistAsync(Guid id)
{
- return await this._context.Users
- .AsNoTracking()
- .AnyAsync(x => x.Id == id);
+ return await this._userManager.Users.AnyAsync(x => x.Id == id);
}
public async Task<bool> DoesUsernameExistAsync(string username)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.UserName == username);
}
public async Task<bool> DoesEmailExistAsync(string email)
{
- return await this._context.Users
+ return await this._userManager.Users
.AsNoTracking()
.AnyAsync(u => u.Email == email);
}
@@ -87,7 +132,7 @@ namespace DevHive.Data.Repositories
foreach (var username in usernames)
{
- if (!await this._context.Users.AnyAsync(x => x.UserName == username))
+ if (!await this.DoesUsernameExistAsync(username))
{
valid = false;
break;
@@ -96,11 +141,10 @@ namespace DevHive.Data.Repositories
return valid;
}
- public bool DoesUserHaveThisUsername(Guid id, string username)
+ public async Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username)
{
- return this._context.Users
- .AsNoTracking()
- .Any(x => x.Id == id &&
+ return await this._userManager.Users
+ .AnyAsync(x => x.Id == id &&
x.UserName == username);
}
#endregion