Skip to content

Commit bd548eb

Browse files
feat: adiciona método de busca por slug na api-storefront
1 parent 439e74b commit bd548eb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/modules/app/AppRepositoryGql.ts

+8
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,12 @@ export class AppRepositoryGql {
1010

1111
return app
1212
}
13+
14+
static async getBySlug(slug: string, fields?: AppFields[]): Promise<App> {
15+
const appQuery = new AppQueries(fields)
16+
const getAppQuery: string = appQuery.getApp()
17+
const { app }: AppResponse = await getClient().query(getAppQuery, { filter: { slug: slug } })
18+
19+
return app
20+
}
1321
}

src/modules/app/AppRepositoryJson.ts

+18
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,22 @@ export class AppRepositoryJson {
1818

1919
return mock
2020
}
21+
22+
static async getBySlug(slug: string, fields?: AppFields[]) {
23+
const mock = shop_ctx.mock?.apps || {}
24+
25+
if (slug != mock.slug) {
26+
throw new Error('apps_not_found')
27+
}
28+
29+
const isFieldNotSelected = entry => !fields.includes(entry)
30+
31+
const deleteFieldIfNecessary = entry => {
32+
isFieldNotSelected(entry) && delete mock[entry]
33+
}
34+
35+
fields && Object.keys(mock).forEach(deleteFieldIfNecessary)
36+
37+
return mock
38+
}
2139
}

src/modules/app/AppService.ts

+11
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@ export class AppService {
1616
throw new Error(error?.message)
1717
}
1818
}
19+
20+
static async getBySlug(slug: string, fields?: AppFields[]): Promise<App> {
21+
try {
22+
const result = await Repository().getBySlug(slug, fields)
23+
BroadcastService.emit('Apps', result)
24+
25+
return result
26+
} catch (error) {
27+
throw new Error(error?.message)
28+
}
29+
}
1930
}

0 commit comments

Comments
 (0)