Skip to content

Commit e58786a

Browse files
committed
i <3 copilot
1 parent b1e6567 commit e58786a

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/routes/events.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ const getEventSchema = requestSchema.extend({
5757
id: z.string(),
5858
});
5959

60-
// export type EventGetResponse = z.infer<typeof getEventSchema>;
61-
// const getEventJsonSchema = zodToJsonSchema(getEventSchema);
60+
export type EventGetResponse = z.infer<typeof getEventSchema>;
61+
const getEventJsonSchema = zodToJsonSchema(getEventSchema);
6262

6363
const getEventsSchema = z.array(getEventSchema);
6464
export type EventsGetResponse = z.infer<typeof getEventsSchema>;
@@ -111,6 +111,46 @@ const eventsPlugin: FastifyPluginAsync = async (fastify, _options) => {
111111
}
112112
},
113113
);
114+
type EventGetRequest = {
115+
Params: { id: string };
116+
Querystring: undefined;
117+
Body: undefined;
118+
};
119+
fastify.get<EventGetRequest>(
120+
"/:id",
121+
{
122+
schema: {
123+
response: { 200: getEventJsonSchema },
124+
},
125+
},
126+
async (request: FastifyRequest<EventGetRequest>, reply) => {
127+
const id = request.params.id;
128+
try {
129+
const response = await dynamoClient.send(
130+
new ScanCommand({
131+
TableName: genericConfig.DynamoTableName,
132+
FilterExpression: "#id = :id",
133+
ExpressionAttributeNames: {
134+
"#id": "id",
135+
},
136+
ExpressionAttributeValues: marshall({ ":id": id }),
137+
}),
138+
);
139+
const items = response.Items?.map((item) => unmarshall(item));
140+
if (items?.length !== 1) {
141+
throw new Error("Event not found");
142+
}
143+
reply.send(items[0]);
144+
} catch (e: unknown) {
145+
if (e instanceof Error) {
146+
request.log.error("Failed to get from DynamoDB: " + e.toString());
147+
}
148+
throw new DatabaseFetchError({
149+
message: "Failed to get event from Dynamo table.",
150+
});
151+
}
152+
},
153+
);
114154
type EventsGetRequest = {
115155
Body: undefined;
116156
Querystring?: EventsGetQueryParams;

0 commit comments

Comments
 (0)