Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions lib/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import { z } from "zod"

// When defining your database schema, try to use snake case for column names.
// When defining your database schema, try to use snake case for column names.

export const thingSchema = z.object({
thing_id: z.string(),
name: z.string(),
description: z.string(),
})
export type Thing = z.infer<typeof thingSchema>
export const thingSchema = z.object({
thing_id: z.string(),
name: z.string(),
description: z.string(),
})
export type Thing = z.infer<typeof thingSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>
export const bountySchema = z.object({
bounty_id: z.string(),
issue_url: z.string(),
amount_usd: z.number(),
status: z.enum(["open", "in_progress", "paid"]).default("open"),
created_at: z.string(),
})
export type Bounty = z.infer<typeof bountySchema>

export const paymentSchema = z.object({
payment_id: z.string(),
bounty_id: z.string(),
recipient_username: z.string(),
amount_usd: z.number(),
paid_at: z.string(),
})
export type Payment = z.infer<typeof paymentSchema>

export const databaseSchema = z.object({
idCounter: z.number().default(0),
things: z.array(thingSchema).default([]),
bounties: z.array(bountySchema).default([]),
payments: z.array(paymentSchema).default([]),
})
export type DatabaseSchema = z.infer<typeof databaseSchema>

32 changes: 32 additions & 0 deletions routes/bounties/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"
import { randomUUID } from "crypto"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
issue_url: z.string().url(),
amount_usd: z.number().positive(),
}),
jsonResponse: z.object({
bounty: z.object({
bounty_id: z.string(),
issue_url: z.string(),
amount_usd: z.number(),
status: z.string(),
created_at: z.string(),
}),
}),
})(async (req, ctx) => {
const { issue_url, amount_usd } = await req.json()
const bounty = {
bounty_id: randomUUID(),
issue_url,
amount_usd,
status: "open" as const,
created_at: new Date().toISOString(),
}
ctx.db.addBounty(bounty)
return ctx.json({ bounty })
})

18 changes: 18 additions & 0 deletions routes/bounties/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
jsonResponse: z.object({
bounties: z.array(z.object({
bounty_id: z.string(),
issue_url: z.string(),
amount_usd: z.number(),
status: z.string(),
created_at: z.string(),
})),
}),
})(async (req, ctx) => {
return ctx.json({ bounties: ctx.db.getBounties() })
})

33 changes: 33 additions & 0 deletions routes/payments/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"
import { randomUUID } from "crypto"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
bounty_id: z.string(),
recipient_username: z.string(),
amount_usd: z.number().positive(),
}),
jsonResponse: z.object({
payment: z.object({
payment_id: z.string(),
bounty_id: z.string(),
recipient_username: z.string(),
amount_usd: z.number(),
paid_at: z.string(),
}),
}),
})(async (req, ctx) => {
const { bounty_id, recipient_username, amount_usd } = await req.json()
const payment = {
payment_id: randomUUID(),
bounty_id,
recipient_username,
amount_usd,
paid_at: new Date().toISOString(),
}
ctx.db.addPayment(payment)
return ctx.json({ payment })
})