diff --git a/scripts/perguntas/buscarPergunta.js b/scripts/perguntas/buscarPergunta.js new file mode 100644 index 0000000..a3738bf --- /dev/null +++ b/scripts/perguntas/buscarPergunta.js @@ -0,0 +1,22 @@ +import { apiUrl } from '../constants/apiUrl.js'; + +export const buscarPergunta = async ({ id }) => { + try { + const response = await fetch(`${apiUrl}/pergunta/${id}`, { + method: 'GET', + }); + + if (!response.ok) { + const err = await response.json(); + console.log(err); + throw new Error(err.errors[0]); + } + + const pergunta = await response.json(); + + return pergunta; + } catch (error) { + console.log(error); + throw new Error(error.message); + } +}; diff --git a/scripts/perguntas/editarPergunta.js b/scripts/perguntas/editarPergunta.js new file mode 100644 index 0000000..3b7997c --- /dev/null +++ b/scripts/perguntas/editarPergunta.js @@ -0,0 +1,34 @@ +import { apiUrl } from '../constants/apiUrl.js'; +import { getAuthorization } from '../utils/getAuthorization.js'; + +export const editarPergunta = async ({ id, pergunta, resposta, prioridade }) => { + try { + const response = await fetch(`${apiUrl}/pergunta/${id}`, { + method: 'PATCH', + body: JSON.stringify({ + pergunta, + resposta, + prioridade, + }), + headers: { + 'Content-Type': 'application/json', + Authorization: await getAuthorization(), + }, + }); + + if (response.status === 403) { + const err = await response.json(); + console.log(err); + throw new Error(err.errors[0]); + } + + if (!response.ok) { + const err = await response.json(); + console.log(err); + throw new Error(err.errors[0]); + } + } catch (error) { + console.log(error); + throw new Error(error.message); + } +}; diff --git a/sistema/perguntas/editar/index.php b/sistema/perguntas/editar/index.php index 32ab1c0..89fd0fd 100644 --- a/sistema/perguntas/editar/index.php +++ b/sistema/perguntas/editar/index.php @@ -13,6 +13,8 @@ + +