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

61 lines
1.5 KiB
TypeScript

import { NextApiRequest, NextApiResponse } from "next";
import dbConnect from "../../../lib/dbConnect";
import Pet from "../../../models/Pet";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const {
query: { id },
method,
} = req;
await dbConnect();
switch (method) {
case "GET" /* Get a model by its ID */:
try {
const pet = await Pet.findById(id);
if (!pet) {
return res.status(400).json({ success: false });
}
res.status(200).json({ success: true, data: pet });
} catch (error) {
res.status(400).json({ success: false });
}
break;
case "PUT" /* Edit a model by its ID */:
try {
const pet = await Pet.findByIdAndUpdate(id, req.body, {
new: true,
runValidators: true,
});
if (!pet) {
return res.status(400).json({ success: false });
}
res.status(200).json({ success: true, data: pet });
} catch (error) {
res.status(400).json({ success: false });
}
break;
case "DELETE" /* Delete a model by its ID */:
try {
const deletedPet = await Pet.deleteOne({ _id: id });
if (!deletedPet) {
return res.status(400).json({ success: false });
}
res.status(200).json({ success: true, data: {} });
} catch (error) {
res.status(400).json({ success: false });
}
break;
default:
res.status(400).json({ success: false });
break;
}
}