|
1 | 1 | import { inject, Injectable } from "@angular/core"; |
2 | | -import { BehaviorSubject, map, of } from "rxjs"; |
| 2 | +import { catchError, finalize, throwError } from "rxjs"; |
3 | 3 | import { PostApi } from "../apis"; |
4 | 4 | import { Post } from "../models"; |
| 5 | +import { PostStore } from "./post-store.service"; |
5 | 6 |
|
6 | 7 | @Injectable({ |
7 | 8 | providedIn: "root", |
8 | 9 | }) |
9 | 10 | export class PostService { |
10 | | - private readonly PostApi = inject(PostApi); |
11 | | - private _posts$ = new BehaviorSubject<{ |
12 | | - data: Post[]; |
13 | | - expired: number; |
14 | | - }>({ data: [], expired: 0 }); |
15 | | - |
16 | | - get posts() { |
17 | | - return this._posts$.value.data; |
18 | | - } |
| 11 | + private readonly postApi = inject(PostApi); |
| 12 | + private readonly postStore = inject(PostStore); |
19 | 13 |
|
20 | | - clearCache() { |
21 | | - this._posts$.next({ data: [], expired: 0 }); |
| 14 | + get postState() { |
| 15 | + return this.postStore.state; |
22 | 16 | } |
23 | 17 |
|
24 | 18 | isExpired() { |
25 | | - const expired = Date.now() > this._posts$.value.expired; |
| 19 | + const expired = Date.now() > this.postStore.state().expired; |
26 | 20 | if (expired) { |
27 | | - this.clearCache(); |
| 21 | + this.postStore.reset(); |
28 | 22 | } |
29 | 23 | return expired; |
30 | 24 | } |
31 | 25 |
|
32 | | - getAllPosts() { |
33 | | - if (this.posts.length && !this.isExpired()) { |
34 | | - return of(this.posts); |
| 26 | + fetchAllPosts() { |
| 27 | + if (!this.isExpired()) { |
| 28 | + console.log("data not expired"); |
| 29 | + return; |
35 | 30 | } |
36 | 31 |
|
37 | | - return this.PostApi.getAll({ limit: 100 }).pipe( |
38 | | - map((data) => { |
39 | | - this._posts$.next({ |
40 | | - data, |
41 | | - expired: Date.now() + 3 * 60 * 1000, // expires in 3 minutes |
42 | | - }); |
43 | | - return data; |
44 | | - }), |
45 | | - ); |
| 32 | + this.postStore.setLoading(true); |
| 33 | + this.postApi |
| 34 | + .getAll({ limit: 20 }) |
| 35 | + .pipe( |
| 36 | + catchError((error) => { |
| 37 | + console.log("fetch all post", error); |
| 38 | + return throwError(() => error); |
| 39 | + }), |
| 40 | + finalize(() => { |
| 41 | + this.postStore.setLoading(false); |
| 42 | + }), |
| 43 | + ) |
| 44 | + .subscribe((data) => { |
| 45 | + const expired = Date.now() + 1 * 60 * 1000; |
| 46 | + this.postStore.setData(data, expired); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + getPostDetailById(id: number) { |
| 51 | + return this.postApi.getById(id); |
| 52 | + } |
| 53 | + |
| 54 | + createPost(data: Post, loading = true) { |
| 55 | + this.postStore.setLoading(loading); |
| 56 | + this.postApi |
| 57 | + .addPost(data) |
| 58 | + .pipe( |
| 59 | + catchError((error) => { |
| 60 | + console.log("create-post", error); |
| 61 | + return throwError(() => error); |
| 62 | + }), |
| 63 | + finalize(() => { |
| 64 | + this.postStore.setLoading(false); |
| 65 | + }), |
| 66 | + ) |
| 67 | + .subscribe((post) => { |
| 68 | + this.postStore.addNewData(post); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + updatePost(id: number, data: Partial<Post>, loading = true) { |
| 73 | + this.postStore.setLoading(loading); |
| 74 | + this.postApi |
| 75 | + .updatePost(id, data) |
| 76 | + .pipe( |
| 77 | + catchError((error) => { |
| 78 | + console.log("update-post", error); |
| 79 | + return throwError(() => error); |
| 80 | + }), |
| 81 | + finalize(() => { |
| 82 | + this.postStore.setLoading(false); |
| 83 | + }), |
| 84 | + ) |
| 85 | + .subscribe((post) => { |
| 86 | + this.postStore.updateDataById(id, post); |
| 87 | + }); |
46 | 88 | } |
47 | 89 |
|
48 | | - getPostById(id: number) { |
49 | | - return this.PostApi.getById(id); |
| 90 | + deletePostById(id: number, loading = true) { |
| 91 | + this.postStore.setLoading(loading); |
| 92 | + this.postApi |
| 93 | + .deletePost(id) |
| 94 | + .pipe( |
| 95 | + catchError((error) => { |
| 96 | + console.log("delete-post", error); |
| 97 | + return throwError(() => error); |
| 98 | + }), |
| 99 | + finalize(() => { |
| 100 | + this.postStore.setLoading(false); |
| 101 | + }), |
| 102 | + ) |
| 103 | + .subscribe(() => { |
| 104 | + this.postStore.deleteDataById(id); |
| 105 | + }); |
50 | 106 | } |
51 | 107 | } |
0 commit comments