-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
25 refa/estacoes
- Loading branch information
Showing
28 changed files
with
1,848 additions
and
284 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { api_route } from '..'; | ||
|
||
export const deletarEstacao = async (id: number): Promise<void> => { | ||
try { | ||
const response = await fetch(`${api_route}/estacoes/${id}`, { | ||
method: 'DELETE', | ||
headers: { | ||
'Authorization': `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao deletar a estação: ${response.statusText}`); | ||
} | ||
|
||
console.log('Estação deletada com sucesso'); | ||
} catch (error) { | ||
console.error('Erro na requisição:', error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { api_route } from ".."; | ||
|
||
interface Endereco { | ||
id: number; | ||
logradouro: string; | ||
bairro: string; | ||
cidade: string; | ||
estado: string; | ||
numero: number; | ||
complemento: string; | ||
cep: string; | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
export const obterEnderecos = async (): Promise<Endereco[]> => { | ||
try { | ||
const response = await fetch(`${api_route}enderecos`, { | ||
method: "GET", | ||
headers: { | ||
"Authorization": `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao obter endereços: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data as Endereco[]; | ||
} catch (error) { | ||
console.error("Erro na requisição:", error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export const obterEnderecoPorId = async (id: number): Promise<Endereco> => { | ||
try { | ||
const response = await fetch(`${api_route}enderecos/${id}`, { | ||
method: "GET", | ||
headers: { | ||
"Authorization": `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao obter endereço: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data as Endereco; | ||
} catch (error) { | ||
console.error("Erro na requisição:", error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { api_route } from ".."; | ||
|
||
interface Estacao { | ||
id: number; | ||
nome: string; | ||
topico: string; | ||
ativo: boolean; | ||
criado: string; | ||
modificado: string; | ||
endereco_id: number; | ||
} | ||
|
||
// Função para obter o token armazenado no localStorage | ||
const obterToken = (): string | null => { | ||
return localStorage.getItem("token"); | ||
}; | ||
|
||
export const obterEstacoes = async (): Promise<Estacao[]> => { | ||
const token = obterToken(); // Obtendo o token armazenado | ||
|
||
if (!token) { | ||
throw new Error("Token não encontrado. Por favor, faça login."); | ||
} | ||
|
||
try { | ||
const response = await fetch(`${api_route}estacoes`, { | ||
method: "GET", | ||
headers: { | ||
"Authorization": `Token ${token}`, // Usando o token dinamicamente | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao obter estações: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data as Estacao[]; | ||
} catch (error) { | ||
console.error("Erro na requisição:", error); | ||
throw error; | ||
} | ||
}; | ||
|
||
export const obterEstacaoPorId = async (id: number): Promise<Estacao> => { | ||
const token = obterToken(); // Obtendo o token armazenado | ||
|
||
if (!token) { | ||
throw new Error("Token não encontrado. Por favor, faça login."); | ||
} | ||
|
||
try { | ||
const response = await fetch(`${api_route}estacoes/${id}`, { | ||
method: "GET", | ||
headers: { | ||
"Authorization": `Token ${token}`, // Usando o token dinamicamente | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao obter estação: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data as Estacao; | ||
} catch (error) { | ||
console.error("Erro na requisição:", error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { api_route } from ".."; | ||
|
||
interface Endereco { | ||
logradouro: string; | ||
bairro: string; | ||
cidade: string; | ||
estado: string; | ||
numero: number; | ||
complemento: string; | ||
cep: string; | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
export const criarEndereco = async (endereco: Endereco): Promise<Endereco> => { | ||
try { | ||
const response = await fetch(`${api_route}enderecos`, { | ||
method: "POST", | ||
headers: { | ||
"Authorization": `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(endereco), | ||
}); | ||
|
||
const responseData = await response.json(); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao criar o endereço: ${response.statusText}`); | ||
} | ||
|
||
return responseData; | ||
} catch (error) { | ||
console.error("Erro na requisição do endereço:", error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { api_route } from ".."; | ||
import { criarEndereco } from "@/app/_api/post/enderecos"; | ||
|
||
interface Estacao { | ||
nome: string; | ||
topico: string; | ||
ativo: boolean; | ||
criado: string; | ||
modificado: string; | ||
} | ||
|
||
interface Endereco { | ||
logradouro: string; | ||
bairro: string; | ||
cidade: string; | ||
estado: string; | ||
numero: number; | ||
complemento: string; | ||
cep: string; | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
// Remova o segundo parâmetro 'endereco' porque ele já foi tratado antes | ||
export const criarEstacao = async (estacao: Estacao, enderecoId: number): Promise<any> => { | ||
console.log('Função criarEstacao foi chamada'); | ||
try { | ||
// Criando o payload da estação com o endereco (ID) correto | ||
const estacaoComEndereco = { ...estacao, endereco: enderecoId }; // Usar "endereco" diretamente | ||
|
||
console.log('Payload enviado:', estacaoComEndereco); | ||
|
||
const response = await fetch(`${api_route}estacoes`, { | ||
method: "POST", | ||
headers: { | ||
"Authorization": `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify(estacaoComEndereco), | ||
}); | ||
|
||
console.log('Resposta da API ao criar a estação:', response); | ||
|
||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
console.error('Erro ao criar a estação:', errorData); | ||
throw new Error(`Erro ao criar a estação: ${response.statusText}`); | ||
} | ||
|
||
return await response.json(); | ||
} catch (error) { | ||
console.error("Erro na requisição:", error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { api_route } from '..'; | ||
|
||
interface Endereco { | ||
id: number, | ||
logradouro: string; | ||
bairro: string; | ||
cidade: string; | ||
estado: string; | ||
numero: number; | ||
complemento: string; | ||
cep: string; | ||
latitude: number; | ||
longitude: number; | ||
} | ||
|
||
export const atualizarEndereco = async ( | ||
endereco: Endereco | ||
): Promise<any> => { | ||
try { | ||
const response = await fetch(`${api_route}enderecos/${endereco.id}`, { | ||
method: 'PUT', | ||
headers: { | ||
'Authorization': `Token 2948c11eaf985f44737d8fa84db99846e8197fae`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(endereco), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error(`Erro ao atualizar o Endereço: ${response.statusText}`); | ||
} | ||
|
||
const data = await response.json(); | ||
return data as Endereco[]; | ||
} catch (error) { | ||
console.error('Erro na requisição:', error); | ||
throw error; | ||
} | ||
}; |
Oops, something went wrong.