-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/elct9620/GGJ2025
- Loading branch information
Showing
7 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { inject, injectable } from "tsyringe-neo"; | ||
|
||
import { EmailController, EmailParams } from "./EmailController"; | ||
import { SESv2Client } from "@aws-sdk/client-sesv2"; | ||
import { SesEmailPresenter } from "@presenter/SesEmailPresenter"; | ||
import { KvCityRepository } from "@repository/KvCityRepository"; | ||
import { TaklWithJackUsecase } from "@usecase/TalkWithJackUsecase"; | ||
|
||
@injectable() | ||
export class NpcJackController extends EmailController { | ||
public static readonly SenderName: string = "Jack"; | ||
|
||
constructor( | ||
@inject(SESv2Client) private readonly ses: SESv2Client, | ||
@inject(KvCityRepository) private readonly cityRepository: KvCityRepository, | ||
) { | ||
super(); | ||
} | ||
|
||
protected async onMessage(params: EmailParams): Promise<void> { | ||
var sender = params.to; | ||
if (!sender.includes("<")) { | ||
sender = `${NpcJackController.SenderName} <${sender}>`; | ||
} | ||
|
||
const presenter = new SesEmailPresenter(this.ses, { | ||
sender, | ||
recipients: [params.from], | ||
messageId: params.messageId, | ||
references: params.references, | ||
subject: params.subject, | ||
}); | ||
const usecase = new TaklWithJackUsecase(presenter, this.cityRepository); | ||
|
||
await usecase.execute(params.userId, params.body); | ||
await presenter.render(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
import { handle } from "hono/cloudflare-pages"; | ||
import { CityEvent, CityEventType, CityInitializedEvent } from "./CityEvent"; | ||
|
||
type EventHandler<T extends CityEvent> = (event: T) => void; | ||
|
||
export class City { | ||
constructor(public readonly id: string) {} | ||
private _events: CityEvent[] = []; | ||
|
||
constructor(public readonly id: string) { | ||
this.apply(new CityInitializedEvent({})); | ||
} | ||
|
||
public apply(event: CityEvent): void { | ||
const handlerName = `on${event.type}` as keyof this; | ||
const handler = this[handlerName] as EventHandler<typeof event>; | ||
if (!handler) { | ||
throw new Error(`Missing event handler: ${String(handlerName)}`); | ||
} | ||
|
||
handler(event); | ||
this._events.push(event); | ||
} | ||
|
||
public get events(): CityEvent[] { | ||
return this._events.map((event) => ({ ...event }) as CityEvent); | ||
} | ||
|
||
private onCityInitializedEvent(event: CityInitializedEvent): void { | ||
// Do nothing | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
export enum CityEventType { | ||
CityInitializedEvent = "CityInitializedEvent", | ||
} | ||
|
||
abstract class Event { | ||
constructor( | ||
public readonly type: CityEventType, | ||
public readonly _payload: Record<string, any>, | ||
public readonly createdAt: Date = new Date(), | ||
) {} | ||
|
||
get payload(): Record<string, any> { | ||
return { ...this._payload }; | ||
} | ||
} | ||
|
||
export class CityInitializedEvent extends Event { | ||
constructor(payload: Record<string, any>) { | ||
super(CityEventType.CityInitializedEvent, payload); | ||
} | ||
} | ||
|
||
export type CityEvent = CityInitializedEvent; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { CityRepository, EmailPresenter } from "./interface"; | ||
|
||
export class TaklWithJackUsecase { | ||
constructor( | ||
private readonly presenter: EmailPresenter, | ||
private readonly cityRepository: CityRepository, | ||
) {} | ||
|
||
async execute(userId: string, message: string): Promise<void> { | ||
await this.cityRepository.destroy(userId); | ||
this.presenter.addText("Hello, I'm Jack."); | ||
} | ||
} |
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