Skip to content

Commit f9faac4

Browse files
committed
Added profile "watching"
1 parent 3dcd6fd commit f9faac4

13 files changed

+652
-4
lines changed

ReplayBrowser/Controllers/ReplayController.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Authorization;
2+
using Microsoft.AspNetCore.Components.Authorization;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.EntityFrameworkCore;
45
using ReplayBrowser.Data;
@@ -14,12 +15,61 @@ public class ReplayController : Controller
1415
{
1516
private readonly ReplayDbContext _dbContext;
1617
private readonly AccountService _accountService;
18+
private readonly ReplayHelper _replayHelper;
1719

18-
public ReplayController(ReplayDbContext dbContext, AccountService accountService)
20+
public ReplayController(ReplayDbContext dbContext, AccountService accountService, ReplayHelper replayHelper)
1921
{
2022
_dbContext = dbContext;
2123
_accountService = accountService;
24+
_replayHelper = replayHelper;
2225
}
26+
27+
[HttpGet("profile/{profileGuid:guid}")]
28+
public async Task<IActionResult> GetPlayerData(Guid profileGuid)
29+
{
30+
// ok very jank, we construct a AuthenticationState object from the current user
31+
var authState = new AuthenticationState(HttpContext.User);
32+
33+
try
34+
{
35+
return Ok(await _replayHelper.GetPlayerProfile(profileGuid, authState));
36+
}
37+
catch (UnauthorizedAccessException e)
38+
{
39+
return Unauthorized(e.Message);
40+
}
41+
}
42+
43+
/// <summary>
44+
/// Marks a profile "watched" for the current user.
45+
/// </summary>
46+
/// <returns>True if the profile is now watched, false if it is now unwatched.</returns>
47+
[HttpPost("watch/{profileGuid:guid}")]
48+
public async Task<IActionResult> WatchProfile(Guid profileGuid)
49+
{
50+
var guid = AccountHelper.GetAccountGuid(HttpContext.User);
51+
var account = await _dbContext.Accounts
52+
.Include(a => a.Settings)
53+
.Include(a => a.History)
54+
.FirstOrDefaultAsync(a => a.Guid == guid);
55+
56+
if (account == null)
57+
{
58+
return Unauthorized();
59+
}
60+
61+
var isWatched = account.SavedProfiles.Contains(profileGuid);
62+
63+
if (!account.SavedProfiles.Remove(profileGuid))
64+
{
65+
account.SavedProfiles.Add(profileGuid);
66+
}
67+
68+
await _dbContext.SaveChangesAsync();
69+
70+
return Ok(!isWatched);
71+
}
72+
2373

2474
/// <summary>
2575
/// Marks a replay as a favorite for the current user.

ReplayBrowser/Data/CollectedPlayerData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class CollectedPlayerData
3131

3232
public DateTime LastSeen { get; set; } = DateTime.MinValue;
3333

34+
/// <summary>
35+
/// Is this profile currently being "watched" by the user?
36+
/// </summary>
37+
public bool IsWatched { get; set; } = false;
38+
3439
public override bool Equals(object? obj)
3540
{
3641
if (obj is not CollectedPlayerData other)

0 commit comments

Comments
 (0)