153 lines
3.9 KiB
JavaScript
153 lines
3.9 KiB
JavaScript
class Api {
|
|
getAuthors(params, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
params = Object.entries(params)
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join("&");
|
|
params = params !== "" ? `&${params}` : "";
|
|
xhr.open(
|
|
"GET",
|
|
`/api/v/1.0/get-authors?response_format=json${params}`,
|
|
true,
|
|
);
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send();
|
|
}
|
|
|
|
getMusic(params, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
params = Object.entries(params)
|
|
.map(([key, value]) => `${key}=${value}`)
|
|
.join("&");
|
|
params = params !== "" ? `&${params}` : "";
|
|
xhr.open("GET", `/api/v/1.0/get-music?response_format=json${params}`, true);
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send();
|
|
}
|
|
|
|
deleteMusic(id, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/remove-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
if (JSON.parse(this.responseText).response === undefined) {
|
|
throw this.responseText;
|
|
}
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "music",
|
|
id,
|
|
}),
|
|
);
|
|
}
|
|
|
|
editMusic(id, name, data, author_id, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/edit-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
if (JSON.parse(this.responseText).response === undefined) {
|
|
throw this.responseText;
|
|
}
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "music",
|
|
id,
|
|
name,
|
|
author_id,
|
|
data,
|
|
}),
|
|
);
|
|
}
|
|
|
|
createMusic(author_id, name, data = null, cb) {
|
|
data = data === null ? undefined : data;
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/create-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "music",
|
|
name,
|
|
author_id,
|
|
data,
|
|
}),
|
|
);
|
|
}
|
|
|
|
createAuthor(name, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/create-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "author",
|
|
name,
|
|
}),
|
|
);
|
|
}
|
|
|
|
removeAuthor(id, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/remove-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "author",
|
|
id,
|
|
}),
|
|
);
|
|
}
|
|
|
|
editAuthor(id, name, cb) {
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `/api/v/1.0/edit-item?response_format=json`, true);
|
|
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
|
|
xhr.onreadystatechange = function (event) {
|
|
//console.log(event);
|
|
if (this.readyState != 4) return;
|
|
cb(JSON.parse(this.responseText).response);
|
|
};
|
|
xhr.send(
|
|
JSON.stringify({
|
|
type: "author",
|
|
id,
|
|
name,
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|
|
const api = new Api();
|