From a1e46b76a1299e35b1ac8cae69e77c66d74224a6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 8 May 2021 16:49:17 +0300 Subject: Moved user repository logic into user service logic (no more repos) --- ExamTemplate/Data/Repositories/UserRepository.cs | 66 ------------------------ 1 file changed, 66 deletions(-) delete mode 100644 ExamTemplate/Data/Repositories/UserRepository.cs (limited to 'ExamTemplate/Data/Repositories/UserRepository.cs') diff --git a/ExamTemplate/Data/Repositories/UserRepository.cs b/ExamTemplate/Data/Repositories/UserRepository.cs deleted file mode 100644 index 3e5ceaa..0000000 --- a/ExamTemplate/Data/Repositories/UserRepository.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System.Security.Claims; -using System.Threading.Tasks; -using ExamTemplate.Data.Models; -using Microsoft.AspNetCore.Identity; -using Microsoft.EntityFrameworkCore; - -namespace ExamTemplate.Data.Repositories -{ - public class UserRepository - { - private readonly TemplateContext _context; - private readonly UserManager _userManager; - private readonly RoleManager _roleManager; - - public UserRepository(TemplateContext templateContext, UserManager userManager, RoleManager roleManager) - { - this._context = templateContext; - this._userManager = userManager; - this._roleManager = roleManager; - } - - public async Task GetByUsernameAsync(string username) - { - return await this._userManager.Users - .Include(x => x.Roles) - .FirstOrDefaultAsync(x => x.UserName == username); - } - - public async Task GetByClaimsAsync(ClaimsPrincipal claimsPrincipal) - { - return await this._userManager.GetUserAsync(claimsPrincipal); - } - - public async Task AddAsync(User user, string password) - { - user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, password); - IdentityResult result = await this._userManager.CreateAsync(user); - - return result.Succeeded; - } - - public async Task AddRoleToUserAsync(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._context.SaveChangesAsync() >= 1; - } - - return succeeded; - } - - public async Task EditUserAsync(User user) - { - IdentityResult result = await this._userManager.UpdateAsync(user); - - return result.Succeeded; - } - - public async Task VerifyPasswordAsync(User user, string password) - { - return await this._userManager.CheckPasswordAsync(user, password); - } - } -} -- cgit v1.2.3