import express from "express";
import { getList, getOne, add, update, remove } from "~/services/wishlist";

// eslint-disable-next-line new-cap
const router = express.Router();

router.get("/public", async function (req, res, next) {
	res.json(await getList(1));
});

router.get("/authenticated", async function (req, res, next) {
	res.json(await getList(0));
});

router.get("/:path/:wishlistID", async function (req, res, next) {
	res.json(await getOne(req.params.path, req.params.wishlistID));
});

router.post("/", async function (req, res, next) {
	try {
		res.json(await add(req.body));
	} catch (err) {
		res.status(500).json(err.message);
		next(err);
	}
});

router.put("/:id", async function (req, res, next) {
	try {
		res.json(await update(req.params.id, req.body));
	} catch (err) {
		next(err);
	}
});

router.delete("/:id", async function (req, res, next) {
	try {
		res.json(await remove(req.params.id));
	} catch (err) {
		next(err);
	}
});

export default router;
