Skip to content

Commit

Permalink
does it work?
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed May 28, 2024
1 parent 15d472a commit 80c9195
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
6 changes: 1 addition & 5 deletions Tests/PlantTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using api.Events.PlantEvents.Client;
using api.Events.PlantEvents.Server;
using api.Events.Statistics;
using lib;
using Shared.Dtos.FromClient.Plant;
using Shared.Dtos.FromClient.Requirements;
Expand All @@ -18,13 +17,10 @@ public async Task CreatePlant()
var createPlantDto = GenerateRandomCreatePlantDto();

var webSocketTestClient = await new WebSocketTestClient().ConnectAsync();
webSocketTestClient.SubscribeToErrors();

await webSocketTestClient.DoAndAssert(new ClientWantsToCreatePlantDto { CreatePlantDto = createPlantDto, Jwt = jwt }, receivedMessages =>
{
receivedMessages.ForEach(e => Console.WriteLine(e.eventType));
return receivedMessages.Count(e => e.eventType == nameof(ServerSavesPlant)) == 1 &&
receivedMessages.Count(e => e.eventType == nameof(ServerSendsStats)) == 1;
return receivedMessages.Count(e => e.eventType == nameof(ServerSavesPlant)) == 1;
});
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ await webSocketTestClient.DoAndAssert(new ClientWantsToSignUpDto { RegisterUserD

var jwtSubscription = webSocketTestClient.Client.MessageReceived.Subscribe(msg =>
{
var eventType = JsonSerializer.Deserialize<BaseDto>(msg.Text).eventType;
if (eventType != nameof(ServerAuthenticatesUser)) return;
var serverAuthenticates = JsonSerializer.Deserialize<ServerAuthenticatesUser>(msg.Text, options: new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
jwt = serverAuthenticates.Jwt;
jwt = serverAuthenticates!.Jwt;
});
webSocketTestClient.Send(new ClientWantsToLogInDto { LoginDto = loginDto });

Expand Down
1 change: 1 addition & 0 deletions api/Events/Auth/Client/ClientWantsToLogIn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio
Jwt = jwt,

});

socket.SendDto(new ServerSendsUserInfo
{
GetUserDto = getUserDto
Expand Down
13 changes: 5 additions & 8 deletions api/Events/PlantEvents/Client/ClientWantsToCreatePlant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace api.Events.PlantEvents.Client;

public class ClientWantsToCreatePlantDto: BaseDtoWithJwt
{
public CreatePlantDto CreatePlantDto { get; set; }
public required CreatePlantDto CreatePlantDto { get; set; }
}

[ValidateDataAnnotations]
Expand All @@ -21,18 +21,15 @@ public class ClientWantsToCreatePlant(PlantService plantService, JwtService jwtS
public override async Task Handle(ClientWantsToCreatePlantDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);
var plant = plantService.CreatePlant(dto.CreatePlantDto, email);
var stats = statsService.GetStats(email);

await Task.WhenAll(plant, stats);
var plant = await plantService.CreatePlant(dto.CreatePlantDto, email);

var serverCreatesNewPlant = new ServerSavesPlant
{
Plant = plant.Result
Plant = plant
};

socket.SendDto(serverCreatesNewPlant);

socket.SendDto(new ServerSendsStats{Stats = stats.Result});
var stats = await statsService.GetStats(email);
socket.SendDto(new ServerSendsStats{Stats = stats});
}
}
6 changes: 4 additions & 2 deletions api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public static class Startup
nameof(ClientWantsToSignUp)
];

private static readonly List<string?> NonProdEnvironments = ["Development", "Testing"];

public static async Task Main(string[] args)
{
var app = await StartApi(args);
Expand Down Expand Up @@ -86,15 +88,15 @@ public static async Task<WebApplication> StartApi(string[] args)
var scope = app.Services.CreateScope();
var db = await app.Services.GetRequiredService<IDbContextFactory<ApplicationDbContext>>().CreateDbContextAsync();

if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
if (NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")))
{
await db.Database.EnsureDeletedAsync();
}

await db.Database.EnsureCreatedAsync();
await db.Database.MigrateAsync();

if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
if (NonProdEnvironments.Contains(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")))
{
await db.SeedDevelopmentDataAsync(scope, app.Configuration["AzureBlob:DefaultPlantImageUrl"] ?? "https://example.com");
}
Expand Down
Binary file removed api/logo.png
Binary file not shown.

0 comments on commit 80c9195

Please sign in to comment.