Skip to content

Commit

Permalink
Created statsservice to handle updating stats when collection or plan…
Browse files Browse the repository at this point in the history
…t is added, updated or deleted

Upon logging out we no longer remove the entire connection, just the email, the connection is removed on onclose
  • Loading branch information
mariaruth1 committed May 28, 2024
1 parent c569b77 commit da1c116
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 55 deletions.
8 changes: 8 additions & 0 deletions Shared/Models/Stats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Shared.Models;

public class Stats
{
public int TotalPlants { get; set; }
public int HappyPlants { get; set; }
public int Collections { get; set; }
}
21 changes: 21 additions & 0 deletions api/Core/Services/StatsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Shared.Dtos;
using Shared.Models;

namespace api.Core.Services;

public class StatsService(CollectionsService collectionsService, PlantService plantService)
{
public async Task<Stats> GetStats(string email)
{
var totalPlants = await plantService.GetTotalPlantsCount(email);
var happyPlants = await plantService.GetHappyPlantsCount(email);
var collections = await collectionsService.GetTotalCollectionsCount(email);

return new Stats
{
TotalPlants = totalPlants,
HappyPlants = happyPlants,
Collections = collections
};
}
}
2 changes: 1 addition & 1 deletion api/Events/Auth/Client/ClientWantsToLogOut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ClientWantsToLogOut(WebSocketConnectionService connectionService)
{
public override Task Handle(ClientWantsToLogOutDto dto, IWebSocketConnection socket)
{
connectionService.RemoveConnection(socket);
connectionService.RemoveEmailFromConnection(socket);
socket.SendDto(new ServerLogsOutUser());
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Core.Services;
using api.Events.Collections.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -13,16 +14,20 @@ public class ClientWantsToCreateCollectionDto : BaseDtoWithJwt
public required CreateCollectionDto CreateCollectionDto { get; set; }
}

public class ClientWantsToCreateCollection(CollectionsService collectionsService, JwtService jwtService) : BaseEventHandler<ClientWantsToCreateCollectionDto>
public class ClientWantsToCreateCollection(CollectionsService collectionsService, JwtService jwtService, StatsService statsService) : BaseEventHandler<ClientWantsToCreateCollectionDto>
{
public override async Task Handle(ClientWantsToCreateCollectionDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
await collectionsService.CreateCollection(dto.CreateCollectionDto, email);
var allCollections = await collectionsService.GetCollectionsForUser(email);
var stats = await statsService.GetStats(email);

socket.SendDto(new ServerSendsAllCollections()
{
Collections = allCollections.ToList()
});

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Core.Services;
using api.Events.Collections.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -13,16 +14,20 @@ public class ClientWantsToDeleteCollectionDto : BaseDtoWithJwt
public Guid CollectionId { get; set; }
}

public class ClientWantsToDeleteCollection(CollectionsService collectionsService, JwtService jwtService) : BaseEventHandler<ClientWantsToDeleteCollectionDto>
public class ClientWantsToDeleteCollection(CollectionsService collectionsService, JwtService jwtService, StatsService statsService) : BaseEventHandler<ClientWantsToDeleteCollectionDto>
{
public override async Task Handle(ClientWantsToDeleteCollectionDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
await collectionsService.DeleteCollection(dto.CollectionId, email);
var allCollections = await collectionsService.GetCollectionsForUser(email);

var stats = await statsService.GetStats(email);
socket.SendDto(new ServerSendsAllCollections()
{
Collections = allCollections.ToList()
});

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
6 changes: 5 additions & 1 deletion api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using api.Core.Services;
using api.EventFilters;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -15,18 +16,21 @@ public class ClientWantsToCreatePlantDto: BaseDtoWithJwt
}

[ValidateDataAnnotations]
public class ClientWantsToCreatePlant(PlantService plantService, JwtService jwtService): BaseEventHandler<ClientWantsToCreatePlantDto>
public class ClientWantsToCreatePlant(PlantService plantService, JwtService jwtService, StatsService statsService): BaseEventHandler<ClientWantsToCreatePlantDto>
{
public override async Task Handle(ClientWantsToCreatePlantDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plant = await plantService.CreatePlant(dto.CreatePlantDto, email);
var stats = await statsService.GetStats(email);

var serverCreatesNewPlant = new ServerSavesPlant
{
Plant = plant
};

socket.SendDto(serverCreatesNewPlant);

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
7 changes: 6 additions & 1 deletion api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using api.EventFilters;
using api.Events.Global;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -16,13 +17,17 @@ public class ClientWantsToDeletePlantDto: BaseDtoWithJwt
}

[ValidateDataAnnotations]
public class ClientWantsToDeletePlant(PlantService plantService, JwtService jwtService): BaseEventHandler<ClientWantsToDeletePlantDto>
public class ClientWantsToDeletePlant(PlantService plantService, JwtService jwtService, StatsService statsService): BaseEventHandler<ClientWantsToDeletePlantDto>
{
public override async Task Handle(ClientWantsToDeletePlantDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var stats = await statsService.GetStats(email);

await plantService.DeletePlant(dto.PlantId, email);
socket.SendDto( new ServerConfirmsDelete());

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Core.Services;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -9,16 +10,20 @@ namespace api.Events.PlantEvents.Client;

public class ClientWantsToGetCriticalPlantsDto : BaseDtoWithJwt;

public class ClientWantsToGetCriticalPlants(JwtService jwtService, PlantService plantService) : BaseEventHandler<ClientWantsToGetCriticalPlantsDto>
public class ClientWantsToGetCriticalPlants(JwtService jwtService, PlantService plantService, StatsService statsService) : BaseEventHandler<ClientWantsToGetCriticalPlantsDto>
{
public override async Task Handle(ClientWantsToGetCriticalPlantsDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plants = await plantService.GetCriticalPlants(email);
var stats = await statsService.GetStats(email);

var serverResponse = new ServerSendsCriticalPlants
{
Plants = plants
};
socket.SendDto(serverResponse);

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
7 changes: 6 additions & 1 deletion api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using api.Core.Services;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -13,15 +14,19 @@ public class ClientWantsToUpdatePlantDto: BaseDtoWithJwt
public required UpdatePlantDto UpdatePlantDto { get; set; }
}

public class ClientWantsToUpdatePlant(PlantService plantService, JwtService jwtService): BaseEventHandler<ClientWantsToUpdatePlantDto>
public class ClientWantsToUpdatePlant(PlantService plantService, JwtService jwtService, StatsService statsService): BaseEventHandler<ClientWantsToUpdatePlantDto>
{
public override async Task Handle(ClientWantsToUpdatePlantDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plant = await plantService.UpdatePlant(dto.UpdatePlantDto, email);
var stats = await statsService.GetStats(email);

socket.SendDto(new ServerSavesPlant
{
Plant = plant
});

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
28 changes: 28 additions & 0 deletions api/Events/Statistics/ClientWantsStats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using api.Core.Services;
using api.Extensions;
using Fleck;
using lib;
using Shared.Models;

namespace api.Events.Statistics;

public class ClientWantsStatsDto : BaseDtoWithJwt
{

}

public class ClientWantsStats(StatsService statsService, JwtService jwtService) : BaseEventHandler<ClientWantsStatsDto>
{
public override async Task Handle(ClientWantsStatsDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);

var stats = await statsService.GetStats(email);
socket.SendDto(new ServerSendsStats{Stats = stats});
}
}

public class ServerSendsStats : BaseDto
{
public Stats Stats { get; set; } = null!;
}
48 changes: 0 additions & 48 deletions api/Events/Stats/ClientWantsStats.cs

This file was deleted.

6 changes: 6 additions & 0 deletions api/WebSocketConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public void UpdateConnectionEmail(IWebSocketConnection connection, string email)
var clientId = connection.ConnectionInfo.Id;
_connectedClients[clientId].Email = email;
}

public void RemoveEmailFromConnection(IWebSocketConnection connection)
{
var clientId = connection.ConnectionInfo.Id;
_connectedClients[clientId].Email = null;
}

public void RemoveConnection(IWebSocketConnection connection)
{
Expand Down

0 comments on commit da1c116

Please sign in to comment.