Created
October 18, 2021 13:32
-
-
Save tsh-code/1de9cfeb7c2511f96b41e36a4f8ed749 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Request, Response } from "express"; | |
import { celebrate, Joi } from "celebrate"; | |
import { CommandBus } from "@tshio/command-bus"; | |
import { CreateUserCommand } from "../commands/create-user.command"; | |
import { Action } from "../../../../shared/http/types"; | |
export interface CreateUserActionDependencies { | |
commandBus: CommandBus; | |
} | |
export const createUserActionValidation = celebrate( | |
{ | |
body: Joi.object().keys({ | |
name: Joi.string().required(), | |
}), | |
}, | |
{ abortEarly: false } | |
); | |
class CreateUserAction implements Action { | |
constructor(private dependencies: CreateUserActionDependencies) {} | |
async invoke({ body }: Request, res: Response) { | |
const result = await this.dependencies.commandBus.execute( | |
new CreateUserCommand({ | |
name: body.name, | |
}) | |
); | |
res.json(result); | |
} | |
} | |
export default CreateUserAction; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment