Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 18, 2021 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsh-code/1de9cfeb7c2511f96b41e36a4f8ed749 to your computer and use it in GitHub Desktop.
Save tsh-code/1de9cfeb7c2511f96b41e36a4f8ed749 to your computer and use it in GitHub Desktop.
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