1
1
using Microsoft . AspNetCore . Authorization ;
2
+ using Microsoft . AspNetCore . Components . Authorization ;
2
3
using Microsoft . AspNetCore . Mvc ;
3
4
using Microsoft . EntityFrameworkCore ;
4
5
using ReplayBrowser . Data ;
@@ -14,12 +15,61 @@ public class ReplayController : Controller
14
15
{
15
16
private readonly ReplayDbContext _dbContext ;
16
17
private readonly AccountService _accountService ;
18
+ private readonly ReplayHelper _replayHelper ;
17
19
18
- public ReplayController ( ReplayDbContext dbContext , AccountService accountService )
20
+ public ReplayController ( ReplayDbContext dbContext , AccountService accountService , ReplayHelper replayHelper )
19
21
{
20
22
_dbContext = dbContext ;
21
23
_accountService = accountService ;
24
+ _replayHelper = replayHelper ;
22
25
}
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
+
23
73
24
74
/// <summary>
25
75
/// Marks a replay as a favorite for the current user.
0 commit comments