1
1
using System ;
2
+ using System . Diagnostics ;
2
3
using System . Threading . Tasks ;
3
4
using Blazored . LocalStorage ;
4
5
using LinkDotNet . Domain ;
5
6
using LinkDotNet . Infrastructure . Persistence ;
6
7
using Microsoft . AspNetCore . Components ;
7
8
using Microsoft . AspNetCore . Components . Authorization ;
8
- using Microsoft . Extensions . Logging ;
9
9
10
10
namespace LinkDotNet . Blog . Web . Shared
11
11
{
@@ -20,35 +20,32 @@ public class UserRecordService : IUserRecordService
20
20
private readonly NavigationManager navigationManager ;
21
21
private readonly AuthenticationStateProvider authenticationStateProvider ;
22
22
private readonly ILocalStorageService localStorageService ;
23
- private readonly ILogger logger ;
24
23
25
24
public UserRecordService (
26
25
IRepository < UserRecord > userRecordRepository ,
27
26
NavigationManager navigationManager ,
28
27
AuthenticationStateProvider authenticationStateProvider ,
29
- ILocalStorageService localStorageService ,
30
- ILogger logger )
28
+ ILocalStorageService localStorageService )
31
29
{
32
30
this . userRecordRepository = userRecordRepository ;
33
31
this . navigationManager = navigationManager ;
34
32
this . authenticationStateProvider = authenticationStateProvider ;
35
33
this . localStorageService = localStorageService ;
36
- this . logger = logger ;
37
34
}
38
35
39
36
public async Task StoreUserRecordAsync ( )
40
37
{
41
38
try
42
39
{
43
- await GetAndStoreUSerRecordAsync ( ) ;
40
+ await GetAndStoreUserRecordAsync ( ) ;
44
41
}
45
42
catch ( Exception e )
46
43
{
47
- logger . Log ( LogLevel . Error , e , "Couldn't write user record ") ;
44
+ Trace . Write ( $ "Exception: { e } ") ;
48
45
}
49
46
}
50
47
51
- private async Task GetAndStoreUSerRecordAsync ( )
48
+ private async Task GetAndStoreUserRecordAsync ( )
52
49
{
53
50
var userIdentity = ( await authenticationStateProvider . GetAuthenticationStateAsync ( ) ) . User . Identity ;
54
51
if ( userIdentity == null || userIdentity . IsAuthenticated )
@@ -58,11 +55,13 @@ private async Task GetAndStoreUSerRecordAsync()
58
55
59
56
var identifierHash = await GetIdentifierHashAsync ( ) ;
60
57
58
+ var url = GetClickedUrl ( ) ;
59
+
61
60
var record = new UserRecord
62
61
{
63
62
UserIdentifierHash = identifierHash ,
64
63
DateTimeUtcClicked = DateTime . UtcNow ,
65
- UrlClicked = navigationManager . ToBaseRelativePath ( navigationManager . Uri ) ,
64
+ UrlClicked = url ,
66
65
} ;
67
66
68
67
await userRecordRepository . StoreAsync ( record ) ;
@@ -81,5 +80,18 @@ private async Task<int> GetIdentifierHashAsync()
81
80
await localStorageService . SetItemAsync ( "user" , id ) ;
82
81
return id . GetHashCode ( ) ;
83
82
}
83
+
84
+ private string GetClickedUrl ( )
85
+ {
86
+ var basePath = navigationManager . ToBaseRelativePath ( navigationManager . Uri ) ;
87
+
88
+ if ( string . IsNullOrEmpty ( basePath ) )
89
+ {
90
+ return string . Empty ;
91
+ }
92
+
93
+ var queryIndex = basePath . IndexOf ( '?' ) ;
94
+ return queryIndex <= 0 ? basePath [ ..( queryIndex - 1 ) ] : basePath ;
95
+ }
84
96
}
85
97
}
0 commit comments