zgo/src/app/user.service.ts

125 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-10-15 19:14:49 +00:00
import {Injectable} from '@angular/core';
import {Subject, BehaviorSubject, Observable} from 'rxjs';
2021-10-15 19:14:49 +00:00
import {HttpClient, HttpParams} from '@angular/common/http';
import {User} from './user.model';
2021-10-20 20:51:14 +00:00
import {Owner} from './owner.model';
2021-10-15 19:14:49 +00:00
@Injectable({providedIn: 'root'})
export class UserService{
2021-10-21 16:22:48 +00:00
private dataStore: { user: User, owner: Owner} = {
user: {
address: '',
session: '',
blocktime: 0
},
owner: {
address: '',
name: ''
}
};
2021-10-15 19:14:49 +00:00
private uZaddr = '';
2021-10-20 20:51:14 +00:00
private oZaddr = '';
private uName = '';
private session: string | null = '';
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
private _userUpdated: BehaviorSubject<User> = new BehaviorSubject(this.dataStore.user);
2021-10-20 20:51:14 +00:00
private uNameUpdated = new Subject<string>();
2021-10-21 16:22:48 +00:00
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
2021-10-21 16:22:48 +00:00
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
public readonly userUpdate: Observable<User> = this._userUpdated.asObservable();
2021-10-15 19:14:49 +00:00
constructor(private http: HttpClient){
2021-10-20 20:51:14 +00:00
this.session = localStorage.getItem('s4z_token');
if (this.session != null) {
this.findUser(this.session);
}
}
findUser(session: string) {
const params = new HttpParams().append('session', session);
let obs = this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'});
obs.subscribe((UserDataResponse) => {
console.log(UserDataResponse.status);
if (UserDataResponse.status == 200){
this.dataStore.user = UserDataResponse.body!.user[0];
console.log(`US: Found user, returning it`);
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
this._userUpdated.next(Object.assign({}, this.dataStore).user);
2021-10-21 19:29:04 +00:00
this.getOwner(Object.assign({},this.dataStore.user).address);
} else {
console.log('US: Did not find user');
}
});
return obs;
2021-10-15 19:14:49 +00:00
}
addUser(address: string, session: string, blocktime: number) {
const user: User={_id: '', address: address, session: session, blocktime: blocktime};
2021-10-21 16:22:48 +00:00
let obs = this.http.post<{message: string}>('http://localhost:3000/api/users', user);
obs.subscribe((responseData) => {
2021-10-20 20:51:14 +00:00
console.log(responseData.message);
2021-10-21 16:22:48 +00:00
this.getOwner(address).subscribe((response) => {
console.log('addUser, checking owner', response);
});
2021-10-20 20:51:14 +00:00
});
2021-10-21 16:22:48 +00:00
return obs;
2021-10-20 20:51:14 +00:00
}
addOwner(address: string) {
const owner: Owner={_id: '', address: address, name: 'Zgo-'.concat(address.substring(0,5))};
2021-10-21 16:22:48 +00:00
let obs = this.http.post<{message: string}>('http://localhost:3000/api/addowner', {address: owner.address, name: owner.name});
obs.subscribe((responseData) => {
2021-10-20 20:51:14 +00:00
console.log(responseData.message);
});
2021-10-21 16:22:48 +00:00
return obs;
2021-10-20 20:51:14 +00:00
}
2021-10-28 20:34:48 +00:00
updateOwner(owner: Owner) {
this.http.post<{message: string, owner: Owner}>('http://localhost:3000/api/updateowner', {owner: owner}).
2021-10-15 19:14:49 +00:00
subscribe((responseData) => {
console.log(responseData.message);
2021-10-28 20:34:48 +00:00
//this.dataStore.owner = responseData.owner;
//this._ownerUpdated.next(Object.assign({},this.dataStore).owner);
2021-10-15 19:14:49 +00:00
});
}
2021-10-20 20:51:14 +00:00
getOwner(address: string) {
2021-10-21 19:29:04 +00:00
console.log('getOwner', address);
2021-10-20 20:51:14 +00:00
const ownParams = new HttpParams().append('address', address);
2021-10-21 16:22:48 +00:00
let obs = this.http.get<{message:string, owner: any}>('http://localhost:3000/api/getowner', {params: ownParams, observe: 'response'});
obs.subscribe((OwnerDataResponse) => {
2021-10-21 19:29:04 +00:00
console.log('api/getowner', OwnerDataResponse.status);
2021-10-20 20:51:14 +00:00
if (OwnerDataResponse.status == 200) {
2021-10-21 16:22:48 +00:00
this.dataStore.owner = OwnerDataResponse.body!.owner[0];
2021-10-21 19:29:04 +00:00
//console.log('getOwner object', this.dataStore.owner);
this._ownerUpdated.next(Object.assign({},this.dataStore).owner);
2021-10-20 20:51:14 +00:00
} else {
2021-10-21 19:29:04 +00:00
console.log("No owner found, adding");
2021-10-21 16:22:48 +00:00
this.addOwner(address);
2021-10-20 20:51:14 +00:00
}
});
2021-10-21 16:22:48 +00:00
return obs;
2021-10-20 20:51:14 +00:00
}
hasOwner() {
return this.uZaddr === this.oZaddr;
}
getNameUpdateListener() {
return this.uNameUpdated;
}
2021-10-15 19:14:49 +00:00
}