Commit 80c40adf authored by Nawasan Wisitsingkhon's avatar Nawasan Wisitsingkhon

remove wishlsit by user

parent 92932730
...@@ -11,6 +11,7 @@ const WishlistController = { ...@@ -11,6 +11,7 @@ const WishlistController = {
const wishlists = await db.wishlist.findMany({ const wishlists = await db.wishlist.findMany({
where: { user_id: Number(req.user.id) }, where: { user_id: Number(req.user.id) },
}); });
await db.$disconnect();
res.json({ status: 101, wishlists }); res.json({ status: 101, wishlists });
} catch (err) { } catch (err) {
res.json({ status: 100, message: "found some error" }); res.json({ status: 100, message: "found some error" });
...@@ -39,10 +40,32 @@ const WishlistController = { ...@@ -39,10 +40,32 @@ const WishlistController = {
}); });
res.json({ status: 201, message: "add wishlist success" }); res.json({ status: 201, message: "add wishlist success" });
} catch (err) { } catch (err) {
console.log(err);
res.json({ status: 200, message: "found some error on server" }); res.json({ status: 200, message: "found some error on server" });
} }
}, },
/**
*
* @param {Request} req
* @param {Response} res
*/
async delete(req, res) {
try {
const { id } = req.query;
if (!id) throw 400;
const wishlist_find = await db.wishlist.findFirst({
where: {
product_id: Number(id),
user_id: Number(req.user.id),
},
});
if(!wishlist_find)throw 400;
await db.wishlist.delete({where: {id: wishlist_find.id}})
await db.$disconnect();
res.json({ status: 401, message: "remove fav success" });
} catch (err) {
res.json({ status: 400, message: "remove fav failed with error" });
}
},
}; };
export default WishlistController; export default WishlistController;
...@@ -4,5 +4,6 @@ const UserRouter = express.Router(); ...@@ -4,5 +4,6 @@ const UserRouter = express.Router();
UserRouter.get("/wishlist", WishlistController.index); UserRouter.get("/wishlist", WishlistController.index);
UserRouter.post("/wishlist", WishlistController.create); UserRouter.post("/wishlist", WishlistController.create);
UserRouter.delete("/wishlist", WishlistController.delete);
export default UserRouter; export default UserRouter;
...@@ -29,13 +29,16 @@ export default function Home() { ...@@ -29,13 +29,16 @@ export default function Home() {
return; return;
} }
if (isRemove) { if (isRemove) {
let response = await axios.delete(`/api/u/wishlist?id=${id}`, {
headers: { token: user.value.token },
});
console.log(response.data);
} else { } else {
let response = await axios.post( let response = await axios.post(
"/api/u/wishlist", "/api/u/wishlist",
{ id }, { id },
{ headers: { token: user.value.token } } { headers: { token: user.value.token } }
); );
console.log(response.data);
} }
FetchWishlist(); FetchWishlist();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment