From 874146a1c5eab9b4eeda021af3fc438133f73f5e Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Tue, 28 May 2024 15:37:03 +0200 Subject: [PATCH 1/4] data now sent from server upon modifications from client instead of being requested --- api/Core/Services/ConditionsLogsService.cs | 6 ++++ api/Events/Auth/Client/ClientWantsToLogIn.cs | 32 ++++++++++++++++++- .../Client/ClientWantsToCreateCollection.cs | 2 +- .../Client/ClientWantsToUpdateCollection.cs | 2 +- .../Client/ClientWantsToCreatePlant.cs | 6 ++++ .../Client/ClientWantsToDeletePlant.cs | 6 ++++ .../Client/ClientWantsToUpdatePlant.cs | 6 ++++ 7 files changed, 57 insertions(+), 3 deletions(-) diff --git a/api/Core/Services/ConditionsLogsService.cs b/api/Core/Services/ConditionsLogsService.cs index 9f60247..d7df5e7 100644 --- a/api/Core/Services/ConditionsLogsService.cs +++ b/api/Core/Services/ConditionsLogsService.cs @@ -1,4 +1,5 @@ using api.Events.Conditions.Server; +using api.Events.PlantEvents.Server; using api.Extensions; using Infrastructure.Repositories; using Shared.Dtos; @@ -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) { diff --git a/api/Events/Auth/Client/ClientWantsToLogIn.cs b/api/Events/Auth/Client/ClientWantsToLogIn.cs index a4ff964..dc07865 100644 --- a/api/Events/Auth/Client/ClientWantsToLogIn.cs +++ b/api/Events/Auth/Client/ClientWantsToLogIn.cs @@ -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; @@ -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 { public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnection socket) @@ -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 @@ -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 + }); } } diff --git a/api/Events/Collections/Client/ClientWantsToCreateCollection.cs b/api/Events/Collections/Client/ClientWantsToCreateCollection.cs index 4553efd..4ba1c15 100644 --- a/api/Events/Collections/Client/ClientWantsToCreateCollection.cs +++ b/api/Events/Collections/Client/ClientWantsToCreateCollection.cs @@ -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() }); diff --git a/api/Events/Collections/Client/ClientWantsToUpdateCollection.cs b/api/Events/Collections/Client/ClientWantsToUpdateCollection.cs index 4c5b632..52d25ef 100644 --- a/api/Events/Collections/Client/ClientWantsToUpdateCollection.cs +++ b/api/Events/Collections/Client/ClientWantsToUpdateCollection.cs @@ -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() { diff --git a/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs index ff059e9..78cbf3d 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs @@ -22,6 +22,7 @@ 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 { @@ -29,6 +30,11 @@ public override async Task Handle(ClientWantsToCreatePlantDto dto, IWebSocketCon }; socket.SendDto(serverCreatesNewPlant); + socket.SendDto(new ServerSendsPlants + { + Plants = allPlants + }); + var stats = await statsService.GetStats(email); socket.SendDto(new ServerSendsStats{Stats = stats}); } diff --git a/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs index 76319a3..d98a982 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs @@ -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}); } } diff --git a/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs index 1dc8348..1dd2c8f 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs @@ -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}); } } \ No newline at end of file From 91c04d0032d2cac70e301d2a9290f789fc9e572e Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Tue, 28 May 2024 16:17:37 +0200 Subject: [PATCH 2/4] added check for existing plant with device id to create and update plant --- .../Repositories/PlantRepository.cs | 7 ++++ api/Core/Services/PlantService.cs | 32 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Infrastructure/Repositories/PlantRepository.cs b/Infrastructure/Repositories/PlantRepository.cs index 0727ce1..f1fc8d6 100644 --- a/Infrastructure/Repositories/PlantRepository.cs +++ b/Infrastructure/Repositories/PlantRepository.cs @@ -11,9 +11,16 @@ public class PlantRepository(IDbContextFactory dbContextFa public async Task CreatePlant(Plant plant) { await using var context = await dbContextFactory.CreateDbContextAsync(); + await context.Plants.AddAsync(plant); await context.SaveChangesAsync(); } + + public async Task DoesDeviceIdExist(string deviceId) + { + await using var context = await dbContextFactory.CreateDbContextAsync(); + return await context.Plants.AnyAsync(p => p.DeviceId == deviceId); + } public async Task GetPlantById(Guid id) { diff --git a/api/Core/Services/PlantService.cs b/api/Core/Services/PlantService.cs index 3922459..e395a54 100644 --- a/api/Core/Services/PlantService.cs +++ b/api/Core/Services/PlantService.cs @@ -1,5 +1,7 @@ using api.Core.Options; using api.Core.Services.External.BlobStorage; +using api.Events.Global; +using api.Extensions; using Infrastructure.Repositories; using Microsoft.Extensions.Options; using Shared.Dtos.FromClient.Plant; @@ -13,7 +15,7 @@ public class PlantService( PlantRepository plantRepository, RequirementService requirementService, IBlobStorageService blobStorageService, - IOptions azureBlobStorageOptions) + IOptions azureBlobStorageOptions, WebSocketConnectionService webSocketConnectionService) { public async Task CreatePlant(CreatePlantDto createPlantDto, string loggedInUser) { @@ -28,6 +30,16 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge ímageUrl = await blobStorageService.SaveImageToBlobStorage(createPlantDto.Base64Image, loggedInUser, true); } + + if (!String.IsNullOrEmpty(createPlantDto.DeviceId)) + { + var deviceExists = await plantRepository.DoesDeviceIdExist(createPlantDto.DeviceId); + HandleExistingDeviceId(loggedInUser); + if (deviceExists) + { + createPlantDto.DeviceId = null; + } + } // Insert plant first to get the plantId var plant = new Plant { @@ -40,6 +52,8 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge LatestChange = DateTime.UtcNow }; + + await plantRepository.CreatePlant(plant); // Create requirements for the plant to crete a link between the two @@ -50,6 +64,12 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge return plant; } + private void HandleExistingDeviceId(string loggedInUser) + { + var connection = webSocketConnectionService.GetConnectionByEmail(loggedInUser); + connection?.SendDto(new ServerSendsErrorMessage{Error = "Device ID already in use"}); + } + public async Task GetPlantById(Guid id, string requesterEmail) { var plant = await VerifyPlantExistsAndUserHasAccess(id, requesterEmail); @@ -89,6 +109,16 @@ public async Task UpdatePlant(UpdatePlantDto updatePlantDto, string reque imageUrl = await blobStorageService.SaveImageToBlobStorage(updatePlantDto.Base64Image, requesterEmail, true, plant.ImageUrl); } + if (!String.IsNullOrEmpty(updatePlantDto.DeviceId)) + { + var deviceExists = await plantRepository.DoesDeviceIdExist(updatePlantDto.DeviceId); + HandleExistingDeviceId(requesterEmail); + if (deviceExists) + { + updatePlantDto.DeviceId = null; + } + } + // Update the plant plant = new Plant { From fe71f75ef9b4b6f3fa518b46dc73211173a24d65 Mon Sep 17 00:00:00 2001 From: Julia Ilasova <1julka1il@gmail.com> Date: Wed, 29 May 2024 09:48:25 +0200 Subject: [PATCH 3/4] reshuffle some method calls --- .../Collections/Client/ClientWantsToCreateCollection.cs | 2 +- api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs | 2 +- api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs | 4 ++-- api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs | 6 +++--- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/api/Events/Collections/Client/ClientWantsToCreateCollection.cs b/api/Events/Collections/Client/ClientWantsToCreateCollection.cs index 4ba1c15..628461a 100644 --- a/api/Events/Collections/Client/ClientWantsToCreateCollection.cs +++ b/api/Events/Collections/Client/ClientWantsToCreateCollection.cs @@ -21,13 +21,13 @@ public override async Task Handle(ClientWantsToCreateCollectionDto dto, IWebSock 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() }); + var stats = await statsService.GetStats(email); socket.SendDto(new ServerSendsStats{Stats = stats}); } } \ No newline at end of file diff --git a/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs index 78cbf3d..b69bb98 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs @@ -22,7 +22,6 @@ 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 { @@ -30,6 +29,7 @@ public override async Task Handle(ClientWantsToCreatePlantDto dto, IWebSocketCon }; socket.SendDto(serverCreatesNewPlant); + var allPlants = await plantService.GetPlantsForUser(email, 1, 100); socket.SendDto(new ServerSendsPlants { Plants = allPlants diff --git a/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs index d98a982..9c3255d 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToDeletePlant.cs @@ -22,17 +22,17 @@ public class ClientWantsToDeletePlant(PlantService plantService, JwtService jwtS public override async Task Handle(ClientWantsToDeletePlantDto dto, IWebSocketConnection socket) { 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()); + var allPlants = await plantService.GetPlantsForUser(email, 1, 100); socket.SendDto(new ServerSendsPlants { Plants = allPlants }); + var stats = await statsService.GetStats(email); socket.SendDto(new ServerSendsStats{Stats = stats}); } } diff --git a/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs b/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs index 1dd2c8f..84757ff 100644 --- a/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs +++ b/api/Events/PlantEvents/Client/ClientWantsToUpdatePlant.cs @@ -19,20 +19,20 @@ public class ClientWantsToUpdatePlant(PlantService plantService, JwtService jwtS 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); - var allPlants = await plantService.GetPlantsForUser(email, 1, 100); + var plant = await plantService.UpdatePlant(dto.UpdatePlantDto, email); socket.SendDto(new ServerSavesPlant { Plant = plant }); + var allPlants = await plantService.GetPlantsForUser(email, 1, 100); socket.SendDto(new ServerSendsPlants { Plants = allPlants }); + var stats = await statsService.GetStats(email); socket.SendDto(new ServerSendsStats{Stats = stats}); } } \ No newline at end of file From cabeb4e6fe44de343d23ce9ef87d1852486d71d6 Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Wed, 29 May 2024 10:04:15 +0200 Subject: [PATCH 4/4] send initial data on authentication added check that device id is not already attached to plant upon update --- api/Core/Services/PlantService.cs | 8 ++- .../Client/ClientWantsToCheckJwtValidity.cs | 15 +---- api/Events/Auth/Client/ClientWantsToLogIn.cs | 63 +------------------ api/Events/Auth/Client/InitialDataHelper.cs | 63 +++++++++++++++++++ .../AddServicesAndRepositoriesExtension.cs | 4 ++ 5 files changed, 78 insertions(+), 75 deletions(-) create mode 100644 api/Events/Auth/Client/InitialDataHelper.cs diff --git a/api/Core/Services/PlantService.cs b/api/Core/Services/PlantService.cs index e395a54..2902341 100644 --- a/api/Core/Services/PlantService.cs +++ b/api/Core/Services/PlantService.cs @@ -112,10 +112,14 @@ public async Task UpdatePlant(UpdatePlantDto updatePlantDto, string reque if (!String.IsNullOrEmpty(updatePlantDto.DeviceId)) { var deviceExists = await plantRepository.DoesDeviceIdExist(updatePlantDto.DeviceId); - HandleExistingDeviceId(requesterEmail); if (deviceExists) { - updatePlantDto.DeviceId = null; + var plantWithDeviceId = await plantRepository.GetPlantIdByDeviceIdAsync(updatePlantDto.DeviceId); + if (!plantWithDeviceId.ToString().Equals(updatePlantDto.DeviceId)) + { + HandleExistingDeviceId(requesterEmail); + updatePlantDto.DeviceId = null; + } } } diff --git a/api/Events/Auth/Client/ClientWantsToCheckJwtValidity.cs b/api/Events/Auth/Client/ClientWantsToCheckJwtValidity.cs index d50a3f1..ef779a8 100644 --- a/api/Events/Auth/Client/ClientWantsToCheckJwtValidity.cs +++ b/api/Events/Auth/Client/ClientWantsToCheckJwtValidity.cs @@ -1,6 +1,3 @@ -using api.Core.Services; -using api.Events.Auth.Server; -using api.Extensions; using Fleck; using lib; using Shared.Models; @@ -14,16 +11,10 @@ public class ClientWantsToCheckJwtValidityDto : BaseDtoWithJwt; /// If the token is not valid, an exception will be thrown, and the GlobalExceptionHandler will catch it, and send a /// corresponding message to the client. /// -public class ClientWantsToCheckJwtValidity(WebSocketConnectionService webSocketConnectionService, JwtService jwtService) : BaseEventHandler +public class ClientWantsToCheckJwtValidity(InitialDataHelper initialDataHelper) : BaseEventHandler { - public override Task Handle(ClientWantsToCheckJwtValidityDto dto, IWebSocketConnection socket) + public override async Task Handle(ClientWantsToCheckJwtValidityDto dto, IWebSocketConnection socket) { - var email = jwtService.GetEmailFromJwt(dto.Jwt); - webSocketConnectionService.UpdateConnectionEmail(socket, email); - socket.SendDto(new ServerAuthenticatesUser - { - Jwt = dto.Jwt, - }); - return Task.CompletedTask; + await initialDataHelper.SendInitialData(socket, dto.Jwt!); } } \ No newline at end of file diff --git a/api/Events/Auth/Client/ClientWantsToLogIn.cs b/api/Events/Auth/Client/ClientWantsToLogIn.cs index dc07865..f652c38 100644 --- a/api/Events/Auth/Client/ClientWantsToLogIn.cs +++ b/api/Events/Auth/Client/ClientWantsToLogIn.cs @@ -1,14 +1,6 @@ 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; -using Shared.Dtos; using Shared.Dtos.FromClient.Identity; using Shared.Exceptions; @@ -19,7 +11,7 @@ public class ClientWantsToLogInDto : BaseDto public LoginDto LoginDto { get; set; } = null!; } -public class ClientWantsToLogIn(WebSocketConnectionService webSocketConnectionService, UserService userService, IBlobStorageService blobStorageService, PlantService plantService, CollectionsService collectionsService, StatsService statsService) +public class ClientWantsToLogIn(UserService userService, InitialDataHelper initialDataHelper) : BaseEventHandler { public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnection socket) @@ -27,58 +19,7 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio var jwt = await userService.Login(dto.LoginDto); if (jwt == null) throw new InvalidCredentialsException(); - 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 - { - UserEmail = user.UserEmail, - Username = user.UserName, - }; - - if (!string.IsNullOrEmpty(user.BlobUrl)) - { - getUserDto.BlobUrl = blobStorageService.GenerateSasUri(user.BlobUrl, false); - } - - socket.SendDto(new ServerAuthenticatesUser - { - Jwt = jwt, - - }); - - socket.SendDto(new ServerSendsUserInfo - { - 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 - }); + await initialDataHelper.SendInitialData(socket, jwt); } } diff --git a/api/Events/Auth/Client/InitialDataHelper.cs b/api/Events/Auth/Client/InitialDataHelper.cs new file mode 100644 index 0000000..e78b41f --- /dev/null +++ b/api/Events/Auth/Client/InitialDataHelper.cs @@ -0,0 +1,63 @@ +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 Shared.Dtos; + +namespace api.Events.Auth.Client; + +public class InitialDataHelper(PlantService plantService, IBlobStorageService blobStorageService, CollectionsService collectionsService, StatsService statsService, UserService userService, WebSocketConnectionService webSocketConnectionService, JwtService jwtService) +{ + + public async Task SendInitialData(IWebSocketConnection socket, string jwt) + { + var email = jwtService.GetEmailFromJwt(jwt); + webSocketConnectionService.UpdateConnectionEmail(socket, email); + + var user = await userService.GetUserByEmail(email); + + socket.SendDto(new ServerAuthenticatesUser + { + Jwt = jwt, + }); + + var criticalPlants = await plantService.GetCriticalPlants(user.UserEmail); + socket.SendDto(new ServerSendsCriticalPlants + { + Plants = criticalPlants + }); + + var getUserDto = new GetUserDto + { + UserEmail = user.UserEmail, + Username = user.UserName, + }; + + if (!string.IsNullOrEmpty(user.BlobUrl)) + { + getUserDto.BlobUrl = blobStorageService.GenerateSasUri(user.BlobUrl, false); + } + + socket.SendDto(new ServerSendsUserInfo + { + GetUserDto = getUserDto + }); + + var allCollections = await collectionsService.GetCollectionsForUser(user.UserEmail); + socket.SendDto(new ServerSendsAllCollections + { + Collections = allCollections.ToList() + }); + + var stats = await statsService.GetStats(user.UserEmail); + socket.SendDto(new ServerSendsStats + { + Stats = stats + }); + } +} \ No newline at end of file diff --git a/api/Extensions/AddServicesAndRepositoriesExtension.cs b/api/Extensions/AddServicesAndRepositoriesExtension.cs index 3206523..116f95c 100644 --- a/api/Extensions/AddServicesAndRepositoriesExtension.cs +++ b/api/Extensions/AddServicesAndRepositoriesExtension.cs @@ -1,6 +1,7 @@ using api.Core.Services; using api.Core.Services.External.BackgroundRemoval; using api.Core.Services.External.BlobStorage; +using api.Events.Auth.Client; using Infrastructure.Repositories; namespace api.Extensions; @@ -28,6 +29,9 @@ public static void AddServicesAndRepositories(this IServiceCollection services) services.AddSingleton(); services.AddSingleton(); + // Helpers + services.AddSingleton(); + // External services if (EnvironmentHelper.IsTesting()) {