2022-02-15 11:03:20 +01:00
|
|
|
import moment from "moment";
|
|
|
|
|
|
|
|
import Pages from "./Pages";
|
|
|
|
|
|
|
|
import { getAlbumDetails } from "../helpers";
|
|
|
|
|
|
|
|
import AlbumsModel from "../models/albums";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe permettant la gestion des albums d'un utilisateur
|
|
|
|
*/
|
|
|
|
class Albums extends Pages {
|
|
|
|
async getFormAddOne() {
|
|
|
|
const data = await getAlbumDetails(this.req.params.discogsId);
|
|
|
|
|
|
|
|
const {
|
|
|
|
id, // Integer
|
|
|
|
year, // - Integer
|
|
|
|
uri, // String
|
|
|
|
artists, // - Array<Object>
|
|
|
|
artists_sort, // String
|
|
|
|
labels, // - Array<Object>
|
|
|
|
series, // Array
|
|
|
|
companies, // - Array<Object>
|
|
|
|
formats, // - Array<Object>
|
|
|
|
title, // - String
|
|
|
|
country, // - String
|
|
|
|
released, // - Date
|
|
|
|
notes, // - String
|
|
|
|
identifiers, // - Array<Object>
|
|
|
|
videos, // - Array<Object>
|
|
|
|
genres, // - Array<String>
|
|
|
|
styles, // - Array<String>
|
|
|
|
tracklist, // - Array<Object>
|
|
|
|
extraartists, // - Array<Object>
|
|
|
|
images, // - Array<Object
|
|
|
|
thumb, // - String
|
|
|
|
} = data;
|
|
|
|
|
|
|
|
this.pageContent.page.values = "test";
|
|
|
|
|
|
|
|
this.setPageContent("values", {
|
|
|
|
id,
|
|
|
|
year,
|
|
|
|
uri,
|
|
|
|
artists,
|
|
|
|
artists_sort,
|
|
|
|
labels,
|
|
|
|
series,
|
|
|
|
companies,
|
|
|
|
formats,
|
|
|
|
title,
|
|
|
|
country,
|
|
|
|
released,
|
|
|
|
notes,
|
|
|
|
identifiers,
|
|
|
|
videos,
|
|
|
|
genres,
|
|
|
|
styles,
|
|
|
|
tracklist,
|
|
|
|
extraartists,
|
|
|
|
images,
|
|
|
|
thumb,
|
|
|
|
});
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-02-16 15:42:02 +01:00
|
|
|
static async getAllDistincts(field, user) {
|
2022-02-16 09:55:12 +01:00
|
|
|
const artists = await AlbumsModel.find(
|
|
|
|
{
|
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 artists;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|
2022-02-15 11:03:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default Albums;
|