Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Kolejność wywołania metod:
- // PostComponent.vote => VoteService.voteOnPost => VoteService.vote
- // czyli w sumie od góry do dołu
- export class PostsComponent implements OnInit {
- // deleguje wysłanie requesta żeby zagłosować i podmienia stan głosów
- async vote(id: string) {
- const votes = await VoteService.voteOnPost(this.http, id)
- .then(value => {
- return value;
- });
- console.log(votes);
- console.log(votes.length);
- console.log(votes.pop());
- // podmiana wartości głosów dalej
- }
- export class VoteService {
- // nie chciałem robić enuma bo KISS i żeby uprościć kod (nie musieć przekazywać stringa z opcją) zrobiłem tą metodę
- static voteOnPost(http: HttpClient, id: string) {
- return this.vote(http, 'post', id).then(value => {return value});
- }
- // tutaj wykonuje się już request, response to liczba głosów (bez żadnego jsona czy coś, po prostu na przykład "1"
- private static async vote(http: HttpClient, onWhat: string, id: string) {
- let resp: any = [];
- await http.get("http://localhost:8080/vote/" + onWhat + "/" + id,
- {headers: {'Authorization': localStorage.getItem('Authorization')}})
- .subscribe(
- (response: number) => resp.push(response),
- error => console.log(error));
- return resp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement