Co-authored-by: dbroqua <contact@darkou.fr>
Reviewed-on: https://git.darkou.fr/dbroqua/MusicTopus/pulls/31
This commit is contained in:
Damien Broqua 2022-03-06 14:38:26 +01:00
parent ac72c1c13c
commit aeb5df067c
28 changed files with 630 additions and 66 deletions

View file

@ -5,12 +5,19 @@ import xl from "excel4node";
import Pages from "./Pages";
import AlbumsModel from "../models/albums";
import UsersModel from "../models/users";
import ErrorEvent from "../libs/error";
/**
* Classe permettant la gestion des albums d'un utilisateur
*/
class Albums extends Pages {
/**
* Méthode permettant de remplacer certains cartactères par leur équivalents html
* @param {String} str
*
* @return {String}
*/
static replaceSpecialChars(str) {
if (!str) {
return "";
@ -487,7 +494,7 @@ class Albums extends Pages {
static async getAllDistincts(field, user) {
const distincts = await AlbumsModel.find(
{
user,
User: user,
},
[],
{
@ -513,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) {
@ -524,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,
});
@ -547,7 +584,7 @@ class Albums extends Pages {
const rows = await AlbumsModel.find(
{
user: this.req.user._id,
User: userId,
...where,
},
[],
@ -619,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;

45
src/middleware/Me.js Normal file
View file

@ -0,0 +1,45 @@
import Joi from "joi";
import UsersModel from "../models/users";
/**
* Classe permettant la gestion de l'utilisateur connecté
*/
class Me {
constructor(req) {
this.req = req;
}
/**
* Méthode permettant de modifier le profil d'un utilisateur
* @return {Object}
*/
async patchMe() {
const { body, user } = this.req;
const schema = Joi.object({
isPublicCollection: Joi.boolean(),
});
const value = await schema.validateAsync(body);
const update = await UsersModel.findByIdAndUpdate(
user._id,
{ $set: value },
{ new: true }
);
await new Promise((resolve, reject) => {
this.req.login(update, (err) => {
if (err) {
return reject(err);
}
return resolve(null);
});
});
return update;
}
}
export default Me;