diff --git a/src/app.js b/src/app.js index 268b589..6d1e507 100644 --- a/src/app.js +++ b/src/app.js @@ -13,6 +13,7 @@ import { isXhr } from "./helpers"; import indexRouter from "./routes"; import maCollectionRouter from "./routes/ma-collection"; +import collectionRouter from "./routes/collection"; import importAlbumRouterApiV1 from "./routes/api/v1/albums"; import importSearchRouterApiV1 from "./routes/api/v1/search"; @@ -83,6 +84,7 @@ app.use( app.use("/", indexRouter); app.use("/ma-collection", maCollectionRouter); +app.use("/collection", collectionRouter); app.use("/api/v1/albums", importAlbumRouterApiV1); app.use("/api/v1/search", importSearchRouterApiV1); app.use("/api/v1/me", importMeRouterApiV1); @@ -115,7 +117,10 @@ app.use((error, req, res, next) => { } else { res.status(error.errorCode || 500); res.render("index", { - page: { title: "500: Oups… le serveur a crashé !", error }, + page: { + title: error.title || "500: Oups… le serveur a crashé !", + error, + }, errorCode: error.errorCode || 500, viewname: "error", user: req.user || null, diff --git a/src/libs/error.js b/src/libs/error.js index 562265f..13470f2 100644 --- a/src/libs/error.js +++ b/src/libs/error.js @@ -4,9 +4,10 @@ class ErrorEvent extends Error { /** * @param {Number} errorCode + * @param {String} title * @param {Mixed} ...params */ - constructor(errorCode, ...params) { + constructor(errorCode, title, ...params) { super(...params); if (Error.captureStackTrace) { @@ -14,6 +15,7 @@ class ErrorEvent extends Error { } this.errorCode = parseInt(errorCode, 10); + this.title = title; this.date = new Date(); } } diff --git a/src/middleware/Albums.js b/src/middleware/Albums.js index 06220df..8025973 100644 --- a/src/middleware/Albums.js +++ b/src/middleware/Albums.js @@ -5,6 +5,7 @@ import xl from "excel4node"; import Pages from "./Pages"; import AlbumsModel from "../models/albums"; +import UsersModel from "../models/users"; import ErrorEvent from "../libs/error"; /** @@ -493,7 +494,7 @@ class Albums extends Pages { static async getAllDistincts(field, user) { const distincts = await AlbumsModel.find( { - user, + User: user, }, [], { @@ -519,8 +520,11 @@ class Albums extends Pages { order = "asc", artists_sort, format, + userId: collectionUserId, } = this.req.query; + let userId = this.req.user?._id; + const where = {}; if (artists_sort) { @@ -530,8 +534,35 @@ class Albums extends Pages { where["formats.name"] = format; } + if (!this.req.user && !collectionUserId) { + throw new ErrorEvent( + 401, + "Cette collection n'est pas publique", + "Cette collection n'est pas publique" + ); + } + + if (collectionUserId) { + const userIsSharingCollection = await UsersModel.findById( + collectionUserId + ); + + if ( + !userIsSharingCollection || + !userIsSharingCollection.isPublicCollection + ) { + throw new ErrorEvent( + 401, + "Cette collection n'est pas publique", + "Cette collection n'est pas publique" + ); + } + + userId = userIsSharingCollection._id; + } + const count = await AlbumsModel.count({ - user: this.req.user._id, + User: userId, ...where, }); @@ -553,7 +584,7 @@ class Albums extends Pages { const rows = await AlbumsModel.find( { - user: this.req.user._id, + User: userId, ...where, }, [], @@ -625,6 +656,29 @@ class Albums extends Pages { this.setPageContent("item", item); } + + /** + * Méthode permettant de créer la page "collection/:userId" + */ + async loadPublicCollection() { + const { userId } = this.req.params; + + const user = await UsersModel.findById(userId); + + if (!user || !user.isPublicCollection) { + throw new ErrorEvent( + 401, + "Cet utilisateur ne souhaite pas partager sa collection" + ); + } + + const artists = await Albums.getAllDistincts("artists_sort", userId); + const formats = await Albums.getAllDistincts("formats.name", userId); + + this.setPageContent("username", user.username); + this.setPageContent("artists", artists); + this.setPageContent("formats", formats); + } } export default Albums; diff --git a/src/middleware/Me.js b/src/middleware/Me.js index 68e0f2f..ae2712c 100644 --- a/src/middleware/Me.js +++ b/src/middleware/Me.js @@ -1,7 +1,6 @@ import Joi from "joi"; -import mongoose from "mongoose"; -const Users = mongoose.model("Users"); +import UsersModel from "../models/users"; /** * Classe permettant la gestion de l'utilisateur connecté @@ -23,7 +22,7 @@ class Me { }); const value = await schema.validateAsync(body); - const update = await Users.findByIdAndUpdate( + const update = await UsersModel.findByIdAndUpdate( user._id, { $set: value }, { new: true } diff --git a/src/routes/api/v1/albums.js b/src/routes/api/v1/albums.js index c8f0f1a..e1caa1f 100644 --- a/src/routes/api/v1/albums.js +++ b/src/routes/api/v1/albums.js @@ -9,7 +9,7 @@ const router = express.Router(); router .route("/") - .get(ensureLoggedIn("/connexion"), async (req, res, next) => { + .get(async (req, res, next) => { try { const albums = new Albums(req); const data = await albums.getAll(); diff --git a/src/routes/collection.js b/src/routes/collection.js new file mode 100644 index 0000000..6584992 --- /dev/null +++ b/src/routes/collection.js @@ -0,0 +1,22 @@ +import express from "express"; + +import Albums from "../middleware/Albums"; + +import render from "../libs/format"; + +// eslint-disable-next-line new-cap +const router = express.Router(); + +router.route("/:userId").get(async (req, res, next) => { + try { + const page = new Albums(req, "collection"); + + await page.loadPublicCollection(); + + render(res, page); + } catch (err) { + next(err); + } +}); + +export default router; diff --git a/views/error.ejs b/views/error.ejs index b0e5112..8f80430 100644 --- a/views/error.ejs +++ b/views/error.ejs @@ -5,7 +5,9 @@ Erreur 404

<% } %> + <% if ( process.env.NODE_ENV !== 'production' ) { %>
<%= page.error %>
+ <% } %> \ No newline at end of file diff --git a/views/pages/collection.ejs b/views/pages/collection.ejs new file mode 100644 index 0000000..9359a12 --- /dev/null +++ b/views/pages/collection.ejs @@ -0,0 +1,184 @@ +
+

+ Collection de <%= page.username %> +

+ +
+
+ + +
+
+ + +
+
+ + +
+
+
+
+ + {{ item.artists_sort}} - {{ item.title }} + +
+
+ +
+
+ Année : {{ item.year }} +
+ Pays : {{ item.country }} +
+ + Format : + + {{ format.name }} + + + + +
+ Genre : +
+ Style : +
+
+
+
+
+ Nombre total d'éléments : {{total}} +
+ +
+ + diff --git a/views/pages/mon-compte/ma-collection/index.ejs b/views/pages/mon-compte/ma-collection/index.ejs index 57fcb11..f98d3f9 100644 --- a/views/pages/mon-compte/ma-collection/index.ejs +++ b/views/pages/mon-compte/ma-collection/index.ejs @@ -3,6 +3,9 @@ Ma collection + + Voir ma collection partagée +
@@ -257,11 +260,11 @@ }) .then( (res) => { this.isPublicCollection = res.data.isPublicCollection; - showToastr("Collection partagée", true); if ( this.isPublicCollection ) { - console.log('ici', this.shareLink) - window.open(this.shareLink, '_blank'); + showToastr("Votre collection est désormais publique", true); + } else { + showToastr("Votre collection n'est plus partagée", true); } }) .catch((err) => {