From 0703d0bc33a527c69655db4744891a0ff6ed2ff0 Mon Sep 17 00:00:00 2001
From: dbroqua
Date: Sun, 6 Mar 2022 14:24:10 +0100
Subject: [PATCH] =?UTF-8?q?Affichage=20d'une=20collection=20partag=C3=A9e?=
=?UTF-8?q?=20et=20correction=20d'un=20gros=20bug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/app.js | 7 +-
src/libs/error.js | 4 +-
src/middleware/Albums.js | 60 +++++-
src/middleware/Me.js | 5 +-
src/routes/api/v1/albums.js | 2 +-
src/routes/collection.js | 22 +++
views/error.ejs | 2 +
views/pages/collection.ejs | 184 ++++++++++++++++++
.../pages/mon-compte/ma-collection/index.ejs | 9 +-
9 files changed, 283 insertions(+), 12 deletions(-)
create mode 100644 src/routes/collection.js
create mode 100644 views/pages/collection.ejs
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 @@
<% } %>
+ <% if ( process.env.NODE_ENV !== 'production' ) { %>
+ <% } %>
\ 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 }}
+
+ (
+ {{description}},
+ )
+
+ ,
+
+
+
+ Genre : {{ genre }},
+
+ Style : {{ 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) => {