MusicTopus/src/middleware/Albums.js

157 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-02-15 11:03:20 +01:00
import moment from "moment";
import Pages from "./Pages";
import AlbumsModel from "../models/albums";
import ErrorEvent from "../libs/error";
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
? moment(data.released.replace("-00", "-01"))
: null;
2022-02-15 11:03:20 +01:00
delete data.id;
const album = new AlbumsModel(data);
return album.save();
}
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
{
2022-02-16 15:42:02 +01:00
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 = 1,
limit = 4,
sort = "artists_sort",
order = "asc",
2022-02-16 09:55:12 +01:00
artists_sort,
2022-02-16 15:42:02 +01:00
format,
2022-02-15 16:45:14 +01:00
} = this.req.query;
const skip = (page - 1) * limit;
2022-02-16 09:55:12 +01:00
const where = {};
if (artists_sort) {
where.artists_sort = artists_sort;
}
2022-02-16 15:42:02 +01:00
if (format) {
where["formats.name"] = format;
}
2022-02-16 09:55:12 +01:00
2022-02-15 16:45:14 +01:00
const count = await AlbumsModel.count({
user: this.req.user._id,
2022-02-16 09:55:12 +01:00
...where,
2022-02-15 16:45:14 +01:00
});
const rows = await AlbumsModel.find(
{
user: this.req.user._id,
2022-02-16 09:55:12 +01:00
...where,
2022-02-15 16:45:14 +01:00
},
[],
{
skip,
limit,
sort: {
[sort]: order.toLowerCase() === "asc" ? 1 : -1,
},
}
);
return {
rows,
count,
};
}
2022-02-16 09:55:12 +01: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, "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_sort",
this.req.user._id
);
const formats = await Albums.getAllDistincts(
"formats.name",
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);
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;
const item = await AlbumsModel.findOne({
_id,
User,
});
this.setPageContent("item", item);
}
2022-02-15 11:03:20 +01:00
}
export default Albums;