Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Kolejność wywołania metod:
  2. // PostComponent.vote => VoteService.voteOnPost => VoteService.vote
  3. // czyli w sumie od góry do dołu
  4.  
  5.  
  6.  
  7. export class PostsComponent implements OnInit {
  8.  
  9. // deleguje wysłanie requesta żeby zagłosować i podmienia stan głosów
  10.   async vote(id: string) {
  11.     const votes = await VoteService.voteOnPost(this.http, id)
  12.       .then(value => {
  13.         return value;
  14.       });
  15.     console.log(votes);
  16.     console.log(votes.length);
  17.     console.log(votes.pop());
  18.     // podmiana wartości głosów dalej
  19. }
  20.  
  21.  
  22.  
  23. export class VoteService {
  24.  
  25. // nie chciałem robić enuma bo KISS i żeby uprościć kod (nie musieć przekazywać stringa z opcją) zrobiłem tą metodę
  26.  static voteOnPost(http: HttpClient, id: string) {
  27.     return this.vote(http, 'post', id).then(value => {return value});
  28.   }
  29.  
  30. // tutaj wykonuje się już request, response to liczba głosów (bez żadnego jsona czy coś, po prostu na przykład "1"
  31.   private static async vote(http: HttpClient, onWhat: string, id: string) {
  32.     let resp: any = [];
  33.     await http.get("http://localhost:8080/vote/" + onWhat + "/" + id,
  34.       {headers: {'Authorization': localStorage.getItem('Authorization')}})
  35.       .subscribe(
  36.         (response: number) => resp.push(response),
  37.         error => console.log(error));
  38.     return resp;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement