CodeLiturgy.Dashboard/BlueWest.Api/Users/UserRepository.cs

225 lines
7.4 KiB
C#
Raw Normal View History

2022-09-10 00:33:17 +03:00
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;
/// <summary>
/// Users Repository
/// </summary>
public class UserRepository : UserStore<ApplicationUser, ApplicationRole, ApplicationUserDbContext>, IUsersRepo
{
private readonly ApplicationUserDbContext _context;
public UserRepository(ApplicationUserDbContext context, IdentityErrorDescriber describer = null) : base(context, describer)
{
_context = context;
}
/// <summary>
/// Get Application Users
/// </summary>
/// <returns></returns>
public async Task<IEnumerable<ApplicationUser>> GetUsers()
{
var users = await _context.Users.ToListAsync();
return users;
}
/// <summary>
/// Create Application User
/// </summary>
/// <param name="user"></param>
public async Task CreateUser(ApplicationUser user)
{
await CreateAsync(user, CancellationToken.None);
}
/// <summary>
/// Save Changes
/// </summary>
public async Task SaveChanges()
{
await _context.SaveChangesAsync();
}
private async Task<bool> SaveChanges(ApplicationUser user)
{
_context.Users.Update(user);
return await _context.SaveChangesAsync() > 0;
}
/// <summary>
/// Dispose repository
/// </summary>
public void Dispose()
{
_context.Dispose();
}
/// <inheritdoc />
public override Task<string> GetUserIdAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.Id.ToString());
}
/// <inheritdoc />
public override Task<string> GetUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.UserName);
}
/// <inheritdoc />
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);
}
/// <inheritdoc />
public override Task<string> GetNormalizedUserNameAsync(ApplicationUser user, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.NormalizedUserName);
}
/// <inheritdoc />
public override async Task<IdentityResult> 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();
}
/// <inheritdoc />
public override async Task<IdentityResult> UpdateAsync(ApplicationUser user, CancellationToken cancellationToken)
{
_context.Users.Update(user);
var success = await _context.SaveChangesAsync(cancellationToken) > 0;
if (success) return IdentityResult.Success;
return IdentityResult.Failed();
}
/// <inheritdoc />
public override async Task<IdentityResult> 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;
}
/// <inheritdoc />
public async Task<ApplicationUser> GetUserById(string id)
{
var db = _context.Users;
var user = await db.FirstOrDefaultAsync(u => u.Id.ToString() == id);
return user;
}
/// <inheritdoc />
public override Task<string> GetPasswordHashAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.PasswordHash);
}
/// <inheritdoc />
public override Task<bool> HasPasswordAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<bool>(cancellationToken);
}
return Task.FromResult(!string.IsNullOrEmpty(user.PasswordHash));
}
/// <inheritdoc />
public override Task<string> GetEmailAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.Email);
}
/// <inheritdoc />
public override Task<bool> GetEmailConfirmedAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<bool>(cancellationToken);
}
return Task.FromResult(user.EmailConfirmed);
}
/// <inheritdoc />
public override async Task<ApplicationUser> 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;
}
/// <inheritdoc />
public override Task<string> GetNormalizedEmailAsync(ApplicationUser user, CancellationToken cancellationToken = default)
{
base.GetNormalizedEmailAsync(user, cancellationToken);
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<string>(cancellationToken);
}
return Task.FromResult(user.NormalizedEmail);
}
}