Created
November 6, 2020 13:52
-
-
Save ozzpy/e1ce17226ff4ba0da175abb83495fc8d to your computer and use it in GitHub Desktop.
servicos.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import {Observable, Subject} from 'rxjs'; | |
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; | |
import {environment} from '../../../environments/environment'; | |
import {Storage} from '@ionic/storage'; | |
import {TagsModel} from '../_models/tags.model'; | |
import {map} from 'rxjs/operators'; | |
import {API_PUBLIC, API_TAGS} from '../_server'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class TagsService { | |
private tagsList = new Subject<TagsModel[]>(); | |
private tagsListHome = new Subject<TagsModel[]>(); | |
private userToken = ""; | |
httpHeadersDefault = new HttpHeaders({ | |
'ozzpy' : 'v2020', | |
}); | |
httpHeaders = new HttpHeaders({ | |
'Content-Type' : 'application/json', | |
'ozzpy' : 'v2020', | |
'token-access' : this.userToken | |
}); | |
constructor(private storage: Storage,private http: HttpClient) { | |
this.getStoredToken(); | |
} | |
private getStoredToken() { | |
this.storage.ready().then( ready => { | |
this.storage.get(environment.authTokenKey).then( token => { | |
this.userToken = token; | |
if (this.userToken != null) { | |
this.httpHeaders = new HttpHeaders({ | |
'Content-Type' : 'application/json', | |
'ozzpy' : 'v2020', | |
'token-access' : this.userToken | |
}); | |
}else{ | |
this.httpHeaders = new HttpHeaders({ | |
'Content-Type' : 'application/json', | |
'ozzpy' : 'v2020', | |
'token-access' : "" | |
}); | |
} | |
}); | |
}); | |
} | |
public init() { | |
this.getStoredToken(); | |
} | |
public getAll(isPublic: boolean, limit: string = "") : Observable<TagsModel[]> { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.set('limit',limit); | |
if ( isPublic == true ) { | |
httpParams = httpParams.set('filter','getAllTags'); | |
return this.http.get<any>(API_PUBLIC, { headers: this.httpHeadersDefault, params: httpParams }).pipe( | |
map( response => { | |
if ( response.status == "error" ) { | |
this.sendTagsList([]); | |
return [] | |
} | |
this.sendTagsList(response.data); | |
return response.data; | |
}) | |
); | |
} else { | |
httpParams = httpParams.set('filter','getAll'); | |
return this.http.get<any>(API_TAGS, { headers: this.httpHeaders, params: httpParams }).pipe( | |
map( response => { | |
if ( response.status == "error" ) { | |
this.sendTagsList([]); | |
return [] | |
} | |
this.sendTagsList(response.data); | |
return response.data; | |
}) | |
); | |
} | |
} | |
public _getAllHome(limit: string = "") : Observable<TagsModel[]> { | |
let httpParams = new HttpParams(); | |
httpParams = httpParams.set('limit',limit); | |
httpParams = httpParams.set('filter','getAllTags'); | |
return this.http.get<any>(API_PUBLIC, { headers: this.httpHeadersDefault, params: httpParams }).pipe( | |
map( response => { | |
if ( response.status == "error" ) { | |
this.sendTagsListHome([]); | |
return [] | |
} | |
this.sendTagsListHome(response.data); | |
return response.data; | |
}) | |
); | |
} | |
/** | |
* tagsList Observable | |
*/ | |
sendTagsList(data: TagsModel[]) { | |
this.tagsList.next(data); | |
} | |
clearTagsList() { | |
this.tagsList.next([]); | |
} | |
watchTagsList(): Observable<TagsModel[]> { | |
return this.tagsList.asObservable(); | |
} | |
sendTagsListHome(data: TagsModel[]) { | |
this.tagsListHome.next(data); | |
} | |
clearTagsListHome() { | |
this.tagsListHome.next([]); | |
} | |
watchTagsListHome(): Observable<TagsModel[]> { | |
return this.tagsListHome.asObservable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment