aboutsummaryrefslogtreecommitdiff
path: root/src/Data/DevHive.Data/Repositories/UserRepository.cs
blob: d5704807cd14bf7353762e8bae2f64c5c1c357c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using DevHive.Data.Interfaces;
using DevHive.Data.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

namespace DevHive.Data.Repositories
{
	public class UserRepository : BaseRepository<User>, IUserRepository
	{
		private readonly UserManager<User> _userManager;
		private readonly RoleManager<Role> _roleManager;

		public UserRepository(DevHiveContext context, UserManager<User> userManager, RoleManager<Role> roleManager)
			: base(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._userManager.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._userManager.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 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);

			user.ProfilePicture.PictureURL = pictureUrl;

			return await this.SaveChangesAsync();
		}
		#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._userManager.Users.AnyAsync(x => x.Id == id);
		}

		public async Task<bool> DoesUsernameExistAsync(string username)
		{
			return await this._userManager.Users
				.AsNoTracking()
				.AnyAsync(u => u.UserName == username);
		}

		public async Task<bool> DoesEmailExistAsync(string email)
		{
			return await this._userManager.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.DoesUsernameExistAsync(username))
				{
					valid = false;
					break;
				}
			}
			return valid;
		}

		public async Task<bool> DoesUserHaveThisUsernameAsync(Guid id, string username)
		{
			return await this._userManager.Users
				.AnyAsync(x => x.Id == id &&
					x.UserName == username);
		}
		#endregion
	}
}