1
1
import fetch from '@/utils/fetch-client' ;
2
2
import { PostCategories , PostCategory , PostCategoryAddDto } from '../models/post-category' ;
3
3
import { globalContext } from './global-state' ;
4
+ import { URLSearchParams } from 'url' ;
4
5
5
6
export class PostCategoryService {
6
7
private static _instance : PostCategoryService ;
7
8
8
- private _cached ?: PostCategories ;
9
+ private _cache ?: Map < number , PostCategories > ;
9
10
10
11
private constructor ( ) { }
11
12
@@ -19,19 +20,51 @@ export class PostCategoryService {
19
20
ids = ids . filter ( x => x > 0 ) ;
20
21
if ( ids . length <= 0 ) return [ ] ;
21
22
22
- const categories = await this . fetchCategories ( ! useCache ) ;
23
+ const categories = await this . listCategories ( ! useCache ) ;
23
24
return categories . filter ( ( { categoryId } ) => ids . includes ( categoryId ) ) ;
24
25
}
25
26
26
- async fetchCategories ( forceRefresh = false ) : Promise < PostCategories > {
27
- if ( this . _cached && ! forceRefresh ) return this . _cached ;
28
-
29
- const res = await fetch ( `${ globalContext . config . apiBaseUrl } /api/category/blog/1/edit` ) ;
27
+ listCategories ( ) : Promise < PostCategories > ;
28
+ listCategories ( {
29
+ forceRefresh = false ,
30
+ parentId = - 1 ,
31
+ } : {
32
+ forceRefresh ?: boolean | null ;
33
+ parentId ?: number ;
34
+ } ) : Promise < PostCategories > ;
35
+ listCategories ( forceRefresh : boolean ) : Promise < PostCategories > ;
36
+ async listCategories (
37
+ option : boolean | { forceRefresh ?: boolean | null ; parentId ?: number } = { }
38
+ ) : Promise < PostCategories > {
39
+ const parentId = typeof option === 'object' ? option . parentId ?? - 1 : - 1 ;
40
+ const shouldForceRefresh =
41
+ option === true || ( typeof option === 'object' ? option . forceRefresh ?? false : false ) ;
42
+ const map = ( this . _cache ??= new Map < number , PostCategories > ( ) ) ;
43
+ const cachedCategories = map . get ( parentId ) ;
44
+ if ( cachedCategories && ! shouldForceRefresh ) return cachedCategories ;
45
+
46
+ const res = await fetch (
47
+ `${ globalContext . config . apiBaseUrl } /api/v2/blog-category-types/1/categories?${ new URLSearchParams ( [
48
+ [ 'parent' , parentId <= 0 ? '' : `${ parentId } ` ] ,
49
+ ] ) . toString ( ) } `
50
+ ) ;
30
51
if ( ! res . ok ) throw Error ( `Failed to fetch post categories\n${ res . status } \n${ await res . text ( ) } ` ) ;
31
52
32
- const categories = < PostCategories > await res . json ( ) ;
33
- this . _cached = categories . map ( x => Object . assign ( new PostCategory ( ) , x ) ) ;
34
- return this . _cached ;
53
+ let { categories } = < { parent ?: PostCategory | null ; categories : PostCategories } > await res . json ( ) ;
54
+ categories = categories . map ( x => Object . assign ( new PostCategory ( ) , x ) ) ;
55
+ map . set ( parentId , categories ) ;
56
+ return categories ;
57
+ }
58
+
59
+ async find ( id : number ) {
60
+ const res = await fetch (
61
+ `${ globalContext . config . apiBaseUrl } /api/v2/blog-category-types/1/categories?${ new URLSearchParams ( [
62
+ [ 'parent' , id <= 0 ? '' : `${ id } ` ] ,
63
+ ] ) . toString ( ) } `
64
+ ) ;
65
+ const { parent } = < { parent ?: PostCategory | null ; categories : PostCategories } > await res . json ( ) ;
66
+
67
+ return Object . assign ( new PostCategory ( ) , parent ) ;
35
68
}
36
69
37
70
async newCategory ( categoryAddDto : PostCategoryAddDto ) {
@@ -63,7 +96,7 @@ export class PostCategoryService {
63
96
}
64
97
65
98
clearCache ( ) {
66
- this . _cached = undefined ;
99
+ this . _cache = undefined ;
67
100
}
68
101
}
69
102
0 commit comments