using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using BlueWest.Data; using BlueWest.WebApi.EF; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace BlueWest.WebApi.Context.Users; /// /// Users Repository /// public class UserRepository : UserStore, IUsersRepo { private readonly ApplicationUserDbContext _context; public UserRepository(ApplicationUserDbContext context, IdentityErrorDescriber describer = null) : base(context, describer) { _context = context; } /// /// Get Application Users /// /// public async Task> GetUsers() { var users = await _context.Users.ToListAsync(); return users; } /// /// Create Application User /// /// public async Task CreateUser(ApplicationUser user) { await CreateAsync(user, CancellationToken.None); } /// /// Save Changes /// public async Task SaveChanges() { await _context.SaveChangesAsync(); } private async Task SaveChanges(ApplicationUser user) { _context.Users.Update(user); return await _context.SaveChangesAsync() > 0; } /// /// Dispose repository /// public void Dispose() { _context.Dispose(); } /// public override Task GetUserIdAsync(ApplicationUser user, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.Id.ToString()); } /// public override Task GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.UserName); } /// public override async Task SetUserNameAsync(ApplicationUser user, string userName, CancellationToken cancellationToken) { var foundUser = await _context.Users.FirstOrDefaultAsync(x => x.Id == user.Id, cancellationToken: cancellationToken); if (foundUser == null) return; foundUser.UserName = userName; await SaveChanges(user); } /// public override Task GetNormalizedUserNameAsync(ApplicationUser user, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.NormalizedUserName); } /// public override async Task CreateAsync(ApplicationUser user, CancellationToken cancellationToken) { var u = await _context.AddAsync(user, cancellationToken); if(u.State == EntityState.Added) { await SaveChanges(); return IdentityResult.Success; } return IdentityResult.Failed(); } /// public override async Task UpdateAsync(ApplicationUser user, CancellationToken cancellationToken) { _context.Users.Update(user); var success = await _context.SaveChangesAsync(cancellationToken) > 0; if (success) return IdentityResult.Success; return IdentityResult.Failed(); } /// public override async Task DeleteAsync(ApplicationUser user, CancellationToken cancellationToken) { var foundUser = await _context.Users.FirstOrDefaultAsync(x=> x.Id == user.Id, cancellationToken: cancellationToken); var error = new IdentityError {Description = "ApplicationUser Not found"}; if (foundUser == null) return IdentityResult.Failed(error); _context.Users.Remove(foundUser); return IdentityResult.Success; } /// public async Task GetUserById(string id) { var db = _context.Users; var user = await db.FirstOrDefaultAsync(u => u.Id.ToString() == id); return user; } /// public override Task GetPasswordHashAsync(ApplicationUser user, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.PasswordHash); } /// public override Task HasPasswordAsync(ApplicationUser user, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash)); } /// public override Task GetEmailAsync(ApplicationUser user, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.Email); } /// public override Task GetEmailConfirmedAsync(ApplicationUser user, CancellationToken cancellationToken = default) { if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.EmailConfirmed); } /// public override async Task FindByEmailAsync(string normalizedEmail, CancellationToken cancellationToken = default) { ApplicationUser user = null; var db = _context.Users; user = await db.FirstOrDefaultAsync(u => u.NormalizedEmail == normalizedEmail, cancellationToken: cancellationToken); return user; } /// public override Task GetNormalizedEmailAsync(ApplicationUser user, CancellationToken cancellationToken = default) { base.GetNormalizedEmailAsync(user, cancellationToken); if (cancellationToken.IsCancellationRequested) { return Task.FromCanceled(cancellationToken); } return Task.FromResult(user.NormalizedEmail); } }