-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from Team-Wilhelm/main
data refresh update
- Loading branch information
Showing
36 changed files
with
659 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,172 +54,4 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) | |
|
||
base.OnModelCreating(modelBuilder); | ||
} | ||
|
||
public async Task SeedDevelopmentDataAsync(IServiceScope scope, string defaultPlantImage) | ||
{ | ||
var userRepository = scope.ServiceProvider.GetRequiredService<UserRepository>(); | ||
await userRepository.CreateUser(new RegisterUserDto | ||
{ | ||
Email = "[email protected]", | ||
Password = "password", | ||
Username = "bob" | ||
}); | ||
|
||
var collectionsRepository = scope.ServiceProvider.GetRequiredService<CollectionsRepository>(); | ||
var collection1 = await collectionsRepository.CreateCollection( | ||
new Collection | ||
{ | ||
CollectionId = Guid.NewGuid(), | ||
Name = "Succulents", | ||
UserEmail = "[email protected]", | ||
} | ||
); | ||
var collection2 = await collectionsRepository.CreateCollection( | ||
new Collection | ||
{ | ||
CollectionId = Guid.NewGuid(), | ||
Name = "Cacti", | ||
UserEmail = "[email protected]", | ||
} | ||
); | ||
|
||
var plantRepository = scope.ServiceProvider.GetRequiredService<PlantRepository>(); | ||
await plantRepository.CreatePlant( | ||
new Plant | ||
{ | ||
PlantId = Guid.NewGuid(), | ||
Nickname = "Aloe Vera", | ||
UserEmail = "[email protected]", | ||
ImageUrl = defaultPlantImage, | ||
CollectionId = collection1.CollectionId, | ||
LatestChange = DateTime.UtcNow.Subtract(TimeSpan.FromDays(5)) | ||
} | ||
); | ||
|
||
await plantRepository.CreatePlant( | ||
new Plant | ||
{ | ||
PlantId = Guid.NewGuid(), | ||
Nickname = "Prickly Pear", | ||
UserEmail = "[email protected]", | ||
ImageUrl = defaultPlantImage, | ||
CollectionId = collection2.CollectionId, | ||
LatestChange = DateTime.UtcNow.Subtract(TimeSpan.FromDays(7)) | ||
} | ||
); | ||
|
||
await plantRepository.CreatePlant( | ||
new Plant | ||
{ | ||
PlantId = Guid.NewGuid(), | ||
Nickname = "Dying plant", | ||
UserEmail = "[email protected]", | ||
ImageUrl = defaultPlantImage, | ||
CollectionId = collection2.CollectionId, | ||
LatestChange = DateTime.UtcNow | ||
} | ||
); | ||
|
||
var plants = await plantRepository.GetPlantsForUser("[email protected]", 1, 5); | ||
|
||
var requirementsRepository = scope.ServiceProvider.GetRequiredService<RequirementsRepository>(); | ||
await requirementsRepository.CreateRequirements( | ||
new Requirements | ||
{ | ||
RequirementsId = Guid.NewGuid(), | ||
PlantId = plants.First(p => p.Nickname == "Aloe Vera").PlantId, | ||
LightLevel = RequirementLevel.Low, | ||
SoilMoistureLevel = RequirementLevel.Medium, | ||
HumidityLevel = RequirementLevel.High, | ||
TemperatureLevel = 22, | ||
} | ||
); | ||
|
||
await requirementsRepository.CreateRequirements( | ||
new Requirements | ||
{ | ||
RequirementsId = Guid.NewGuid(), | ||
PlantId = plants.First(p => p.Nickname == "Prickly Pear").PlantId, | ||
LightLevel = RequirementLevel.High, | ||
SoilMoistureLevel = RequirementLevel.Low, | ||
HumidityLevel = RequirementLevel.Low, | ||
TemperatureLevel = 27, | ||
} | ||
); | ||
|
||
await requirementsRepository.CreateRequirements( | ||
new Requirements | ||
{ | ||
RequirementsId = Guid.NewGuid(), | ||
PlantId = plants.First(p => p.Nickname == "Dying plant").PlantId, | ||
LightLevel = RequirementLevel.High, | ||
SoilMoistureLevel = RequirementLevel.Low, | ||
HumidityLevel = RequirementLevel.Medium, | ||
TemperatureLevel = 24, | ||
} | ||
); | ||
|
||
var conditionsLogRepository = scope.ServiceProvider.GetRequiredService<ConditionsLogsRepository>(); | ||
|
||
for (var i = 0; i < 100; i++) | ||
{ | ||
await conditionsLogRepository.CreateConditionsLogAsync( | ||
GetRandomConditionsLog(plants.First(p => p.Nickname == "Prickly Pear").PlantId, i * 6) | ||
); | ||
await conditionsLogRepository.CreateConditionsLogAsync( | ||
GetRandomConditionsLog(plants.First(p => p.Nickname == "Aloe Vera").PlantId, i * 6) | ||
); | ||
} | ||
|
||
await conditionsLogRepository.CreateConditionsLogAsync( | ||
new ConditionsLog() | ||
{ | ||
ConditionsId = Guid.NewGuid(), | ||
PlantId = plants.First(p => p.Nickname == "Dying plant").PlantId, | ||
TimeStamp = DateTime.UtcNow, | ||
Mood = 0, | ||
SoilMoisture = 55, | ||
Light = 13, | ||
Humidity = 68, | ||
Temperature = 25, | ||
} | ||
); | ||
|
||
Console.ForegroundColor = ConsoleColor.Green; | ||
Console.WriteLine("Seeded development data"); | ||
Console.ResetColor(); | ||
} | ||
|
||
private double GetRandomLevelValue() | ||
{ | ||
var random = new Random(); | ||
return random.NextDouble() * 100; | ||
} | ||
|
||
private int GetRandomMood() | ||
{ | ||
var random = new Random(); | ||
return random.Next(0, 5); | ||
} | ||
|
||
private int GetRandomTemperature() | ||
{ | ||
var random = new Random(); | ||
return random.Next(-20, 45); | ||
} | ||
|
||
private ConditionsLog GetRandomConditionsLog(Guid plantId, int hoursAgo = 0) | ||
{ | ||
return new ConditionsLog | ||
{ | ||
ConditionsId = Guid.NewGuid(), | ||
PlantId = plantId, | ||
TimeStamp = DateTime.UtcNow.Subtract(TimeSpan.FromHours(hoursAgo)), | ||
Mood = GetRandomMood(), | ||
SoilMoisture = GetRandomLevelValue(), | ||
Light = GetRandomLevelValue(), | ||
Temperature = GetRandomTemperature(), | ||
Humidity = GetRandomLevelValue(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Text.Json; | ||
using api.Events.Global; | ||
using lib; | ||
using Websocket.Client; | ||
|
||
namespace Tests | ||
{ | ||
public class WebSocketTestClient | ||
{ | ||
public readonly WebsocketClient Client; | ||
public readonly List<BaseDto> ReceivedMessages = []; | ||
private static readonly JsonSerializerOptions JsonSerializerOptions = new() | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
public WebSocketTestClient(string? url = null) | ||
{ | ||
Client = url == null ? new WebsocketClient(new Uri("ws://localhost:" + (Environment.GetEnvironmentVariable("FULLSTACK_API_PORT") ?? "8181"))) : new WebsocketClient(new Uri(url)); | ||
Client.MessageReceived.Subscribe(msg => | ||
{ | ||
BaseDto baseDto = JsonSerializer.Deserialize<BaseDto>(msg.Text, JsonSerializerOptions); | ||
|
||
if (baseDto.eventType == "ServerSendsErrorMessage" || baseDto.eventType.Contains("ServerResponds") || | ||
baseDto.eventType.Contains("ServerRejects")) | ||
{ | ||
var error = JsonSerializer.Deserialize<ServerSendsErrorMessage>(msg.Text, JsonSerializerOptions); | ||
Console.WriteLine("Error: " + error!.Error); | ||
} | ||
|
||
lock (ReceivedMessages) | ||
ReceivedMessages.Add(baseDto); | ||
}); | ||
} | ||
|
||
public async Task<WebSocketTestClient> ConnectAsync() | ||
{ | ||
await Client.Start(); | ||
if (!Client.IsRunning) | ||
throw new Exception("Could not start client!"); | ||
return this; | ||
} | ||
|
||
public void Send<T>(T dto) where T : BaseDto | ||
{ | ||
Client.Send(JsonSerializer.Serialize(dto)); | ||
} | ||
|
||
public async Task DoAndAssert<T>(T? action = null, Func<List<BaseDto>, bool>? condition = null) where T : BaseDto | ||
{ | ||
if ((object) (T) action != null) | ||
Send(action); | ||
if (condition != null) | ||
{ | ||
DateTime startTime = DateTime.UtcNow; | ||
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(5.0)) | ||
{ | ||
lock (ReceivedMessages) | ||
{ | ||
if (condition(ReceivedMessages)) | ||
return; | ||
} | ||
await Task.Delay(100); | ||
} | ||
throw new TimeoutException("Condition not met: "); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.