Skip to content

Commit 70a2176

Browse files
committed
implement getByKey
1 parent 564c03c commit 70a2176

File tree

4 files changed

+103
-4
lines changed

4 files changed

+103
-4
lines changed

src/repositories/abstract.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export default abstract class AbstractRepository {
4949
return this._storage.get(projectKey, this.getTypeId(), id, params)
5050
}
5151

52+
getByKey(
53+
projectKey: string,
54+
key: string,
55+
params: GetParams = {}
56+
): BaseResource | null {
57+
return this._storage.getByKey(projectKey, this.getTypeId(), key, params)
58+
}
59+
5260
delete(
5361
projectKey: string,
5462
id: string,

src/services/abstract.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { QueryParams } from './../repositories/abstract'
12
import { Update } from '@commercetools/platform-sdk'
23
import { ParsedQs } from 'qs'
34
import { Request, Response, Router } from 'express'
@@ -21,15 +22,15 @@ export default abstract class AbstractService {
2122
this.extraRoutes(router)
2223

2324
router.get('/', this.get.bind(this))
25+
router.get('/key=:key', this.getWithKey.bind(this)) // same thing goes for the key routes
2426
router.get('/:id', this.getWithId.bind(this))
25-
router.get('/key=:key', this.getWithKey.bind(this))
2627

27-
router.delete('/:id', this.deletewithId.bind(this))
2828
router.delete('/key=:key', this.deletewithKey.bind(this))
29+
router.delete('/:id', this.deletewithId.bind(this))
2930

3031
router.post('/', this.post.bind(this))
31-
router.post('/:id', this.postWithId.bind(this))
3232
router.post('/key=:key', this.postWithKey.bind(this))
33+
router.post('/:id', this.postWithId.bind(this))
3334

3435
parent.use(`/${basePath}`, router)
3536
}
@@ -47,7 +48,13 @@ export default abstract class AbstractService {
4748
}
4849

4950
getWithKey(request: Request, response: Response) {
50-
return response.status(500).send('Not implemented')
51+
const result = this.repository.getByKey(
52+
request.params.projectKey,
53+
request.params['key'],
54+
{ expand: this._parseParam(request.query.expand) }
55+
)
56+
if (!result) return response.status(404).send()
57+
return response.status(200).send(result)
5158
}
5259

5360
deletewithId(request: Request, response: Response) {

src/services/store.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import supertest from 'supertest'
2+
import { CommercetoolsMock } from '../index'
3+
4+
const ctMock = new CommercetoolsMock()
5+
6+
describe('Store', () => {
7+
beforeAll(() => {
8+
ctMock.start()
9+
})
10+
11+
afterEach(() => {
12+
ctMock.clear()
13+
})
14+
15+
afterAll(() => {
16+
ctMock.stop()
17+
})
18+
19+
test('Get store by key', async () => {
20+
ctMock.project('dummy').add('store', {
21+
id: 'fake-store',
22+
version: 1,
23+
createdAt: '',
24+
lastModifiedAt: '',
25+
key: 'STOREKEY',
26+
distributionChannels: [],
27+
})
28+
29+
const response = await supertest(ctMock.app).get(
30+
`/dummy/stores/key=STOREKEY`
31+
)
32+
33+
expect(response.status).toBe(200)
34+
expect(response.body).toEqual({
35+
createdAt: '',
36+
distributionChannels: [],
37+
id: 'fake-store',
38+
key: 'STOREKEY',
39+
lastModifiedAt: '',
40+
version: 1,
41+
})
42+
})
43+
44+
test('Get store by 404 when not found by key', async () => {
45+
ctMock.project('dummy').add('store', {
46+
id: 'fake-store',
47+
version: 1,
48+
createdAt: '',
49+
lastModifiedAt: '',
50+
key: 'STOREKEY',
51+
distributionChannels: [],
52+
})
53+
54+
const response = await supertest(ctMock.app).get(
55+
`/dummy/stores/key=DOESNOTEXIST`
56+
)
57+
58+
expect(response.status).toBe(404)
59+
})
60+
})

src/storage.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export abstract class AbstractStorage {
6161
params: GetParams
6262
): ResourceMap[RepositoryTypes] | null
6363

64+
abstract getByKey<RepositoryTypes extends keyof ResourceMap>(
65+
projectKey: string,
66+
typeId: RepositoryTypes,
67+
key: string,
68+
params: GetParams
69+
): ResourceMap[RepositoryTypes] | null
70+
6471
abstract delete(
6572
projectKey: string,
6673
typeId: RepositoryTypes,
@@ -163,6 +170,23 @@ export class InMemoryStorage extends AbstractStorage {
163170
return null
164171
}
165172

173+
getByKey<RepositoryTypes extends keyof ResourceMap>(
174+
projectKey: string,
175+
typeId: RepositoryTypes,
176+
key: string,
177+
params: GetParams = {}
178+
): ResourceMap[RepositoryTypes] | null {
179+
const store = this.forProjectKey(projectKey)[typeId]
180+
if (!store) {
181+
throw new Error('No type')
182+
}
183+
184+
const resources: any[] = Array.from(store.values())
185+
const resource = resources.find(e => e.key === key)
186+
if (params.expand) return this.expand(projectKey, resource, params.expand)
187+
return resource
188+
}
189+
166190
delete(
167191
projectKey: string,
168192
typeId: RepositoryTypes,

0 commit comments

Comments
 (0)