import { base, getToken } from './_config' export function getUrlImagen(id_paradero) { return `${base}/paraderos-image/${id_paradero}` } export async function getParaderos(params) { const query = !params ? '' : '?' + (new URLSearchParams(params).toString()); const res = await fetch(`${base}/paraderos/${query}`, { headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function getParadero(rut) { const res = await fetch(`${base}/paraderos/${rut}/`, { headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function createParadero(data) { const res = await fetch(`${base}/paraderos/`, { method: 'POST', body: JSON.stringify(data), headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function updateParadero({ id_paradero: id = null, ...data }) { const res = await fetch(`${base}/paraderos/${id}/`, { method: 'PATCH', body: JSON.stringify(data), headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function deleteParadero(id) { const res = await fetch(`${base}/paraderos/${id}/`, { method: 'DELETE', headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function getParaderoImagenes(id_paradero) { const res = await fetch(`${base}/paraderos-image/?id_paradero=${id_paradero}`, { headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function createParaderoImagen(id_paradero, file) { const form = new FormData() form.append('id_paradero', id_paradero) form.append('imagen', file) form.append('content_type', file.type) const res = await fetch(`${base}/paraderos-image/`, { method: 'POST', body: form, headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.text() return res.json() } export async function deleteParaderoImagen(id_paradero_imagen) { const res = await fetch(`${base}/paraderos-image/${id_paradero_imagen}/`, { method: 'DELETE', headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.text() return res.text() } export async function getLineasParadero(id_paradero) { const res = await fetch(`${base}/lineas-paradero/?id_paradero=${id_paradero}`, { headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.text() return res.json() } export async function createLineaParadero(id_paradero, id_linea) { const res = await fetch(`${base}/lineas-paradero/`, { method: 'POST', body: JSON.stringify({ id_paradero, id_linea }), headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function deleteLineaParadero(id_paradero_linea) { const res = await fetch(`${base}/lineas-paradero/${id_paradero_linea}/`, { method: 'DELETE', headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.text() return res.text() } export async function getInfoPublic(id_paradero) { const res = await fetch(`${base}/paraderos/info-public/${id_paradero}/`, { headers: { "Authorization": `Bearer ${getToken()}` } }) if (!res.ok) throw await res.text() return res.json() } export async function getCount(params) { const query = !params ? '' : '?' + (new URLSearchParams(params).toString()); const res = await fetch(`${base}/paraderos/count/${query}`, { headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }) if (!res.ok) throw await res.text() return res.json() } export async function getCountByComuna(params) { const query = !params ? '' : '?' + (new URLSearchParams(params).toString()); const res = await fetch(`${base}/paraderos/count_by_comuna/${query}`, { headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } }); if (!res.ok) throw new Error(await res.text()); return res.json(); }