43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import {Injectable} from '@angular/core';
|
|
import {Subject} from 'rxjs';
|
|
import {HttpClient, HttpParams} from '@angular/common/http';
|
|
import {User} from './user.model';
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
export class UserService{
|
|
private uZaddr = '';
|
|
private uZaddrUpdated = new Subject<string>();
|
|
|
|
constructor(private http: HttpClient){
|
|
}
|
|
|
|
addUser(address: string, session: string, blocktime: number) {
|
|
const user: User={_id: '', address: address, session: session, blocktime: blocktime};
|
|
this.http.post<{message: string}>('http://localhost:3000/api/users', user).
|
|
subscribe((responseData) => {
|
|
console.log(responseData.message);
|
|
});
|
|
}
|
|
|
|
getUser(session: string) {
|
|
const params = new HttpParams().append('session', session);
|
|
this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
|
|
subscribe((UserDataResponse) => {
|
|
console.log(UserDataResponse.status);
|
|
if (UserDataResponse.status == 200){
|
|
this.uZaddr = UserDataResponse.body!.user[0].address;
|
|
this.uZaddrUpdated.next(this.uZaddr);
|
|
} else {
|
|
this.uZaddrUpdated.next('');
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
getZaddrUpdateListener() {
|
|
return this.uZaddrUpdated;
|
|
}
|
|
|
|
}
|