You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
999 B

import { Router } from "express";
import { CatType, Cat } from "./cats.model";
const router = Router();
// * C
router.post("/cats", (req, res) => {
try {
const data = req.body;
Cat.push(data);
res.status(200).send({
success: true,
data: { data },
});
} catch (error) {}
});
// * R
router.get("/cats", (req, res) => {
try {
const cats = Cat;
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
router.get("/cats/:id", (req, res) => {
try {
const params = req.params;
const cats = Cat.find((cat) => {
return cat.id === params.id;
});
if (!cats) throw new Error("no matched data");
res.status(200).send({
success: true,
data: { cats },
});
} catch (error) {
res.status(400).send({
success: false,
error: error.message,
});
}
});
export default router;