11import fetch from '@/utils/fetch-client' ;
22import { PostCategories , PostCategory , PostCategoryAddDto } from '../models/post-category' ;
33import { globalContext } from './global-state' ;
4+ import { URLSearchParams } from 'url' ;
45
56export class PostCategoryService {
67 private static _instance : PostCategoryService ;
78
8- private _cached ?: PostCategories ;
9+ private _cache ?: Map < number , PostCategories > ;
910
1011 private constructor ( ) { }
1112
@@ -19,19 +20,51 @@ export class PostCategoryService {
1920 ids = ids . filter ( x => x > 0 ) ;
2021 if ( ids . length <= 0 ) return [ ] ;
2122
22- const categories = await this . fetchCategories ( ! useCache ) ;
23+ const categories = await this . listCategories ( ! useCache ) ;
2324 return categories . filter ( ( { categoryId } ) => ids . includes ( categoryId ) ) ;
2425 }
2526
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+ ) ;
3051 if ( ! res . ok ) throw Error ( `Failed to fetch post categories\n${ res . status } \n${ await res . text ( ) } ` ) ;
3152
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 ) ;
3568 }
3669
3770 async newCategory ( categoryAddDto : PostCategoryAddDto ) {
@@ -63,7 +96,7 @@ export class PostCategoryService {
6396 }
6497
6598 clearCache ( ) {
66- this . _cached = undefined ;
99+ this . _cache = undefined ;
67100 }
68101}
69102
0 commit comments