using Microsoft.AspNetCore.Identity; using System; using System.Threading.Tasks; using System.Threading; using MvcMovie.Models; using Microsoft.EntityFrameworkCore; namespace MvcMovie.Data { public class AppUserStore : IUserPasswordStore { private readonly MvcMovieContext _context; public AppUserStore(MvcMovieContext context) { _context = context; } public async Task CreateAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); try { _context.Add(user); await _context.SaveChangesAsync(); return IdentityResult.Success; } catch(Exception ex) { return IdentityResult.Failed(new IdentityError { Description = $"Could not insert user {ex.Message}." }); } } public async Task DeleteAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); var appUser = await _context.Users.FirstOrDefaultAsync(m => m.Id == user.Id); if (appUser == null) throw new ArgumentException("User Not Found"); _context.Users.Remove(appUser); await _context.SaveChangesAsync(); return IdentityResult.Success; } public void Dispose() { } public async Task FindByIdAsync(string userId, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (userId == null) throw new ArgumentNullException(nameof(userId)); Guid idGuid; if(!Guid.TryParse(userId, out idGuid)) { throw new ArgumentException("Not a valid Guid id", nameof(userId)); } return await _context.Users.FirstOrDefaultAsync(m => m.Id == idGuid); } public async Task FindByNameAsync(string normalizedUserName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (normalizedUserName == null) throw new ArgumentNullException(nameof(normalizedUserName)); return await _context.Users.FirstOrDefaultAsync(m => m.NormalizedUserName == normalizedUserName); } public Task GetNormalizedUserNameAsync(AppUser user, CancellationToken cancellationToken) { throw new NotImplementedException(); } public Task GetPasswordHashAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.PasswordHash); } public Task GetUserIdAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.Id.ToString()); } public Task GetUserNameAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); return Task.FromResult(user.UserName); } public Task HasPasswordAsync(AppUser user, CancellationToken cancellationToken) { return Task.FromResult(true); } public Task SetNormalizedUserNameAsync(AppUser user, string normalizedName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (normalizedName == null) throw new ArgumentNullException(nameof(normalizedName)); user.NormalizedUserName = normalizedName; return Task.FromResult(null); } public Task SetPasswordHashAsync(AppUser user, string passwordHash, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (passwordHash == null) throw new ArgumentNullException(nameof(passwordHash)); user.PasswordHash = passwordHash; return Task.FromResult(null); } public Task SetUserNameAsync(AppUser user, string userName, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); if (userName == null) throw new ArgumentNullException(nameof(userName)); user.UserName = userName; return Task.FromResult(null); } public async Task UpdateAsync(AppUser user, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (user == null) throw new ArgumentNullException(nameof(user)); try { _context.Update(user); await _context.SaveChangesAsync(); return IdentityResult.Success; } catch(Exception ex) { return IdentityResult.Failed(new IdentityError { Description = $"Could not update user {ex.Message}." }); } } } }