mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 11:53:12 +00:00
84 lines
3 KiB
C#
84 lines
3 KiB
C#
|
|
using Data.SeaHavenIndustries;
|
||
|
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
using SeaHaven.DataServices.Interfaces;
|
||
|
|
|
||
|
|
namespace SeaHaven.DataServices.Implementation
|
||
|
|
{
|
||
|
|
public class ForgetPasswordDataService : IForgetPasswordDataService
|
||
|
|
{
|
||
|
|
private readonly ApplicationDbContext _context;
|
||
|
|
|
||
|
|
public ForgetPasswordDataService(ApplicationDbContext context)
|
||
|
|
{
|
||
|
|
_context = context;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task ReplaceCodeAsync(string email, string userId, string code, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var normalizedEmail = email.ToLower().Trim();
|
||
|
|
|
||
|
|
var existing = await _context.ForgetPasswordCodes
|
||
|
|
.Where(u => u.Email.ToLower().Trim() == normalizedEmail)
|
||
|
|
.FirstOrDefaultAsync(cancellationToken);
|
||
|
|
|
||
|
|
if (existing != null)
|
||
|
|
{
|
||
|
|
_context.ForgetPasswordCodes.Remove(existing);
|
||
|
|
await _context.SaveChangesAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
var forgetPasswordCode = new ForgetPasswordCode
|
||
|
|
{
|
||
|
|
Email = email,
|
||
|
|
UserId = userId,
|
||
|
|
Code = code
|
||
|
|
};
|
||
|
|
|
||
|
|
_context.ForgetPasswordCodes.Add(forgetPasswordCode);
|
||
|
|
await _context.SaveChangesAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> CodeExistsAsync(string code, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
return await _context.ForgetPasswordCodes
|
||
|
|
.AsNoTracking()
|
||
|
|
.Where(u => u.Code == code)
|
||
|
|
.AnyAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<bool> ExistsByEmailAndCodeAsync(string email, string code, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var normalizedEmail = email.ToLower().Trim();
|
||
|
|
var trimmedCode = code.Trim();
|
||
|
|
|
||
|
|
return await _context.ForgetPasswordCodes
|
||
|
|
.AsNoTracking()
|
||
|
|
.Where(u => u.Email.ToLower().Trim() == normalizedEmail && u.Code == trimmedCode)
|
||
|
|
.AnyAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<ForgetPasswordCode?> GetByEmailAsync(string email, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var normalizedEmail = email.ToLower().Trim();
|
||
|
|
|
||
|
|
return await _context.ForgetPasswordCodes
|
||
|
|
.Where(u => u.Email.ToLower().Trim() == normalizedEmail)
|
||
|
|
.FirstOrDefaultAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task RemoveByEmailAsync(string email, CancellationToken cancellationToken)
|
||
|
|
{
|
||
|
|
var normalizedEmail = email.ToLower().Trim();
|
||
|
|
var records = await _context.ForgetPasswordCodes
|
||
|
|
.Where(code => code.Email.ToLower().Trim() == normalizedEmail)
|
||
|
|
.ToListAsync(cancellationToken);
|
||
|
|
|
||
|
|
if (records.Count == 0)
|
||
|
|
return;
|
||
|
|
|
||
|
|
_context.ForgetPasswordCodes.RemoveRange(records);
|
||
|
|
await _context.SaveChangesAsync(cancellationToken);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|