Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hopefully optimize the DB and split the YAML schema out #27

Merged
merged 7 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 90 additions & 89 deletions ReplayBrowser/Controllers/AccountController.cs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions ReplayBrowser/Controllers/DataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public DataController(ReplayDbContext context)
public async Task<List<string>> GetUsernameCompletion(
[FromQuery] string username)
{
var completions = await _context.Players
.Where(p => p.PlayerOocName.ToLower().StartsWith(username.ToLower()))
.Select(p => p.PlayerOocName)
var completions = await _context.ReplayParticipants
.Where(p => p.Username.ToLower().StartsWith(username.ToLower()))
.Select(p => p.Username)
.Distinct() // Remove duplicates
.Take(10)
.ToListAsync();
Expand Down
55 changes: 28 additions & 27 deletions ReplayBrowser/Controllers/ReplayController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ReplayBrowser.Data;
using ReplayBrowser.Data.Models;
using ReplayBrowser.Helpers;
using ReplayBrowser.Services;

Expand All @@ -16,14 +17,14 @@
private readonly ReplayDbContext _dbContext;
private readonly AccountService _accountService;
private readonly ReplayHelper _replayHelper;

public ReplayController(ReplayDbContext dbContext, AccountService accountService, ReplayHelper replayHelper)
{
_dbContext = dbContext;
_accountService = accountService;
_replayHelper = replayHelper;
}

[HttpGet("{replayId}")]
[AllowAnonymous]
public async Task<IActionResult> GetReplay(int replayId)
Expand All @@ -34,10 +35,10 @@
{
return NotFound();
}

return Ok(replay);
}

/// <summary>
/// Deletes all stored profiles for a server group.
/// </summary>
Expand All @@ -45,31 +46,31 @@
[HttpDelete("profile/delete/{serverId}")]
public async Task<IActionResult> DeleteProfile(string serverId)
{
if (!User.Identity.IsAuthenticated)

Check warning on line 49 in ReplayBrowser/Controllers/ReplayController.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
return Unauthorized();
}

var guidRequestor = AccountHelper.GetAccountGuid(User);

var requestor = await _dbContext.Accounts
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guidRequestor);

if (requestor == null)
{
return NotFound("Account is null. This should not happen.");
}
if (!requestor.IsAdmin)

if (!requestor.IsAdmin)
return Unauthorized("You are not an admin.");

var players = await _dbContext.Replays
.Where(r => r.ServerId == serverId)
.Include(r => r.RoundEndPlayers)
.Where(r => r.RoundEndPlayers != null)
.SelectMany(r => r.RoundEndPlayers)
.Include(r => r.RoundParticipants)
.Where(r => r.RoundParticipants != null)
.SelectMany(r => r.RoundParticipants)

Check warning on line 73 in ReplayBrowser/Controllers/ReplayController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
.Select(p => p.PlayerGuid)
.Distinct()
.ToListAsync();
Expand All @@ -80,26 +81,26 @@
DELETE FROM "CharacterData"
WHERE "CollectedPlayerDataPlayerGuid" = '{player}';
""");

await _dbContext.Database.ExecuteSqlRawAsync($"""
DELETE FROM "JobCountData"
WHERE "CollectedPlayerDataPlayerGuid" = '{player}';
""");
}

await _dbContext.PlayerProfiles
.Where(p => players.Contains(p.PlayerGuid))
.ExecuteDeleteAsync();

return Ok();
}

[HttpGet("profile/{profileGuid:guid}")]
public async Task<IActionResult> GetPlayerData(Guid profileGuid)
{
// ok very jank, we construct a AuthenticationState object from the current user
var authState = new AuthenticationState(HttpContext.User);

try
{
return Ok(await _replayHelper.GetPlayerProfile(profileGuid, authState));
Expand All @@ -109,7 +110,7 @@
return Unauthorized(e.Message);
}
}

/// <summary>
/// Marks a profile "watched" for the current user.
/// </summary>
Expand All @@ -122,25 +123,25 @@
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guid);

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

var isWatched = account.SavedProfiles.Contains(profileGuid);

if (!account.SavedProfiles.Remove(profileGuid))
{
account.SavedProfiles.Add(profileGuid);
}

await _dbContext.SaveChangesAsync();

return Ok(!isWatched);
}


/// <summary>
/// Marks a replay as a favorite for the current user.
/// </summary>
Expand All @@ -153,25 +154,25 @@
.Include(a => a.Settings)
.Include(a => a.History)
.FirstOrDefaultAsync(a => a.Guid == guid);

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

var replay = await _dbContext.Replays.FindAsync(replayId);
if (replay == null)
{
return NotFound();
}

var isFavorited = account.FavoriteReplays.Contains(replayId);

if (!account.FavoriteReplays.Remove(replayId))
{
account.FavoriteReplays.Add(replayId);
}

await _dbContext.SaveChangesAsync();

return Ok(!isFavorited);
Expand Down
Loading
Loading