|
|
@ -50,4 +50,75 @@ router.get("/cats/:id", (req, res) => { |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// * 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; |
|
|
|
export default router; |
|
|
|