Skip to content

Commit

Permalink
feat(core): update record command & handler
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Jan 3, 2023
1 parent d2b4ea2 commit d4668d0
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 9 deletions.
2 changes: 2 additions & 0 deletions apps/backend/src/modules/table/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SetFieldWidthCommandHandler } from './set-field-width.command.handler'
import { SetFiltersCommandHandler } from './set-filters.command.handler'
import { SetKanbanFieldCommandHandler } from './set-kanban-field.command.handler'
import { SwitchDisplayTypeCommandHandler } from './switch-display-type.command.handler'
import { UpdateRecordCommandHandler } from './update-record.command.handler'

export const commandHandlers = [
CreateTableCommandHandler,
Expand All @@ -26,4 +27,5 @@ export const commandHandlers = [
EditTableCommandHandler,
ReorderOptionsCommandHandler,
SwitchDisplayTypeCommandHandler,
UpdateRecordCommandHandler,
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {
IRecordRepository,
ITableRepository,
UpdateRecordCommand,
UpdateRecordCommandHandler as DomainHandler,
} from '@egodb/core'
import type { ICommandHandler } from '@nestjs/cqrs'
import { CommandHandler } from '@nestjs/cqrs'
import { InjectRecordReposiory, InjectTableReposiory } from '../adapters'

@CommandHandler(UpdateRecordCommand)
export class UpdateRecordCommandHandler extends DomainHandler implements ICommandHandler<UpdateRecordCommand> {
constructor(
@InjectTableReposiory()
protected readonly tableRepo: ITableRepository,

@InjectRecordReposiory()
protected readonly recordRepo: IRecordRepository,
) {
super(tableRepo, recordRepo)
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import * as z from 'zod'
import { createFieldValueSchema, fieldNameSchema } from '../../field'
import { mutateRecordValueSchema } from '../../record/record.schema'
import { recordIdSchema } from '../../record/value-objects/record-id.vo'
import { tableIdSchema } from '../../value-objects'

export const createRecordCommandInput = z.object({
tableId: tableIdSchema,
id: recordIdSchema.optional(),
value: z
.array(
z.object({
name: fieldNameSchema,
value: createFieldValueSchema,
}),
)
.min(1),
value: mutateRecordValueSchema,
})
export type ICreateRecordInput = z.infer<typeof createRecordCommandInput>
1 change: 1 addition & 0 deletions packages/core/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './set-field-width'
export * from './set-filters'
export * from './set-kanban-field'
export * from './switch-display-type'
export * from './update-record'
3 changes: 3 additions & 0 deletions packages/core/commands/update-record/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './update-record.comand'
export * from './update-record.command.handler'
export * from './update-record.command.input'
17 changes: 17 additions & 0 deletions packages/core/commands/update-record/update-record.comand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { CommandProps } from '@egodb/domain'
import { Command } from '@egodb/domain'
import type { IMutateRecordValueSchema } from '../../record/record.schema'
import type { IUpdateRecordCommandInput } from './update-record.command.input'

export class UpdateRecordCommand extends Command implements IUpdateRecordCommandInput {
readonly id?: string
readonly tableId: string
readonly value: IMutateRecordValueSchema

constructor(props: CommandProps<IUpdateRecordCommandInput>) {
super(props)
this.tableId = props.tableId
this.id = props.id
this.value = props.value
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { ICommandHandler } from '@egodb/domain'
import type { IRecordRepository } from '../../record/repository'
import type { ITableRepository } from '../../table.repository'
import type { UpdateRecordCommand } from './update-record.comand'

export class UpdateRecordCommandHandler implements ICommandHandler<UpdateRecordCommand, void> {
constructor(protected readonly tableRepo: ITableRepository, protected readonly recordRepo: IRecordRepository) {}

async execute(command: UpdateRecordCommand): Promise<void> {
const table = (await this.tableRepo.findOneById(command.tableId)).unwrap()

// const record = table.createRecord(command)
// await this.recordRepo.insert(record)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as z from 'zod'
import { mutateRecordValueSchema } from '../../record/record.schema'
import { recordIdSchema } from '../../record/value-objects/record-id.vo'
import { tableIdSchema } from '../../value-objects'

export const updateRecordCommandInput = z.object({
tableId: tableIdSchema,
id: recordIdSchema.optional(),
value: mutateRecordValueSchema,
})
export type IUpdateRecordCommandInput = z.infer<typeof updateRecordCommandInput>
13 changes: 13 additions & 0 deletions packages/core/record/record.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { z } from 'zod'
import { createFieldValueSchema } from '../field'
import { fieldNameSchema } from '../field/value-objects/field-name.vo'

export const mutateRecordValueSchema = z
.array(
z.object({
name: fieldNameSchema,
value: createFieldValueSchema,
}),
)
.min(1)
export type IMutateRecordValueSchema = z.infer<typeof mutateRecordValueSchema>

0 comments on commit d4668d0

Please sign in to comment.