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
5 changes: 5 additions & 0 deletions lib/db/db-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ const initializer = combine(databaseSchema.parse({}), (set) => ({
idCounter: state.idCounter + 1,
}))
},
removeThing: (thing_id: string) => {
set((state) => ({
things: state.things.filter((t) => t.thing_id !== thing_id),
}))
},
}))
16 changes: 16 additions & 0 deletions routes/things/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["POST"],
jsonBody: z.object({
thing_id: z.string(),
}),
jsonResponse: z.object({
ok: z.boolean(),
}),
})(async (req, ctx) => {
const { thing_id } = await req.json()
ctx.db.removeThing(thing_id)
return ctx.json({ ok: true })
})
25 changes: 25 additions & 0 deletions routes/things/get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { withRouteSpec } from "lib/middleware/with-winter-spec"
import { z } from "zod"

export default withRouteSpec({
methods: ["GET"],
queryParams: z.object({
thing_id: z.string(),
}),
jsonResponse: z.object({
thing: z.object({
thing_id: z.string(),
name: z.string(),
description: z.string(),
}),
}),
})((req, ctx) => {
const { thing_id } = req.query
const thing = ctx.db.things.find((t) => t.thing_id === thing_id)

if (!thing) {
return ctx.json({ error: "Thing not found" }, { status: 404 })
}

return ctx.json({ thing })
})
32 changes: 32 additions & 0 deletions tests/routes/things/delete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("delete a thing", async () => {
const { axios } = await getTestServer()

await axios.post("/things/create", {
name: "Thing1",
description: "Thing1 Description",
})

const { data: listBefore } = await axios.get("/things/list")
expect(listBefore.things).toHaveLength(1)

await axios.post("/things/delete", {
thing_id: "0",
})

const { data: listAfter } = await axios.get("/things/list")
expect(listAfter.things).toHaveLength(0)
})

test("delete a non-existent thing does not error", async () => {
const { axios } = await getTestServer()

const res = await axios.post("/things/delete", {
thing_id: "999",
})

expect(res.status).toBe(200)
expect(res.data.ok).toBe(true)
})
29 changes: 29 additions & 0 deletions tests/routes/things/get.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getTestServer } from "tests/fixtures/get-test-server"
import { test, expect } from "bun:test"

test("get a thing by id", async () => {
const { axios } = await getTestServer()

await axios.post("/things/create", {
name: "Thing1",
description: "Thing1 Description",
})

const { data } = await axios.get("/things/get?thing_id=0")

expect(data.thing).toEqual({
thing_id: "0",
name: "Thing1",
description: "Thing1 Description",
})
})

test("get a thing that doesn't exist returns 404", async () => {
const { axios } = await getTestServer()

try {
await axios.get("/things/get?thing_id=999")
} catch (e: any) {
expect(e.status).toBe(404)
}
})