Compare commits
No commits in common. 'b87d40ade891e08023a6ca722b4e6dc1891bebbd' and 'd75ea3f2aa6b4c72a89f5542ce6c38d6795a4ca7' have entirely different histories.
b87d40ade8
...
d75ea3f2aa
@ -1,124 +0,0 @@ |
|||||||
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