MusicTopus/src/middleware/Albums.js

321 lines
8.2 KiB
JavaScript
Raw Normal View History

2022-04-10 16:04:50 +02:00
import { format as formatDate } from "date-fns";
2022-02-15 11:03:20 +01:00
import Pages from "./Pages";
import Export from "./Export";
2022-02-15 11:03:20 +01:00
import AlbumsModel from "../models/albums";
import JobsModel from "../models/jobs";
import UsersModel from "../models/users";
import ErrorEvent from "../libs/error";
2022-08-30 15:17:14 +02:00
import { getAlbumDetails } from "../helpers";
2022-02-15 11:03:20 +01:00
/**
* Classe permettant la gestion des albums d'un utilisateur
*/
class Albums extends Pages {
/**
* Méthode permettant d'ajouter un album dans une collection
* @param {Object} req
* @return {Object}
*/
2022-02-15 11:03:20 +01:00
static async postAddOne(req) {
const { body, user } = req;
const data = {
...body,
discogsId: body.id,
User: user._id,
};
2022-02-15 16:45:14 +01:00
data.released = data.released
2022-04-10 16:04:50 +02:00
? new Date(data.released.replace("-00", "-01"))
2022-02-15 16:45:14 +01:00
: null;
2022-02-15 11:03:20 +01:00
delete data.id;
const album = new AlbumsModel(data);
await album.save();
const jobData = {
model: "Albums",
id: album._id,
};
const job = new JobsModel(jobData);
job.save();
return album;
2022-02-15 11:03:20 +01:00
}
2022-02-15 16:45:14 +01:00
/**
* Méthode permettant de récupérer les éléments distincts d'une collection
* @param {String} field
* @param {ObjectId} user
* @return {Array}
*/
2022-02-16 15:42:02 +01:00
static async getAllDistincts(field, user) {
const distincts = await AlbumsModel.find(
2022-02-16 09:55:12 +01:00
{
User: user,
2022-02-16 09:55:12 +01:00
},
[],
{
sort: {
2022-02-16 15:42:02 +01:00
[field]: 1,
2022-02-16 09:55:12 +01:00
},
}
2022-02-16 15:42:02 +01:00
).distinct(field);
2022-02-16 09:55:12 +01:00
return distincts;
2022-02-16 09:55:12 +01:00
}
/**
* Méthode permettant de récupérer la liste des albums d'une collection
* @return {Object}
*/
2022-02-15 16:45:14 +01:00
async getAll() {
const {
page,
limit,
exportFormat = "json",
2022-02-15 16:45:14 +01:00
sort = "artists_sort",
order = "asc",
artist,
2022-02-16 15:42:02 +01:00
format,
year,
genre,
style,
userId: collectionUserId,
2022-02-15 16:45:14 +01:00
} = this.req.query;
let userId = this.req.user?._id;
2022-02-16 09:55:12 +01:00
const where = {};
if (artist) {
where["artists.name"] = artist;
2022-02-16 09:55:12 +01:00
}
2022-02-16 15:42:02 +01:00
if (format) {
where["formats.name"] = format;
}
if (year) {
where.year = year;
}
if (genre) {
where.genres = genre;
}
if (style) {
where.styles = style;
}
2022-02-16 09:55:12 +01:00
if (!this.req.user && !collectionUserId) {
throw new ErrorEvent(
401,
"Collection",
"Cette collection n'est pas publique"
);
}
if (collectionUserId) {
const userIsSharingCollection = await UsersModel.findById(
collectionUserId
);
if (
!userIsSharingCollection ||
!userIsSharingCollection.isPublicCollection
) {
throw new ErrorEvent(
401,
"Collection",
"Cette collection n'est pas publique"
);
}
userId = userIsSharingCollection._id;
}
2022-02-15 16:45:14 +01:00
const count = await AlbumsModel.count({
User: userId,
2022-02-16 09:55:12 +01:00
...where,
2022-02-15 16:45:14 +01:00
});
let params = {
sort: {
[sort]: order.toLowerCase() === "asc" ? 1 : -1,
},
};
if (page && limit) {
const skip = (page - 1) * limit;
params = {
...params,
skip,
limit,
};
}
2022-02-15 16:45:14 +01:00
const rows = await AlbumsModel.find(
{
User: userId,
2022-02-16 09:55:12 +01:00
...where,
2022-02-15 16:45:14 +01:00
},
[],
params
2022-02-15 16:45:14 +01:00
);
switch (exportFormat) {
case "csv":
return Export.convertToCsv(rows);
case "xls":
return Export.convertToXls(rows);
case "xml":
return Export.convertToXml(rows);
case "musictopus":
return Export.convertToMusicTopus(rows);
case "json":
default:
return {
rows,
count,
};
}
2022-02-15 16:45:14 +01:00
}
2022-02-16 09:55:12 +01:00
2022-08-30 15:17:14 +02:00
/**
* Méthode permettant de mettre à jour un album
*
* @return {Object}
*/
async patchOne() {
const { itemId: _id } = this.req.params;
const { _id: User } = this.req.user;
2022-09-14 14:45:22 +02:00
const query = {
2022-08-30 15:17:14 +02:00
_id,
User,
2022-09-14 14:45:22 +02:00
};
const album = await AlbumsModel.findOne(query);
2022-08-30 15:17:14 +02:00
if (!album) {
throw new ErrorEvent(
404,
"Mise à jour",
"Impossible de trouver cet album"
);
2022-08-30 15:17:14 +02:00
}
const values = await getAlbumDetails(album.discogsId);
2022-09-14 14:45:22 +02:00
return AlbumsModel.findOneAndUpdate(query, values, { new: true });
2022-08-30 15:17:14 +02:00
}
/**
* Méthode permettant de supprimer un élément d'une collection
* @return {Boolean}
*/
async deleteOne() {
const res = await AlbumsModel.findOneAndDelete({
user: this.req.user._id,
_id: this.req.params.itemId,
});
if (res) {
return true;
}
throw new ErrorEvent(
404,
"Suppression",
"Impossible de trouver cet album"
);
}
/**
* Méthode permettant de créer la page "ma-collection"
*/
2022-02-16 09:55:12 +01:00
async loadMyCollection() {
2022-02-16 15:42:02 +01:00
const artists = await Albums.getAllDistincts(
"artists.name",
2022-02-16 15:42:02 +01:00
this.req.user._id
);
const formats = await Albums.getAllDistincts(
"formats.name",
this.req.user._id
);
const years = await Albums.getAllDistincts("year", this.req.user._id);
const genres = await Albums.getAllDistincts(
"genres",
this.req.user._id
);
const styles = await Albums.getAllDistincts(
"styles",
this.req.user._id
);
2022-02-16 09:55:12 +01:00
this.setPageContent("artists", artists);
2022-02-16 15:42:02 +01:00
this.setPageContent("formats", formats);
this.setPageContent("years", years);
this.setPageContent("genres", genres);
this.setPageContent("styles", styles);
this.setPageTitle("Ma collection");
2022-02-16 09:55:12 +01:00
}
/**
* Méthode permettant d'afficher le détails d'un album
*/
async loadItem() {
const { itemId: _id } = this.req.params;
const { _id: User } = this.req.user;
2022-04-10 16:04:50 +02:00
const album = await AlbumsModel.findOne({
_id,
User,
});
2022-04-10 16:04:50 +02:00
const item = {
...album.toJSON(),
released: album.released
? formatDate(album.released, "MM/dd/yyyy")
: null,
2022-04-10 16:04:50 +02:00
};
this.setPageContent("item", item);
this.setPageTitle(
`Détails de l'album ${item.title} de ${item.artists_sort}`
);
}
/**
* 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,
"Collection non partagée",
"Cet utilisateur ne souhaite pas partager sa collection"
);
}
const artists = await Albums.getAllDistincts("artists.name", userId);
const formats = await Albums.getAllDistincts("formats.name", userId);
const years = await Albums.getAllDistincts("year", userId);
const genres = await Albums.getAllDistincts("genres", userId);
const styles = await Albums.getAllDistincts("styles", userId);
this.setPageContent("username", user.username);
this.setPageTitle(`Collection publique de ${user.username}`);
this.setPageContent("artists", artists);
this.setPageContent("formats", formats);
this.setPageContent("years", years);
this.setPageContent("genres", genres);
this.setPageContent("styles", styles);
}
2022-02-15 11:03:20 +01:00
}
export default Albums;