Skip to content

Commit 06efbce

Browse files
committed
feature: get by code route added
1 parent aefb7db commit 06efbce

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

imports/api/context/initRoutes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createGetByIdRoute } from '../decorators/createGetByIdRoute'
2+
import { createGetByCodeRoute } from '../decorators/createGetByCodeRoute'
23
import { createRoutes } from '../factories/createRoute'
34

45
export const initRoutes = context => {
@@ -8,6 +9,10 @@ export const initRoutes = context => {
89
context.routes.byId.run = createGetByIdRoute(context)
910
}
1011

12+
if (context.routes.byCode) {
13+
context.routes.byCode.run = createGetByCodeRoute(context)
14+
}
15+
1116
const routes = Object.values(context.routes)
1217
createRoutes(routes)
1318
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Implementation for a generic route to get
3+
* a context document by its code value
4+
* @param context {object} to context for which this route is to be created
5+
*/
6+
export const createGetByCodeRoute = context => async function () {
7+
const api = this
8+
const { shortCode } = api.data()
9+
console.debug('Get by code route for context', context.name, 'shortCode', shortCode, await context.collection().find({ shortCode }).fetchAsync())
10+
return context.collection().findOneAsync({ shortCode })
11+
}

imports/api/decorators/createGetByIdRoute.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* @param context {object} to context for which this route is to be created
55
*/
66
export const createGetByIdRoute = context => async function () {
7-
console.debug('createGetByIdRoute', context.name)
87
const api = this
98
const { _id } = api.data()
109
return context.collection().findOneAsync(_id)

0 commit comments

Comments
 (0)