View difference between Paste ID: Hcx8aKHw and j6eaQ2y3
SHOW: | | - or go back to the newest paste.
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-
    const post = this.posts.find(value => value.id === id);
9+
// deleguje wysłanie requesta żeby zagłosować i podmienia stan głosów
10-
    const index = this.posts.indexOf(post);
10+
11
    const votes = await VoteService.voteOnPost(this.http, id)
12
      .then(value => {
13
        return value;
14
      });
15
    console.log(votes);
16-
    console.log(votes.votes);
16+
    console.log(votes.length);
17-
    this.posts[index] = post;
17+
    console.log(votes.pop());
18-
  } 
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-
    const resp: any = new Post();
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-
        (response: number) => resp.votes = response,
34+
35
      .subscribe(
36
        (response: number) => resp.push(response),
37
        error => console.log(error));
38
    return resp;
39
}