MusicTopus/src/routes/api/v1/albums.js

85 lines
2.4 KiB
JavaScript
Raw Permalink Normal View History

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("/")
.get(async (req, res, next) => {
2022-02-15 16:45:14 +01:00
try {
const albums = new Albums(req);
const data = await albums.getAll();
const { exportFormat = "json" } = req.query;
switch (exportFormat) {
case "csv":
case "musictopus":
res.header("Content-Type", "text/csv");
res.attachment("export-musictopus.csv");
return res.status(200).send(data);
case "xml":
res.header("Content-type", "text/xml");
res.attachment("export-musictopus.xml");
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) {
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
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);
}
})
.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);
}
});
router
.route("/:itemId/share")
.post(ensureLoggedIn("/connexion"), async (req, res, next) => {
try {
const albums = new Albums(req);
const data = await albums.shareOne();
sendResponse(req, res, data);
} catch (err) {
next(err);
}
});
2022-02-15 11:03:20 +01:00
export default router;