Compare commits
2 Commits
d75ea3f2aa
...
b87d40ade8
Author | SHA1 | Date |
---|---|---|
|
b87d40ade8 | 2 weeks ago |
|
c7dc5314a1 | 2 weeks ago |
@ -0,0 +1,124 @@ |
|||||||
|
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, |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// * U
|
||||||
|
router.put("/cats/:id", (req, res) => { |
||||||
|
try { |
||||||
|
const params = req.params; |
||||||
|
const body = req.body; |
||||||
|
let result; |
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id); |
||||||
|
if (index !== -1) { |
||||||
|
const tempId = params.id; |
||||||
|
const newCat = { ...body, id: tempId }; |
||||||
|
Cat[index] = newCat; |
||||||
|
result = newCat; |
||||||
|
} |
||||||
|
res.status(200).send({ |
||||||
|
success: true, |
||||||
|
data: { cat: result }, |
||||||
|
}); |
||||||
|
} catch (error) { |
||||||
|
res.status(400).send({ |
||||||
|
success: false, |
||||||
|
error: error.message, |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
router.patch("/cats/:id", (req, res) => { |
||||||
|
try { |
||||||
|
const params = req.params; |
||||||
|
const body = req.body; |
||||||
|
let result; |
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id); |
||||||
|
if (index !== -1) { |
||||||
|
const tempId = params.id; |
||||||
|
const newCat = { ...Cat[index], ...body, id: tempId }; |
||||||
|
Cat[index] = newCat; |
||||||
|
result = newCat; |
||||||
|
} |
||||||
|
res.status(200).send({ |
||||||
|
success: true, |
||||||
|
data: { cat: result }, |
||||||
|
}); |
||||||
|
} catch (error) { |
||||||
|
res.status(400).send({ |
||||||
|
success: false, |
||||||
|
error: error.message, |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
// * D
|
||||||
|
router.delete("/cats/:id", (req, res) => { |
||||||
|
try { |
||||||
|
const params = req.params; |
||||||
|
const body = req.body; |
||||||
|
let result; |
||||||
|
const index = Cat.findIndex((cat) => cat.id === params.id); |
||||||
|
if (index !== -1) { |
||||||
|
Cat.splice(index, 1); |
||||||
|
} |
||||||
|
res.status(200).send({ |
||||||
|
success: true, |
||||||
|
data: { cat: result }, |
||||||
|
}); |
||||||
|
} catch (error) { |
||||||
|
res.status(400).send({ |
||||||
|
success: false, |
||||||
|
error: error.message, |
||||||
|
}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
export default router; |
Loading…
Reference in new issue