mdb to bulma + view my collection
This commit is contained in:
parent
f08e70eb7c
commit
68fa736a91
21 changed files with 617 additions and 292 deletions
18
src/app.js
18
src/app.js
|
@ -9,6 +9,8 @@ import MongoStore from "connect-mongo";
|
|||
|
||||
import config, { env, mongoDbUri, secret } from "./config";
|
||||
|
||||
import { isXhr } from "./helpers";
|
||||
|
||||
import indexRouter from "./routes";
|
||||
import addAlbumRouter from "./routes/addAlbum";
|
||||
import importRouterApiV1 from "./routes/api/v1";
|
||||
|
@ -68,18 +70,6 @@ app.set("views", path.join(__dirname, "../views"));
|
|||
app.set("view engine", "ejs");
|
||||
|
||||
app.use(express.static(path.join(__dirname, "../public")));
|
||||
app.use(
|
||||
"/libs/jquery",
|
||||
express.static(path.join(__dirname, "../node_modules/jquery/dist/"))
|
||||
);
|
||||
app.use(
|
||||
"/libs/mdb-ui-kit/css",
|
||||
express.static(path.join(__dirname, "../node_modules/mdb-ui-kit/css"))
|
||||
);
|
||||
app.use(
|
||||
"/libs/mdb-ui-kit/js",
|
||||
express.static(path.join(__dirname, "../node_modules/mdb-ui-kit/js"))
|
||||
);
|
||||
app.use(
|
||||
"/libs/vue",
|
||||
express.static(path.join(__dirname, "../node_modules/vue/dist"))
|
||||
|
@ -96,7 +86,7 @@ app.use("/api/v1/albums", importAlbumRouterApiV1);
|
|||
|
||||
// Handle 404
|
||||
app.use((req, res) => {
|
||||
if (req.xhr || req.rawHeaders.indexOf("application/json") !== -1) {
|
||||
if (isXhr(req)) {
|
||||
res.status(404).send({ message: "404: Not found" });
|
||||
} else {
|
||||
res.status(404).render("index", {
|
||||
|
@ -116,7 +106,7 @@ app.use((req, res) => {
|
|||
|
||||
// Handle 500
|
||||
app.use((error, req, res, next) => {
|
||||
if (req.xhr || req.rawHeaders.indexOf("application/json") !== -1) {
|
||||
if (isXhr(req)) {
|
||||
const { message, errorCode, date } = error;
|
||||
res.status(error.errorCode || 500).send({ message, errorCode, date });
|
||||
} else {
|
||||
|
|
|
@ -23,3 +23,16 @@ export const getAlbumDetails = async (id) => {
|
|||
|
||||
return res;
|
||||
};
|
||||
|
||||
export const isXhr = (req) => {
|
||||
const is = req.xhr;
|
||||
if (!is) {
|
||||
for (let i = 0; i < req.rawHeaders.length; i += 1) {
|
||||
if (req.rawHeaders[i].indexOf("application/json") !== -1) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return is;
|
||||
};
|
||||
|
|
|
@ -73,13 +73,49 @@ class Albums extends Pages {
|
|||
discogsId: body.id,
|
||||
User: user._id,
|
||||
};
|
||||
data.released = moment(data.released.replace("-00", "-01"));
|
||||
data.released = data.released
|
||||
? moment(data.released.replace("-00", "-01"))
|
||||
: null;
|
||||
delete data.id;
|
||||
|
||||
const album = new AlbumsModel(data);
|
||||
|
||||
return album.save();
|
||||
}
|
||||
|
||||
async getAll() {
|
||||
const {
|
||||
page = 1,
|
||||
limit = 4,
|
||||
sort = "artists_sort",
|
||||
order = "asc",
|
||||
} = this.req.query;
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const count = await AlbumsModel.count({
|
||||
user: this.req.user._id,
|
||||
});
|
||||
|
||||
const rows = await AlbumsModel.find(
|
||||
{
|
||||
user: this.req.user._id,
|
||||
},
|
||||
[],
|
||||
{
|
||||
skip,
|
||||
limit,
|
||||
sort: {
|
||||
[sort]: order.toLowerCase() === "asc" ? 1 : -1,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
return {
|
||||
rows,
|
||||
count,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export default Albums;
|
||||
|
|
|
@ -7,14 +7,26 @@ import Albums from "../../../middleware/Albums";
|
|||
// eslint-disable-next-line new-cap
|
||||
const router = express.Router();
|
||||
|
||||
router.route("/").post(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
||||
try {
|
||||
const data = await Albums.postAddOne(req);
|
||||
router
|
||||
.route("/")
|
||||
.get(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
||||
try {
|
||||
const albums = new Albums(req);
|
||||
const data = await albums.getAll();
|
||||
|
||||
sendResponse(req, res, data);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
sendResponse(req, res, data);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
})
|
||||
.post(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
||||
try {
|
||||
const data = await Albums.postAddOne(req);
|
||||
|
||||
sendResponse(req, res, data);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -72,6 +72,18 @@ router
|
|||
}
|
||||
});
|
||||
|
||||
router
|
||||
.route("/ma-collection")
|
||||
.get(ensureLoggedIn("/connexion"), async (req, res, next) => {
|
||||
try {
|
||||
const page = new Pages(req, "mon-compte/ma-collection");
|
||||
|
||||
render(res, page);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
router.route("/se-deconnecter").get((req, res) => {
|
||||
req.logout();
|
||||
req.session.destroy(() => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue