11
22using CashFlowAnalyzer . Shared . Models ;
33using System . Net ;
4+ using System . Net . Http . Json ;
45using System . Text ;
56using System . Text . Json ;
67
78namespace CashFlowAnalyzer . Client . Services ;
89
910public class AccountService : IAccountService
1011{
12+ // workaround for a bug? https://github.com/dotnet/aspnetcore/issues/51986
13+ // or misconfiguration https://github.com/dotnet/aspnetcore/issues/51468
14+ private string baseAddress = "http://localhost:5130" ;
1115 private readonly HttpClient _http ;
1216 private readonly ILogger < AccountService > _log ;
1317 public AccountService ( HttpClient http , ILogger < AccountService > log )
@@ -16,27 +20,36 @@ public AccountService(HttpClient http, ILogger<AccountService> log)
1620 _log = log ;
1721 }
1822
19- public async Task < bool > Login ( LoginModel model )
23+ public async Task < AccountServiceResult > Login ( LoginModel model )
2024 {
21- var request = new HttpRequestMessage ( HttpMethod . Post , " api/auth/login") ;
25+ var request = new HttpRequestMessage ( HttpMethod . Post , $ " { baseAddress } / api/auth/login") ;
2226 request . Content = new StringContent ( JsonSerializer . Serialize ( model ) , Encoding . UTF8 , "application/json" ) ;
23- using var response = await _http . SendAsync ( request ) ;
24- if ( response . StatusCode == HttpStatusCode . OK )
25- {
26- _log . LogInformation ( "Login successful for user {Username}" , model . Username ) ;
27- return true ;
28- }
29- _log . LogWarning ( "Login failed for user {Username}" , model . Username ) ;
30- return false ;
27+ return await SendRequest ( request ) ;
28+ }
29+
30+ public async Task < AccountServiceResult > Logout ( )
31+ {
32+ var request = new HttpRequestMessage ( HttpMethod . Post , $ "{ baseAddress } /api/auth/logout") ;
33+ return await SendRequest ( request ) ;
3134 }
3235
33- public Task < bool > Logout ( )
36+ public async Task < AccountServiceResult > Register ( RegisterModel model )
3437 {
35- throw new NotImplementedException ( ) ;
38+ var request = new HttpRequestMessage ( HttpMethod . Post , $ "{ baseAddress } /api/auth/register") ;
39+ request . Content = new StringContent ( JsonSerializer . Serialize ( model ) , Encoding . UTF8 , "application/json" ) ;
40+ return await SendRequest ( request ) ;
3641 }
3742
38- public Task < bool > Register ( RegisterModel model )
43+ private async Task < AccountServiceResult > SendRequest ( HttpRequestMessage request )
3944 {
40- throw new NotImplementedException ( ) ;
45+ using var response = await _http . SendAsync ( request ) ;
46+ if ( response . StatusCode == HttpStatusCode . OK )
47+ {
48+ _log . LogInformation ( "Registration was successful" ) ;
49+ return new AccountServiceResult ( ) { Success = true } ;
50+ }
51+ _log . LogWarning ( "Registration failed" ) ;
52+ var errors = await response . Content . ReadFromJsonAsync < List < string > > ( ) ;
53+ return new AccountServiceResult ( ) { Success = false , Errors = errors } ;
4154 }
4255}
0 commit comments