Skip to content

Commit

Permalink
data now sent from server upon modifications from client instead of b…
Browse files Browse the repository at this point in the history
…eing requested
  • Loading branch information
mariaruth1 committed May 28, 2024
1 parent 8bd1d9b commit 874146a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 3 deletions.
6 changes: 6 additions & 0 deletions api/Core/Services/ConditionsLogsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using api.Events.Conditions.Server;
using api.Events.PlantEvents.Server;
using api.Extensions;
using Infrastructure.Repositories;
using Shared.Dtos;
Expand Down Expand Up @@ -45,6 +46,11 @@ public async Task CreateConditionsLogAsync(CreateConditionsLogDto createConditio
{
ConditionsLog = addedLog
});
var allPlants = await plantService.GetPlantsForUser(email, 1, 100);
connection?.SendDto(new ServerSendsPlants
{
Plants = allPlants
});

if (newMood != recentMood)
{
Expand Down
32 changes: 31 additions & 1 deletion api/Events/Auth/Client/ClientWantsToLogIn.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using api.Core.Services;
using api.Core.Services.External.BlobStorage;
using api.Events.Auth.Server;
using api.Events.Collections.Server;
using api.Events.Global;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using api.Extensions;
using Fleck;
using lib;
Expand All @@ -16,7 +19,7 @@ public class ClientWantsToLogInDto : BaseDto
public LoginDto LoginDto { get; set; } = null!;
}

public class ClientWantsToLogIn(WebSocketConnectionService webSocketConnectionService, UserService userService, IBlobStorageService blobStorageService)
public class ClientWantsToLogIn(WebSocketConnectionService webSocketConnectionService, UserService userService, IBlobStorageService blobStorageService, PlantService plantService, CollectionsService collectionsService, StatsService statsService)
: BaseEventHandler<ClientWantsToLogInDto>
{
public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnection socket)
Expand All @@ -26,6 +29,13 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio

var user = await userService.GetUserByEmail(dto.LoginDto.Email);

var criticalPlants = plantService.GetCriticalPlants(user.UserEmail);
var allCollections = collectionsService.GetCollectionsForUser(user.UserEmail);
var allPlants = plantService.GetPlantsForUser(user.UserEmail, 1, 100);
var stats = statsService.GetStats(user.UserEmail);

await Task.WhenAll(criticalPlants, allCollections, allPlants, stats);

webSocketConnectionService.UpdateConnectionEmail(socket, dto.LoginDto.Email);

var getUserDto = new GetUserDto
Expand All @@ -49,6 +59,26 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio
{
GetUserDto = getUserDto
});

socket.SendDto(new ServerSendsCriticalPlants
{
Plants = criticalPlants.Result
});

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

socket.SendDto(new ServerSendsPlants
{
Plants = allPlants.Result
});

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

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override async Task Handle(ClientWantsToCreateCollectionDto dto, IWebSock
var allCollections = await collectionsService.GetCollectionsForUser(email);
var stats = await statsService.GetStats(email);

socket.SendDto(new ServerSendsAllCollections()
socket.SendDto(new ServerSendsAllCollections
{
Collections = allCollections.ToList()
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ClientWantsToUpdateCollection(CollectionsService collectionsService
public override async Task Handle(ClientWantsToUpdateCollectionDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var collection = await collectionsService.UpdateCollection(dto.UpdateCollectionDto, email);
await collectionsService.UpdateCollection(dto.UpdateCollectionDto, email);
var allCollections = await collectionsService.GetCollectionsForUser(email);
socket.SendDto(new ServerSendsAllCollections()
{
Expand Down
6 changes: 6 additions & 0 deletions api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ public override async Task Handle(ClientWantsToCreatePlantDto dto, IWebSocketCon
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plant = await plantService.CreatePlant(dto.CreatePlantDto, email);
var allPlants = await plantService.GetPlantsForUser(email, 1, 100);

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

socket.SendDto(new ServerSendsPlants
{
Plants = allPlants
});

var stats = await statsService.GetStats(email);
socket.SendDto(new ServerSendsStats{Stats = stats});
}
Expand Down
6 changes: 6 additions & 0 deletions api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ public override async Task Handle(ClientWantsToDeletePlantDto dto, IWebSocketCon
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var stats = await statsService.GetStats(email);
var allPlants = await plantService.GetPlantsForUser(email, 1, 100);

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

socket.SendDto(new ServerSendsPlants
{
Plants = allPlants
});

socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
Expand Down
6 changes: 6 additions & 0 deletions api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@ public override async Task Handle(ClientWantsToUpdatePlantDto dto, IWebSocketCon
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plant = await plantService.UpdatePlant(dto.UpdatePlantDto, email);
var stats = await statsService.GetStats(email);
var allPlants = await plantService.GetPlantsForUser(email, 1, 100);

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

socket.SendDto(new ServerSendsPlants
{
Plants = allPlants
});

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

0 comments on commit 874146a

Please sign in to comment.