-
Notifications
You must be signed in to change notification settings - Fork 0
/
metierPlayList.js
284 lines (248 loc) · 8.59 KB
/
metierPlayList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// partie Metier du PlayList (back)
// list of playlist
//const PlayList = require('./Playlist');
const metierUser = require("./metierUser");
const metierMorceau = require("./metierMorceau");
const list = [];
var idc = 0;
var listU = [];
let listM = [];
// Function verify UserName Exist
var estExist = function (nomUser) {
const listU = metierUser.listerUser();
for (let i = 0; i < listU.length; i++) {
if (nomUser === listU[i].nomUser)
return true;
}
return false;
}
// Function de la verification
var estPresent = function (input) {
return input !== undefined;
}
//Constructor pour la version 1, nomPlayliste, nomCreateur, listMorceau => "creer une playliste et proposer des titres"
function PlayList(nomPlayList, nomCreateur, caractere, idPlayList) {
this.idPlayList = idPlayList;
this.nomPlayList = nomPlayList;
this.nomCreateur = nomCreateur;
this.nbClic = 0;
this.listMorceau = [];
this.listIDMusic = [];
this.listContributeur = [];
this.caractere = caractere;
this.datemisajour = new Date();
}
//ajouter PlayList version 1
var ajouterPlayList = function (playlist) {
let idPlayList;
idPlayList = idc;
idc++;
list[list.length] = new PlayList(playlist.nomPlayList, playlist.nomCreateur, playlist.caractere, idPlayList);
if (!estExist(playlist.nomCreateur)) {
// create User
metierUser.ajouterUserbyName(playlist.nomCreateur);
}
return list[idPlayList];
}
//incrementer nbClic
const incrementerNbClic = function (playlist) {
playlist.nbClic++;
}
// Get a PlayList
var getPlayList = function (idPlayList) {
if (typeof list[idPlayList] === 'undefined') return {};
else {
incrementerNbClic(list[idPlayList]);
return list[idPlayList];
}
}
//Get PlayList by nomCreateur ---- Version 4
var getPlayListbyUsername = function (nomCreateur) {
if (typeof list[nomCreateur] === 'undefined') return {};
else {
return list[nomCreateur];
}
}
//lister les PlayLists
var listerPlayList = function () {
return Object.values(list);
}
//find by style and nomPlayList
var searchPlayList = function (nomPlaylist, nomCreateur, style) {
//copy list to prevent modifications on list
var resPlayLists = [...list];
if (nomPlaylist && nomCreateur && style) {
resPlayLists = resPlayLists.filter(playList => {
if (playList.nomPlayList.indexOf(nomPlaylist) !== -1 && playList.nomCreateur.indexOf(nomCreateur) !== -1 && playList.caractere.indexOf(style) !== -1) {
return true
}
})
}
if (nomPlaylist) {
resPlayLists = resPlayLists.filter(playList => {
if (playList.nomPlayList.toUpperCase().indexOf(nomPlaylist.toUpperCase()) !== -1) {
return true
}
})
}
if (nomCreateur) {
resPlayLists = resPlayLists.filter(playList => {
if (playList.nomCreateur.toUpperCase().indexOf(nomCreateur.toUpperCase()) !== -1) {
return true
}
})
}
if (style) {
resPlayLists = resPlayLists.filter(playList => {
if (playList.caractere.toUpperCase().indexOf(style.toUpperCase()) !== -1) {
return true
}
})
}
return resPlayLists;
}
//Supprimer une playlist
var supprimerPlayList = function (idPlaylist) {
for (var i = 0; i < list.length; i++) {
if (idPlaylist === list[i].idPlayList) {
list.splice(i, 1);
}
}
}
//get position of a playlist
var getposition = function (idPlaylist) {
for (let i = 0; i < list.length; i++) {
if (list[i].idPlayList == idPlaylist)
return i;
}
return -1;
}
/*
// ajouter un Contributeur dans la playlist
var ajouterUserInPlaylist = function (idPlaylist, nomUser) {
let position = getposition(idPlaylist);
if (position !== -1) {
list[position].listContributeur.push(nomUser);
return list[position].listContributeur;
}
// list[getposition(idPlaylist)].listContributeur.add(nomUser);
//alert('Eh ! ça ne marche pas bien ton truc ! ');
}
// ajouter un Morceau dans la PlayList
var ajouterMorcerauInPlayList = function (idPlaylist, titre) {
let position = getposition(idPlaylist);
if (position !== -1) {
list[position].listMorceau.push(titre);
return list[position].listMorceau;
}
}
*/
// ajouter un Morceau et un Contributeur dans la Playlist
var ajouterMorcerauInPlayList = function (idPlaylist, titre) {
// vérifie qu'il n'existe pas déjà
if (estPresent(titre)) {
let position = getposition(idPlaylist);
if (position !== -1) {
// console.log(nomUser);
// console.log(titre);
list[position].listMorceau.push(titre);
//list[position].listContributeur.push(nomUser)
return list[position];
}
}
}
// Get a PlayList by creator
var getPlayListByCreateur = function (idUser) {
let response = [];
list.forEach(function (playlist) {
if (playlist.nomCreateur === idUser) {
response.push(playlist);
}
});
return response;
}
// Get a PlayList by style
var getPlayListByStyle = function (caractere) {
let response = [];
list.forEach(function (playlist) {
if (playlist.caractere === caractere) {
response.push(playlist);
}
});
return response;
}
// Get liste de tous les Morceaux
listM = metierMorceau.listerMorceau();
// Edit PlayList
var ajouterUserMorceauInPl = function (idPlaylist, nomUser, titre, idMusic) {
// vérifie qu'il n'existe pas déjà
let position = getposition(idPlaylist);
let i = 0;
let inlist = false;
let inlistM = false;
if (estPresent(nomUser) && estPresent(titre)) {
console.log('test de validation');
if (!estExist(nomUser)) {
// create User
metierUser.ajouterUserbyName(nomUser);
if (position !== -1) {
//ajoute Morceau
i = 0;
while (position !== -1 && i < list[idPlaylist].listIDMusic.length && !inlistM) {
console.log('idMorceau ' + list[idPlaylist].listIDMusic[i] + ' Music' + titre);
console.log(idMusic === list[idPlaylist].listIDMusic[i] + ' ' + idMusic + ' ' + list[idPlaylist].listIDMusic[i]);
if (idMusic === list[idPlaylist].listIDMusic[i]) {
console.log('inlistM : ' + inlistM);
inlistM = true;
}
i++;
}
console.log(inlistM);
if (position !== -1 && !inlistM) {
list[position].listMorceau.push(titre);
list[position].listIDMusic.push(idMusic);
list[position].listContributeur.push(nomUser);
}
}
} else {
while (position !== -1 && i < list[idPlaylist].listIDMusic.length && !inlistM) {
console.log('list[idPlaylist].idMorceau[i] ' + list[idPlaylist].listIDMusic[i] + ' Music ' + titre);
console.log('idMorceau ' + idMusic);
if (idMusic === list[idPlaylist].listIDMusic[i]) {
console.log('inlistM: ' + inlistM);
inlistM = true;
}
i++;
}
if (position !== -1 && !inlistM) {
list[position].listMorceau.push(titre);
list[position].listIDMusic.push(idMusic);
i = 0;
while (position !== -1 && i < list[idPlaylist].listContributeur.length && !inlist) {
console.log('test while user')
if (list[idPlaylist].listContributeur[i] === nomUser) {
console.log(inlist + ' test user inlist');
inlist = true;
}
i++;
}
if (!inlist && position !== -1) {
list[position].listContributeur.push(nomUser);
}
}
}
return list[position];
}
return -1;
}
exports.ajouterPlayList = ajouterPlayList;
exports.getPlayList = getPlayList;
exports.listerPlayList = listerPlayList;
exports.incrementerNbClic = incrementerNbClic;
exports.searchPlayList = searchPlayList;
exports.getPlayListbyUsername = getPlayListbyUsername;
exports.supprimerPlayList = supprimerPlayList;
exports.getPlayListByCreateur = getPlayListByCreateur;
exports.getPlayListByStyle = getPlayListByStyle;
exports.estPresent = estPresent;
exports.ajouterUserMorceauInPl = ajouterUserMorceauInPl;