153 lines
4.2 KiB
JavaScript
153 lines
4.2 KiB
JavaScript
// ======================================================
|
|
// API CORE REQUEST
|
|
// ======================================================
|
|
async function apiRequest(method, path, body = null) {
|
|
|
|
if (!Admin.BASE_URL) {
|
|
console.error("BASE_URL non ancora inizializzato");
|
|
throw new Error("BASE_URL non inizializzato");
|
|
}
|
|
|
|
const options = {
|
|
method,
|
|
headers: {
|
|
"Authorization": "Bearer " + Admin.token,
|
|
"Content-Type": "application/json"
|
|
}
|
|
};
|
|
|
|
if (body) {
|
|
options.body = JSON.stringify(body);
|
|
}
|
|
|
|
const res = await fetch(`${Admin.BASE_URL}${path}`, options);
|
|
if (!res.ok) {
|
|
const text = await res.text();
|
|
throw new Error(`Errore API ${res.status}: ${text}`);
|
|
}
|
|
|
|
return res.json();
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// API GET / POST / DELETE
|
|
// ======================================================
|
|
function apiGet(path) {
|
|
return apiRequest("GET", path);
|
|
}
|
|
|
|
function apiPost(path, body = null) {
|
|
return apiRequest("POST", path, body);
|
|
}
|
|
|
|
function apiDelete(path) {
|
|
return apiRequest("DELETE", path);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// ENDPOINT DINAMICO (photos / activities)
|
|
// ======================================================
|
|
function apiBase(mode) {
|
|
return mode === "activities" ? "/activities" : "/photos";
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// UTILITY: LEGGI DATA/ORA DAL CAMPO SINCE (foto)
|
|
// ======================================================
|
|
function getSinceISO() {
|
|
const el = document.getElementById("sinceInput");
|
|
if (!el || !el.value) return null;
|
|
return new Date(el.value).toISOString();
|
|
}
|
|
|
|
// ======================================================
|
|
// UTILITY: LEGGI DATA/ORA DAL CAMPO SINCE (attività)
|
|
// ======================================================
|
|
function getSinceISOActivities() {
|
|
const el = document.getElementById("sinceInputActivities");
|
|
if (!el || !el.value) return null;
|
|
return new Date(el.value).toISOString();
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// FOTO — GET DB CHANGES
|
|
// ======================================================
|
|
async function getDBChanges(since, user) {
|
|
return apiGet(`${apiBase("photos")}/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// FOTO — GET HARD DELETED
|
|
// ======================================================
|
|
async function getHardDeleted(since, user) {
|
|
return apiGet(`${apiBase("photos")}/deleted_hard?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// ATTIVITÀ — GET DB CHANGES
|
|
// ======================================================
|
|
async function getActivitiesChanges(since, user) {
|
|
return apiGet(`${apiBase("activities")}/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// ATTIVITÀ — GET HARD DELETED
|
|
// ======================================================
|
|
async function getActivitiesHardDeleted(since, user) {
|
|
return apiGet(`${apiBase("activities")}/deleted_hard?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// FOTO — GET BY ID
|
|
// ======================================================
|
|
async function getPhotoById(id, user) {
|
|
return apiGet(`${apiBase("photos")}/byIds?id=${encodeURIComponent(id)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// ATTIVITÀ — GET BY ID
|
|
// ======================================================
|
|
async function getActivityById(id, user) {
|
|
return apiGet(`${apiBase("activities")}/byIds?id=${encodeURIComponent(id)}&user=${encodeURIComponent(user)}`);
|
|
}
|
|
|
|
|
|
|
|
// ======================================================
|
|
// EXPORT
|
|
// ======================================================
|
|
window.AdminAPI = {
|
|
apiGet,
|
|
apiPost,
|
|
apiDelete,
|
|
|
|
getSinceISO,
|
|
getSinceISOActivities,
|
|
|
|
getDBChanges,
|
|
getHardDeleted,
|
|
|
|
getActivitiesChanges,
|
|
getActivitiesHardDeleted,
|
|
|
|
getPhotoById,
|
|
getActivityById
|
|
};
|