Skip to content

Commit

Permalink
Let AssistantHub update last seen without the possibility of concurre…
Browse files Browse the repository at this point in the history
…ncy errors
  • Loading branch information
NixFey committed Sep 17, 2024
1 parent 33caa7b commit be2a589
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
18 changes: 18 additions & 0 deletions Data/FimRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;

namespace fim_queueing_admin.Data;

/// <summary>
/// Used for anything that can't be easily done with native EF
/// </summary>
/// <param name="dbContext"></param>
public class FimRepository(FimDbContext dbContext)
{
public async Task<bool> SetCartLastSeen(Guid id, DateTime? lastSeen)
{
// Note: EF will automatically parameterize this query, id and lastSeen are *not* just substituted in as strings
var result = await dbContext.Database.ExecuteSqlAsync(
$"UPDATE equipment SET configuration['LastSeen'] = to_jsonb({lastSeen}) WHERE id = {id}");
return result > 0;
}
}
18 changes: 3 additions & 15 deletions Hubs/AssistantHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,20 @@
namespace fim_queueing_admin.Hubs;

[Authorize(AuthTokenScheme.AuthenticationScheme)]
public class AssistantHub(FimDbContext dbContext, AssistantService assistantService, ILogger<AssistantHub> logger) : Hub
public class AssistantHub(FimDbContext dbContext, FimRepository repository, AssistantService assistantService, ILogger<AssistantHub> logger) : Hub
{
private Guid CartId => Guid.Parse(Context.User?.FindFirst(ClaimTypes.CartId)?.Value ??
throw new ApplicationException("No cart id"));

public override async Task OnConnectedAsync()
{
// await dbContext.Carts.Where(c => c.Id == CartId).ExecuteUpdateAsync(c => c
// .SetProperty(p => p.Configuration!.LastSeen, DateTime.MaxValue));
var cart = await dbContext.Carts.FirstOrDefaultAsync(c => c.Id == CartId);
if (cart is not null)
{
cart.Configuration!.LastSeen = DateTime.MaxValue;
await dbContext.SaveChangesAsync();
}
await repository.SetCartLastSeen(CartId, DateTime.MaxValue);
await SendPendingAlertsToCaller();
}

public override async Task OnDisconnectedAsync(Exception? exception)
{
var cart = await dbContext.Carts.FirstOrDefaultAsync(c => c.Id == CartId);
if (cart is not null)
{
cart.Configuration!.LastSeen = DateTime.UtcNow;
await dbContext.SaveChangesAsync();
}
await repository.SetCartLastSeen(CartId, DateTime.UtcNow);
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async Task<string> GetAccessToken()

builder.Services.AddSingleton<IAuthorizationHandler, UserAccessHandler>();
builder.Services.AddSingleton<DisplayHubManager>();
builder.Services.AddScoped<FimRepository>();
builder.Services.AddSignalR();

builder.Services.AddCors(opt => opt.AddDefaultPolicy(pol =>
Expand Down

0 comments on commit be2a589

Please sign in to comment.