Skip to content

Commit 6af911d

Browse files
authored
Merge pull request #53 from veloek/fill-db-with-activities
- Dev tool to add 100 activities - Use bogus to add a random activity with dev tools
2 parents 0dc1fe6 + 1764e4e commit 6af911d

File tree

9 files changed

+108
-21
lines changed

9 files changed

+108
-21
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Bogus;
2+
using Tevling.Strava;
3+
4+
namespace Tevling.Bogus;
5+
6+
public class DetailedActivityFaker : Faker<DetailedActivity>
7+
{
8+
public DetailedActivityFaker(string name, long stravaId)
9+
{
10+
RuleFor(da => da.Id, f => stravaId);
11+
RuleFor(da => da.Name, f => name);
12+
RuleFor(da => da.Description, f => f.Lorem.Sentence());
13+
RuleFor(da => da.Distance, f => f.Random.Float(1000, 10000));
14+
RuleFor(da => da.MovingTime, f => f.Random.Int(1000, 10000));
15+
RuleFor(da => da.ElapsedTime, f => f.Random.Int(1000, 10000));
16+
RuleFor(da => da.TotalElevationGain, f => f.Random.Float(1000, 10000));
17+
RuleFor(da => da.Calories, f => f.Random.Float(1000, 10000));
18+
RuleFor(da => da.Type, f => f.PickRandom<ActivityType>());
19+
RuleFor(da => da.StartDate, f => f.Date.Past(yearsToGoBack: 1));
20+
RuleFor(da => da.Manual, f => f.Random.Bool());
21+
}
22+
}

Tevling/Components/DevTools.razor

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
<div class="d-flex flex-column align-items-start rg-3">
2+
<div class="d-flex flex-row cg-3">
3+
<InputRadioGroup @bind-Value="IsRandomEnabled" @bind-Value:after="OnRandomizationChanged">
4+
<InputRadio class="btn-check" id="disable-random" Value="false"/>
5+
<label class="btn btn-outline-success" for="disable-random">Disable randomization</label>
6+
7+
<InputRadio class="btn-check" id="enable-random" Value="true"/>
8+
<label class="btn btn-outline-danger" for="enable-random">Enable randomization</label>
9+
</InputRadioGroup>
10+
</div>
211
<button class="btn btn-primary" @onclick="AddActivity">Add activity</button>
12+
<button class="btn btn-primary" @onclick="AddLoadsOfActivities">Add loads of activities</button>
313
<button class="btn btn-primary" @onclick="AddOthersActivity" disabled="@NoOtherAthletes">Add activity for others</button>
414
<button class="btn btn-primary" @onclick="AddAthlete">Add athlete</button>
515
<button class="btn btn-primary" @onclick="AddChallenge">Add challenge</button>

Tevling/Components/DevTools.razor.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,24 @@ public partial class DevTools : ComponentBase
55
[Inject] private IActivityService ActivityService { get; set; } = null!;
66
[Inject] private IAthleteService AthleteService { get; set; } = null!;
77
[Inject] private IChallengeService ChallengeService { get; set; } = null!;
8+
[Inject] private IDevService DevService { get; set; } = null!;
89

910
[Parameter] public Athlete? Athlete { get; set; }
1011

1112
private Athlete[] _athletes = [];
1213
private bool NoOtherAthletes => _athletes.Length == 1;
1314
private int ClearChallengeWinnerId { get; set; }
15+
public bool IsRandomEnabled { get; set; }
1416

1517
protected override async Task OnInitializedAsync()
1618
{
1719
_athletes = await AthleteService.GetAthletesAsync();
20+
IsRandomEnabled = DevService.IsRandomEnabled();
21+
}
22+
23+
private void OnRandomizationChanged()
24+
{
25+
DevService.SetRandomEnabled(IsRandomEnabled);
1826
}
1927

2028
private Task AddActivity()
@@ -25,6 +33,14 @@ private Task AddActivity()
2533
return ActivityService.CreateActivityAsync(Athlete.StravaId, Random.Shared.Next(1000, 10000));
2634
}
2735

36+
private async Task AddLoadsOfActivities()
37+
{
38+
for (int i = 0; i < 100; i++)
39+
{
40+
await AddActivity();
41+
}
42+
}
43+
2844
private Task AddOthersActivity()
2945
{
3046
long randomAthleteStravaId;
@@ -164,7 +180,8 @@ private async Task AddFollower()
164180
throw new ArgumentException(nameof(Athlete));
165181

166182
await AthleteService.ToggleFollowingAsync(
167-
follower, Athlete.Id);
183+
follower,
184+
Athlete.Id);
168185
}
169186

170187
private async Task ClearChallengeWinner()

Tevling/Controllers/DevController.cs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ namespace Tevling.Controllers;
99
[ApiController]
1010
[Route("dev")]
1111
[FeatureGate(FeatureFlag.DevTools)]
12-
public class DevController : ControllerBase
12+
public class DevController(IDevService devService) : ControllerBase
1313
{
14-
public DevController()
15-
{
16-
}
1714

1815
[HttpGet]
1916
[Route("strava/authorize")]
@@ -34,22 +31,7 @@ public async Task<IActionResult> Deauthorize()
3431
[Route("strava/activities/{stravaId}")]
3532
public IActionResult GetActivity(long stravaId)
3633
{
37-
DetailedActivity activity = new()
38-
{
39-
Id = stravaId,
40-
Name = "Activity_" + stravaId,
41-
Description = "Description_" + stravaId,
42-
Distance = 1234,
43-
MovingTime = 631,
44-
ElapsedTime = 963,
45-
TotalElevationGain = 124.0f,
46-
Calories = 0.0f,
47-
Type = ActivityType.Run,
48-
StartDate = DateTimeOffset.UtcNow,
49-
Manual = true,
50-
};
51-
52-
return new JsonResult(activity);
34+
return new JsonResult(devService.GetActivity(stravaId));
5335
}
5436

5537
[HttpGet]

Tevling/Pages/About.razor

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<a href="https://www.chartjs.org/" target="_blank">Chart.js</a>
2828
- <a href="https://github.com/chartjs/Chart.js/blob/master/LICENSE.md" target="_blank">MIT license</a>
2929
</li>
30+
<li>
31+
<a href="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/bchavez/Bogus" target="_blank">Bogus</a>
32+
- <a href="https://github.com/bchavez/Bogus/blob/master/LICENSE" target="_blank">MIT license</a>
33+
</li>
3034
<li>
3135
<a href="https://www.flaticon.com/free-icons/arm-wrestling" title="arm wrestling icons">Arm wrestling icons created by Freepik - Flaticon</a>
3236
</li>

Tevling/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
IConfigurationSection section = builder.Configuration.GetSection(nameof(StravaConfig));
4444
StravaConfig stravaConfig = section.Get<StravaConfig>() ?? new StravaConfig();
4545
builder.Services.AddSingleton(stravaConfig);
46+
builder.Services.AddSingleton<IDevService, DevService>();
4647

4748
builder.Services
4849
.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)

Tevling/Services/DevService.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Tevling.Bogus;
2+
using Tevling.Strava;
3+
4+
namespace Tevling.Services;
5+
6+
public class DevService : IDevService
7+
{
8+
private bool _isRandomEnabled = false;
9+
10+
public bool IsRandomEnabled()
11+
{
12+
return _isRandomEnabled;
13+
}
14+
15+
public void SetRandomEnabled(bool changed)
16+
{
17+
_isRandomEnabled = changed;
18+
}
19+
20+
public DetailedActivity GetActivity(long stravaId)
21+
{
22+
return _isRandomEnabled
23+
? new DetailedActivityFaker(name: "Activity " + stravaId, stravaId: stravaId).Generate()
24+
: new DetailedActivity
25+
{
26+
Id = stravaId,
27+
Name = "Activity_" + stravaId,
28+
Description = "Description_" + stravaId,
29+
Distance = 1234,
30+
MovingTime = 631,
31+
ElapsedTime = 963,
32+
TotalElevationGain = 124.0f,
33+
Calories = 0.0f,
34+
Type = ActivityType.Run,
35+
StartDate = DateTimeOffset.UtcNow,
36+
Manual = true,
37+
};
38+
}
39+
}

Tevling/Services/IDevService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Tevling.Strava;
2+
3+
namespace Tevling.Services;
4+
5+
public interface IDevService
6+
{
7+
public void SetRandomEnabled(bool changed);
8+
public bool IsRandomEnabled();
9+
10+
public DetailedActivity GetActivity(long stravaId);
11+
}

Tevling/Tevling.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
12+
<PackageReference Include="Bogus" Version="35.6.3" />
1213
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="4.13.0">
1314
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1415
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)