Skip to content

Instantly share code, notes, and snippets.

View Miczeq22's full-sized avatar
💚
👨🏻‍💻

Mikołaj Wargowski Miczeq22

💚
👨🏻‍💻
  • ChannelBuddy PRO
  • Katowice, Poland
View GitHub Profile
export const resolvers = {
Mutation: {
addCardToDeck,
removeCardFromDeck,
},
Query: {
getCardsForDeck,
},
};
const registerCustomerAction = (
payload: RegisterCustomerPayload,
context: ApplicationContext
) => context.commandBus.execute(new RegisterCustomerCommand(payload));
class CommandBus {
constructor(private readonly handlers: CommandHandler<any>[]) {}
public async execute(command: Command<unknown>) {
const handler = this.handlers.find(
(existingHandler) => existingHandler.type === command.type
);
if (!handler) {
throw new Error(`Handler for command: ${command.type} does not exist.`);
interface GetUserEnrolledDecksQueryHandlerDependencies {
enrolledDeckRepository: EnrolledDeckRepository;
deckElasticView: ElasticView<Deck>;
storageClient: StorageClient;
}
export class GetUserEnrolledDecksQueryHandler extends QueryHandler<
GetUserEnrolledDecks,
GetUserEnrolledDecksQueryResult
> {
abstract class CommandHandler<T extends Command<unknown>> {
constructor(public readonly type: string) {}
public abstract async execute(command: T);
}
interface Dependencies {
customerRepository: CustomerRepository;
}
class RegisterCustomerCommandHandler extends CommandHandler<
RegisterCustomerCommand
> {
constructor(private readonly dependencies: Dependencies) {
super(REGISTER_CUSTOMER_COMMAND_TYPE);
}
interface RegisterCustomerPayload {
email: string;
password: string;
name: string;
}
export const REGISTER_CUSTOMER_COMMAND_TYPE = "customer/register";
class RegisterCustomerCommand extends Command<
RegisterCustomerPayload
abstract class Command<T> {
constructor(public readonly type: string, public readonly payload: T) {}
}
function getOrCreateProduct(data: ProductData) {
let existingProduct = productRepository.findByAttributes(data);
if (!existingProduct) {
existingProduct = productRepository.save(data);
}
return existingProduct;
}
abstract class CommandHandler<T extends Command<unknown>> {
constructor(public readonly type: string) {}
public abstract async execute(command: T);
}