Skip to content

Commit

Permalink
feat(core): add get record query handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenqin Nee authored and Chenqin Nee committed Dec 15, 2022
1 parent 3d10a49 commit 628108e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/core/queries/get-record/get-record.query.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { IQueryHandler } from '@egodb/domain'
import type { IRecordQueryModel } from '../../record'
import { WithRecordId } from '../../record'
import type { ITableRepository } from '../../table.repository'
import type { GetRecordQuery } from './get-record.query'
import type { IGetRecordOutput } from './get-record.query.interface'

export class GetRecordQueryHandler implements IQueryHandler<GetRecordQuery, IGetRecordOutput> {
constructor(protected readonly tableRepo: ITableRepository, protected readonly rm: IRecordQueryModel) {}

async execute(query: GetRecordQuery): Promise<IGetRecordOutput> {
const spec = WithRecordId.fromString(query.id)

const record = (await this.rm.findOne(spec)).unwrap()

return record
}
}
6 changes: 6 additions & 0 deletions packages/core/queries/get-record/get-record.query.input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as z from 'zod'
import { recordIdSchema } from '../../record'

export const getRecordQueryInput = z.object({
id: recordIdSchema,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type * as z from 'zod'
import type { getRecordQueryInput } from './get-record.query.input'
import type { getRecordQueryOutput } from './get-record.query.output'

export type IGetRecordQuery = z.infer<typeof getRecordQueryInput>
export type IGetRecordOutput = z.infer<typeof getRecordQueryOutput>
3 changes: 3 additions & 0 deletions packages/core/queries/get-record/get-record.query.output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { queryRecordSchema } from '../../record'

export const getRecordQueryOutput = queryRecordSchema.optional()
10 changes: 10 additions & 0 deletions packages/core/queries/get-record/get-record.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Query } from '@egodb/domain'
import type { IGetRecordQuery } from './get-record.query.interface'

export class GetRecordQuery extends Query implements IGetRecordQuery {
readonly id: string
constructor(query: IGetRecordQuery) {
super()
this.id = query.id
}
}
5 changes: 5 additions & 0 deletions packages/core/queries/get-record/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './get-record.query'
export * from './get-record.query.handler'
export * from './get-record.query.input'
export * from './get-record.query.interface'
export * from './get-record.query.output'
1 change: 1 addition & 0 deletions packages/core/queries/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './get-record'
export * from './get-records'
export * from './get-table'
export * from './get-tables'
11 changes: 11 additions & 0 deletions packages/trpc/router/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
CreateRecordCommand,
createRecordCommandInput,
createRecordCommandOutput,
GetRecordQuery,
getRecordQueryInput,
getRecordQueryOutput,
GetRecordsQuery,
getRecordsQueryInput,
getRecordsQueryOutput,
Expand All @@ -24,6 +27,14 @@ export const createRecordRouter =
const cmd = new CreateRecordCommand(input)
return commandBus.execute(cmd)
}),
get: procedure
.meta({ openapi: { method: 'GET', path: '/record.get', tags } })
.input(getRecordQueryInput)
.output(getRecordQueryOutput)
.query(({ input }) => {
const query = new GetRecordQuery(input)
return queryBus.execute(query)
}),
list: procedure
.meta({ openapi: { method: 'GET', path: '/record.list', tags } })
.input(getRecordsQueryInput)
Expand Down

0 comments on commit 628108e

Please sign in to comment.