forked from denoland/std
-
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.
feat: REST API for items and comments (denoland#441)
This change adds the following REST API endpoints: - `GET /api/items` - `GET /api/items/[id]` - `GET /api/items/[id]/comments` Documentation will come in a follow-up PR. Prerequisite for denoland#438 or denoland#429 Towards denoland#439
- Loading branch information
Showing
8 changed files
with
201 additions
and
73 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
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,22 @@ | ||
import { Handlers, Status } from "$fresh/server.ts"; | ||
import { collectValues, getItem, listCommentsByItem } from "@/utils/db.ts"; | ||
import { getCursor } from "@/utils/pagination.ts"; | ||
|
||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
export const handler: Handlers = { | ||
async GET(req, ctx) { | ||
const itemId = ctx.params.id; | ||
const item = await getItem(itemId); | ||
if (item === null) return new Response(null, { status: Status.NotFound }); | ||
|
||
const url = new URL(req.url); | ||
const iter = listCommentsByItem(itemId, { | ||
cursor: getCursor(url), | ||
limit: 10, | ||
// Newest to oldest | ||
reverse: true, | ||
}); | ||
const comments = await collectValues(iter); | ||
return Response.json({ comments, cursor: iter.cursor }); | ||
}, | ||
}; |
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,12 @@ | ||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
import { type Handlers, Status } from "$fresh/server.ts"; | ||
import { getItem } from "@/utils/db.ts"; | ||
|
||
export const handler: Handlers = { | ||
async GET(_req, ctx) { | ||
const item = await getItem(ctx.params.id); | ||
return item === null | ||
? new Response(null, { status: Status.NotFound }) | ||
: Response.json(item); | ||
}, | ||
}; |
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,17 @@ | ||
import { Handlers } from "$fresh/server.ts"; | ||
import { collectValues, listItemsByTime } from "@/utils/db.ts"; | ||
import { getCursor } from "@/utils/pagination.ts"; | ||
|
||
// Copyright 2023 the Deno authors. All rights reserved. MIT license. | ||
export const handler: Handlers = { | ||
async GET(req) { | ||
const url = new URL(req.url); | ||
const iter = listItemsByTime({ | ||
cursor: getCursor(url), | ||
limit: 10, | ||
reverse: true, | ||
}); | ||
const items = await collectValues(iter); | ||
return Response.json({ items, cursor: iter.cursor }); | ||
}, | ||
}; |
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
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