2022-02-15 11:03:20 +01:00
|
|
|
import express from "express";
|
|
|
|
import { ensureLoggedIn } from "connect-ensure-login";
|
|
|
|
|
|
|
|
import { sendResponse } from "../../../libs/format";
|
|
|
|
import Albums from "../../../middleware/Albums";
|
|
|
|
|
|
|
|
// eslint-disable-next-line new-cap
|
|
|
|
const router = express.Router();
|
|
|
|
|
2022-02-15 16:45:14 +01:00
|
|
|
router
|
|
|
|
.route("/")
|
2022-03-06 14:38:26 +01:00
|
|
|
.get(async (req, res, next) => {
|
2022-02-15 16:45:14 +01:00
|
|
|
try {
|
|
|
|
const albums = new Albums(req);
|
|
|
|
const data = await albums.getAll();
|
2022-03-04 16:33:45 +01:00
|
|
|
const { exportFormat = "json" } = req.query;
|
|
|
|
|
|
|
|
switch (exportFormat) {
|
|
|
|
case "csv":
|
|
|
|
case "musictopus":
|
|
|
|
res.header("Content-Type", "text/csv");
|
2022-03-05 19:43:26 +01:00
|
|
|
res.attachment("export-musictopus.csv");
|
2022-03-04 16:33:45 +01:00
|
|
|
return res.status(200).send(data);
|
|
|
|
case "xml":
|
|
|
|
res.header("Content-type", "text/xml");
|
2022-03-05 19:43:26 +01:00
|
|
|
res.attachment("export-musictopus.xml");
|
2022-03-04 16:33:45 +01:00
|
|
|
return res.status(200).send(data);
|
|
|
|
case "xls":
|
|
|
|
return data.write("musictopus.xls", res);
|
|
|
|
case "json":
|
|
|
|
default:
|
|
|
|
return sendResponse(req, res, data);
|
|
|
|
}
|
2022-02-15 16:45:14 +01:00
|
|
|
} catch (err) {
|
2022-03-04 16:33:45 +01:00
|
|
|
return next(err);
|
2022-02-15 16:45:14 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.post(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const data = await Albums.postAddOne(req);
|
|
|
|
|
|
|
|
sendResponse(req, res, data);
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
2022-02-15 11:03:20 +01:00
|
|
|
|
2022-02-20 21:52:47 +01:00
|
|
|
router
|
|
|
|
.route("/:itemId")
|
2022-08-30 15:17:14 +02:00
|
|
|
.patch(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const albums = new Albums(req);
|
|
|
|
const data = await albums.patchOne();
|
|
|
|
|
|
|
|
sendResponse(req, res, data);
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
})
|
2022-02-20 21:52:47 +01:00
|
|
|
.delete(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const albums = new Albums(req);
|
|
|
|
const data = await albums.deleteOne();
|
|
|
|
|
|
|
|
sendResponse(req, res, data);
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-15 11:03:20 +01:00
|
|
|
export default router;
|