Files
StevenandGitHub 4466ba436b chore(examples): use default prettier for examples/templates (#60530)
## Description
This PR ensures that the default prettier config is used for examples
and templates.

This config is compatible with `prettier@3` as well (upgrading prettier
is bigger change that can be a future PR).

## Changes
- Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will
be needed upgrading to prettier@3)
- Added `examples/.prettierrc.json` with default config (this will
change every example)
- Added `packages/create-next-app/templates/.prettierrc.json` with
default config (this will change every template)

## Related

- Fixes #54402
- Closes #54409
2024-01-11 16:01:44 -07:00

84 lines
2.4 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import { TodoItem } from "../../../db/models/todoItems";
import tigrisDb from "../../../lib/tigris";
type Data = {
result?: TodoItem;
error?: string;
};
async function handleGet(
req: NextApiRequest,
res: NextApiResponse<Data>,
itemId: number,
) {
try {
const itemsCollection = tigrisDb.getCollection<TodoItem>(TodoItem);
const item = await itemsCollection.findOne({ filter: { id: itemId } });
if (!item) {
res.status(404).json({ error: "No item found" });
} else {
res.status(200).json({ result: item });
}
} catch (err) {
const error = err as Error;
res.status(500).json({ error: error.message });
}
}
async function handlePut(req: NextApiRequest, res: NextApiResponse<Data>) {
try {
const item = JSON.parse(req.body) as TodoItem;
const itemsCollection = tigrisDb.getCollection<TodoItem>(TodoItem);
const updated = await itemsCollection.insertOrReplaceOne(item);
res.status(200).json({ result: updated });
} catch (err) {
const error = err as Error;
res.status(500).json({ error: error.message });
}
}
async function handleDelete(
req: NextApiRequest,
res: NextApiResponse<Data>,
itemId: number,
) {
try {
const itemsCollection = tigrisDb.getCollection<TodoItem>(TodoItem);
const status = (await itemsCollection.deleteOne({ filter: { id: itemId } }))
.status;
if (status === "deleted") {
res.status(200).json({});
} else {
res.status(500).json({ error: `Failed to delete ${itemId}` });
}
} catch (err) {
const error = err as Error;
res.status(500).json({ error: error.message });
}
}
// GET /api/item/[id] -- gets item from collection where id = [id]
// PUT /api/item/[id] {ToDoItem} -- updates the item in collection where id = [id]
// DELETE /api/item/[id] -- deletes the item in collection where id = [id]
export default async function handler(
req: NextApiRequest,
res: NextApiResponse<Data>,
) {
const { id } = req.query;
switch (req.method) {
case "GET":
await handleGet(req, res, Number(id));
break;
case "PUT":
await handlePut(req, res);
break;
case "DELETE":
await handleDelete(req, res, Number(id));
break;
default:
res.setHeader("Allow", ["GET", "PUT", "DELETE"]);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}