Skip to content

Commit

Permalink
feat: send real time events for events
Browse files Browse the repository at this point in the history
Refs: #26
  • Loading branch information
MaSch0212 committed Jun 22, 2024
1 parent ca26fea commit 4089fe7
Show file tree
Hide file tree
Showing 22 changed files with 271 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/client/src/app/models/realtime-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type EventInstancesChangedRealtimeEvent = {
};
export type EventPreconfigurationChangedRealtimeEvent = {
eventId: string;
eventTimeslotId: string;
eventPreconfigurationId: string;
changeType: RealtimeEventChangeType;
};
Expand All @@ -31,6 +32,5 @@ export type PlayerEventChangedRealtimeEvent = {
};
export type PlayerEventRegistrationChangedRealtimeEvent = {
eventId: string;
changeType: RealtimeEventChangeType;
};
export type UserSettingsChangedRealtimeEvent = {};
4 changes: 2 additions & 2 deletions src/server/domain/Models/RealtimeEvents/RealtimeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public record EventInstancesChanged(string EventId) : IGroupRealtimeEvent
/// <summary>Event that is triggered when a preconfiguration of an event timeslot changed.</summary>
public record EventPreconfigurationChanged(
string EventId,
string EventTimeslotId,
string EventPreconfigurationId,
RealtimeEventChangeType ChangeType
) : IGroupRealtimeEvent
Expand All @@ -96,8 +97,7 @@ public record PlayerEventChanged(string EventId, RealtimeEventChangeType ChangeT
/// <summary>Event that is triggered when registrations of a player for an event changed.</summary>
public record PlayerEventRegistrationChanged(
[property: JsonIgnore] string UserId,
string EventId,
RealtimeEventChangeType ChangeType
string EventId
) : IUserRealtimeEvent
{
public static string MethodName => "PlayerEventRegistrationChanged";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MinigolfFriday.Data.Entities;
using MinigolfFriday.Domain.Models;
using MinigolfFriday.Domain.Models.Push;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Mappers;
using MinigolfFriday.Host.Services;

Expand All @@ -31,8 +32,12 @@ public CreateEventRequestValidator()
}

/// <summary>Create a new event.</summary>
public class CreateEventEndpoint(DatabaseContext databaseContext, IEventMapper eventMapper)
: Endpoint<CreateEventRequest, CreateEventResponse>
public class CreateEventEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IEventMapper eventMapper,
IIdService idService
) : Endpoint<CreateEventRequest, CreateEventResponse>
{
public override void Configure()
{
Expand All @@ -51,7 +56,14 @@ public override async Task HandleAsync(CreateEventRequest req, CancellationToken
};
databaseContext.Events.Add(entity);
await databaseContext.SaveChangesAsync(ct);

await SendAsync(new(eventMapper.Map(entity)), 201, ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventChanged(
idService.Event.Encode(entity.Id),
RealtimeEventChangeType.Created
),
ct
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using MinigolfFriday.Data;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Services;

Expand All @@ -20,8 +21,11 @@ public DeleteEventRequestValidator(IIdService idService)
}

/// <summary>Delete an event.</summary>
public class DeleteEventEndpoint(DatabaseContext databaseContext, IIdService idService)
: Endpoint<DeleteEventRequest>
public class DeleteEventEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IIdService idService
) : Endpoint<DeleteEventRequest>
{
public override void Configure()
{
Expand All @@ -35,7 +39,7 @@ public override async Task HandleAsync(DeleteEventRequest req, CancellationToken
var eventId = idService.Event.DecodeSingle(req.EventId);
var info = await databaseContext
.Events.Where(x => x.Id == eventId)
.Select(x => new { Started = x.StartedAt != null })
.Select(x => new { Started = x.StartedAt != null, x.Staged })
.FirstOrDefaultAsync(ct);

if (info == null)
Expand All @@ -54,5 +58,23 @@ public override async Task HandleAsync(DeleteEventRequest req, CancellationToken

await databaseContext.Events.Where(x => x.Id == eventId).ExecuteDeleteAsync(ct);
await SendOkAsync(ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventChanged(
idService.Event.Encode(eventId),
RealtimeEventChangeType.Deleted
),
ct
);
if (!info.Staged)
{
await realtimeEventsService.SendEventAsync(
new RealtimeEvent.PlayerEventChanged(
idService.Event.Encode(eventId),
RealtimeEventChangeType.Deleted
),
ct
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using FastEndpoints;
using FluentValidation;
using MinigolfFriday.Domain.Models;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Services;

Expand All @@ -27,6 +28,7 @@ public BuildEventInstancesRequestValidator(IIdService idService)

/// <summary>Build event instances.</summary>
public class BuildEventInstancesEndpoint(
IRealtimeEventsService realtimeEventsService,
IIdService idService,
IEventInstanceService eventInstanceService
) : Endpoint<BuildEventInstancesRequest, BuildEventInstancesResponse>
Expand Down Expand Up @@ -54,5 +56,23 @@ public override async Task HandleAsync(BuildEventInstancesRequest req, Cancellat
await eventInstanceService.PersistEventInstancesAsync(instances, ct);

await SendAsync(new(instances, persist), cancellation: ct);

if (persist)
{
await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventInstancesChanged(idService.Event.Encode(eventId)),
ct
);
if (!@event.Staged)
{
await realtimeEventsService.SendEventAsync(
new RealtimeEvent.PlayerEventChanged(
idService.Event.Encode(eventId),
RealtimeEventChangeType.Updated
),
ct
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using MinigolfFriday.Data;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Services;

Expand All @@ -28,6 +29,7 @@ public AddPlayersToPreconfigurationRequestValidator(IIdService idService)
/// <summary>Add players to an event instance preconfiguration.</summary>
public class AddPlayersToPreconfigurationEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IIdService idService
) : Endpoint<AddPlayersToPreconfigurationRequest>
{
Expand All @@ -54,7 +56,8 @@ CancellationToken ct
.Select(x => new
{
Started = x.EventTimeSlot.Event.StartedAt != null,
x.EventTimeSlot.EventId
x.EventTimeSlot.EventId,
TimeslotId = x.EventTimeSlot.Id
})
.FirstOrDefaultAsync(ct);

Expand Down Expand Up @@ -86,5 +89,15 @@ await this.SendErrorAsync(
);
await databaseContext.SaveChangesAsync(ct);
await SendAsync(null, cancellation: ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventPreconfigurationChanged(
idService.Event.Encode(preconfigInfo.EventId),
idService.EventTimeslot.Encode(preconfigInfo.TimeslotId),
idService.Preconfiguration.Encode(preconfigId),
RealtimeEventChangeType.Updated
),
ct
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using MinigolfFriday.Data;
using MinigolfFriday.Data.Entities;
using MinigolfFriday.Domain.Models;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Mappers;
using MinigolfFriday.Host.Services;
Expand All @@ -30,6 +31,7 @@ public CreatePreconfigurationRequestValidator(IIdService idService)
/// <summary>Create a new event instance preconfiguration for a given event timeslot.</summary>
public class CreatePreconfigurationEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IEventMapper eventMapper,
IIdService idService
) : Endpoint<CreatePreconfigurationRequest, CreatePreconfigurationResponse>
Expand Down Expand Up @@ -78,5 +80,15 @@ await this.SendErrorAsync(
databaseContext.EventInstancePreconfigurations.Add(preconfig);
await databaseContext.SaveChangesAsync(ct);
await SendAsync(new(eventMapper.Map(preconfig)), 201, ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventPreconfigurationChanged(
idService.Event.Encode(timeslotInfo.EventId),
idService.EventTimeslot.Encode(timeslotId),
idService.Preconfiguration.Encode(preconfig.Id),
RealtimeEventChangeType.Created
),
ct
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using MinigolfFriday.Data;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Services;

Expand All @@ -20,8 +21,11 @@ public DeletePreconfigurationRequestValidator(IIdService idService)
}

/// <summary>Delete an event instance preconfiguration.</summary>
public class DeletePreconfigurationEndpoint(DatabaseContext databaseContext, IIdService idService)
: Endpoint<DeletePreconfigurationRequest>
public class DeletePreconfigurationEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IIdService idService
) : Endpoint<DeletePreconfigurationRequest>
{
public override void Configure()
{
Expand All @@ -43,7 +47,8 @@ public override async Task HandleAsync(DeletePreconfigurationRequest req, Cancel
.Select(x => new
{
Started = x.EventTimeSlot.Event.StartedAt != null,
x.EventTimeSlot.EventId
x.EventTimeSlot.EventId,
TimeslotId = x.EventTimeSlot.Id
})
.FirstOrDefaultAsync(ct);

Expand Down Expand Up @@ -71,5 +76,15 @@ await this.SendErrorAsync(

await preconfigQuery.ExecuteDeleteAsync(ct);
await SendAsync(null, cancellation: ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventPreconfigurationChanged(
idService.Event.Encode(preconfigInfo.EventId),
idService.EventTimeslot.Encode(preconfigInfo.TimeslotId),
idService.Preconfiguration.Encode(preconfigId),
RealtimeEventChangeType.Deleted
),
ct
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using MinigolfFriday.Data;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Services;

Expand All @@ -28,6 +29,7 @@ public RemovePlayersFromPreconfigurationRequestValidator(IIdService idService)
/// <summary>Remove players from an event instance preconfiguration.</summary>
public class RemovePlayersFromPreconfigurationEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IIdService idService
) : Endpoint<RemovePlayersFromPreconfigurationRequest>
{
Expand Down Expand Up @@ -58,7 +60,8 @@ CancellationToken ct
.Select(x => new
{
Started = x.EventTimeSlot.Event.StartedAt != null,
x.EventTimeSlot.EventId
x.EventTimeSlot.EventId,
TimeslotId = x.EventTimeSlot.Id
})
.FirstOrDefaultAsync(ct);

Expand Down Expand Up @@ -90,5 +93,15 @@ await this.SendErrorAsync(
entity.Players.Remove(databaseContext.UserById(idService.User.DecodeSingle(playerId)));
await databaseContext.SaveChangesAsync(ct);
await SendAsync(null, cancellation: ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventPreconfigurationChanged(
idService.Event.Encode(preconfigInfo.EventId),
idService.EventTimeslot.Encode(preconfigInfo.TimeslotId),
idService.Preconfiguration.Encode(preconfigId),
RealtimeEventChangeType.Updated
),
ct
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.EntityFrameworkCore;
using MinigolfFriday.Data;
using MinigolfFriday.Domain.Models.Push;
using MinigolfFriday.Domain.Models.RealtimeEvents;
using MinigolfFriday.Host.Common;
using MinigolfFriday.Host.Mappers;
using MinigolfFriday.Host.Services;
Expand All @@ -24,6 +25,7 @@ public StartEventRequestValidator(IIdService idService)
/// <summary>Starts an event.</summary>
public class StartEventEndpoint(
DatabaseContext databaseContext,
IRealtimeEventsService realtimeEventsService,
IIdService idService,
IUserPushSubscriptionMapper userPushSubscriptionMapper,
IWebPushService webPushService
Expand Down Expand Up @@ -106,6 +108,22 @@ await this.SendErrorAsync(
await databaseContext
.Events.Where(x => x.Id == eventId)
.ExecuteUpdateAsync(x => x.SetProperty(x => x.StartedAt, now), ct);
await SendAsync(null, cancellation: ct);

await realtimeEventsService.SendEventAsync(
new RealtimeEvent.EventChanged(
idService.Event.Encode(eventId),
RealtimeEventChangeType.Updated
),
ct
);
await realtimeEventsService.SendEventAsync(
new RealtimeEvent.PlayerEventChanged(
idService.Event.Encode(eventId),
RealtimeEventChangeType.Updated
),
ct
);

var pushSubscription = await databaseContext
.Events.Where(x => x.Id == eventId)
Expand All @@ -124,7 +142,5 @@ await webPushService.SendAsync(
new PushNotificationData.EventStarted(idService.Event.Encode(eventId)),
ct
);

await SendAsync(null, cancellation: ct);
}
}
Loading

0 comments on commit 4089fe7

Please sign in to comment.