Skip to content

Commit

Permalink
Fix some nullref errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed May 18, 2024
1 parent f5ea959 commit 2d3963a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
8 changes: 4 additions & 4 deletions ReplayBrowser/Helpers/ReplayHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public async Task<List<Replay>> GetMostRecentReplays(AuthenticationState state)

var caller = AccountHelper.GetAccountGuid(state);
replays = FilterReplays(replays, caller);
var account = _accountService.GetAccount(state);
var account = await _accountService.GetAccount(state);

await _accountService.AddHistory(account, new HistoryEntry()
{
Expand All @@ -51,7 +51,7 @@ public async Task<List<Replay>> GetMostRecentReplays(AuthenticationState state)

public async Task<CollectedPlayerData?> GetPlayerProfile(Guid playerGuid, AuthenticationState authenticationState)
{
var accountCaller = _accountService.GetAccount(authenticationState);
var accountCaller = await _accountService.GetAccount(authenticationState);

var accountRequested = _accountService.GetAccountSettings(playerGuid);

Expand Down Expand Up @@ -244,7 +244,7 @@ public async Task<int> GetTotalReplayCount()
if (replay == null)
return null;

var caller = _accountService.GetAccount(authstate);
var caller = await _accountService.GetAccount(authstate);
replay = FilterReplay(replay, caller);
return replay;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ private Replay FilterReplay(Replay replay, Account? caller = null)

public async Task<SearchResult> SearchReplays(SearchMode searchMode, string query, int page, AuthenticationState authenticationState)
{
var callerAccount = _accountService.GetAccount(authenticationState);
var callerAccount = await _accountService.GetAccount(authenticationState);

switch (searchMode)
{
Expand Down
2 changes: 1 addition & 1 deletion ReplayBrowser/Pages/Account/Logs.razor
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ else
}

var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var account = AccountService.GetAccount(authState);
var account = await AccountService.GetAccount(authState);
if (account == null || !account.IsAdmin)
{
Is1984 = true;
Expand Down
10 changes: 8 additions & 2 deletions ReplayBrowser/Pages/Account/Manage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@if(ErrorMessage != null)
{
<div class="alert alert-danger" role="alert">
Failed to save changes:
Error:
@ErrorMessage
</div>
}
Expand Down Expand Up @@ -68,7 +68,13 @@ else
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
account = AccountService.GetAccount(authState);
account = await AccountService.GetAccount(authState);

if (account == null)
{
ErrorMessage = "Account not found, please go to the home page and then try again. How did you get here?";
return;
}

var uri = new Uri(NavigationManager.Uri);
var query = uri.Query;
Expand Down
29 changes: 26 additions & 3 deletions ReplayBrowser/Services/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class AccountService : IHostedService, IDisposable
{
private readonly IMemoryCache _cache;
private readonly IServiceScopeFactory _scopeFactory;
private readonly Ss14ApiHelper _apiHelper;

private bool _settingsGenerated = false;

public AccountService(IMemoryCache cache, IServiceScopeFactory scopeFactory)
public AccountService(IMemoryCache cache, IServiceScopeFactory scopeFactory, Ss14ApiHelper apiHelper)
{
_cache = cache;
_scopeFactory = scopeFactory;
_apiHelper = apiHelper;
}

public Task StartAsync(CancellationToken cancellationToken)
Expand Down Expand Up @@ -77,15 +79,36 @@ public void Dispose()
_cache.Dispose();
}

public Account? GetAccount(AuthenticationState authstate)
public async Task<Account>? GetAccount(AuthenticationState authstate)
{
var guid = AccountHelper.GetAccountGuid(authstate);
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ReplayDbContext>();
return context.Accounts
var account = context.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefault(a => a.Guid == guid);

if (account == null)
{
if (guid == null)
{
return null;
}

// If the account doesn't exist, we need to create it.
account = new Account()
{
Guid = (Guid)guid,
Username = (await _apiHelper.FetchPlayerDataFromGuid((Guid)guid)).Username,
Settings = new AccountSettings()
};

context.Accounts.Add(account);
await context.SaveChangesAsync();
}

return account;
}

public async Task UpdateAccount(Account? account)
Expand Down
6 changes: 3 additions & 3 deletions ReplayBrowser/Services/LeaderboardService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ public async Task<LeaderboardData> GetLeaderboard(RangeOption rangeOption, strin
{
var context = _scopeFactory.CreateScope().ServiceProvider.GetRequiredService<ReplayDbContext>();

var accountCaller = _accountService.GetAccount(authenticationState);

Account? accountCaller = null;
if (logAction)
{
accountCaller = await _accountService.GetAccount(authenticationState);
await _accountService.AddHistory(accountCaller, new HistoryEntry()
{
Action = Enum.GetName(typeof(Action), Action.LeaderboardViewed) ?? "Unknown",
Expand All @@ -156,7 +156,7 @@ public async Task<LeaderboardData> GetLeaderboard(RangeOption rangeOption, strin
});
}

if (username != null)
if (username != null && accountCaller != null)
{
var accountRequested = await context.Accounts
.Include(a => a.Settings)
Expand Down

0 comments on commit 2d3963a

Please sign in to comment.