47 lines
1.4 KiB
C#
47 lines
1.4 KiB
C#
using FluentResults;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NexusReader.Application.Commands.Sync;
|
|
using NexusReader.Domain.Entities;
|
|
using NexusReader.Infrastructure.Persistence;
|
|
using NexusReader.Infrastructure.RealTime;
|
|
|
|
namespace NexusReader.Infrastructure.Handlers;
|
|
|
|
public class UpdateReadingProgressCommandHandler : IRequestHandler<UpdateReadingProgressCommand, Result>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
private readonly IHubContext<SyncHub> _hubContext;
|
|
|
|
public UpdateReadingProgressCommandHandler(
|
|
AppDbContext context,
|
|
IHubContext<SyncHub> hubContext)
|
|
{
|
|
_context = context;
|
|
_hubContext = hubContext;
|
|
}
|
|
|
|
public async Task<Result> Handle(UpdateReadingProgressCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == request.UserId, cancellationToken);
|
|
if (user == null)
|
|
{
|
|
return Result.Fail("User not found.");
|
|
}
|
|
|
|
var now = DateTime.UtcNow;
|
|
user.LastReadPageId = request.PageId;
|
|
user.LastReadAt = now;
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
// Broadcast to other devices
|
|
await _hubContext.Clients
|
|
.Group($"User_{request.UserId}")
|
|
.SendAsync("ProgressUpdated", request.PageId, now, cancellationToken);
|
|
|
|
return Result.Ok();
|
|
}
|
|
}
|