Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix filter and feat same answer #182

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/src/Controllers/PerguntaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ private function processCollectionRequest(string $method, array $ordenacao): voi
break;
}

$perguntaExiste = $this->gateway->perguntaExists($data["pergunta"]);

if ($perguntaExiste) {
http_response_code(422);
echo json_encode([
"status" => "error",
"errors" => ["Essa pergunta já existe no banco"]
]);
break;
}

$perguntaCriada = $this->gateway->create($data);

http_response_code(201);
Expand Down
14 changes: 14 additions & 0 deletions api/src/Gateways/PerguntaGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,18 @@ public function getTotalCurtidas()
$data = $stmt->fetch(PDO::FETCH_ASSOC);
return intval($data['total_curtidas']);
}

public function perguntaExists(string $pergunta)
{
$perguntaLowerCase = strtolower($pergunta);

$sql = "SELECT COUNT(*) AS total FROM pergunta WHERE LOWER(pergunta) = :pergunta";
$stmt = $this->conn->prepare($sql);
$stmt->bindParam(':pergunta', $perguntaLowerCase, PDO::PARAM_STR);
$stmt->execute();

$result = $stmt->fetch(PDO::FETCH_ASSOC);

return $result['total'] > 0;
}
}
19 changes: 10 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,19 @@ autoCompleteJS.input.addEventListener('input', async function (event) {
}
});

autoCompleteJS.input.addEventListener('blur', async function (event) {
if (loading) return;
if (!event.target.value) {
await renderPerguntas();
}
});
let timeoutId;

autoCompleteJS.input.addEventListener('results', async function (event) {
clearTimeout(timeoutId);
if (loading) return;
await renderPerguntas({
titulo: event.detail.query,
});

timeoutId = setTimeout(async () => {
if (autoCompleteJS.input.value) {
await renderPerguntas({
titulo: event.detail.query,
});
}
}, 300);
});

autoCompleteJS.input.addEventListener('selection', async function (event) {
Expand Down
6 changes: 5 additions & 1 deletion scripts/perguntas/criarPergunta.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export const criarPergunta = async ({ pergunta, resposta, prioridade }) => {
}),
});

if (!response.ok) return false;
if (!response.ok) {
const err = await response.json();
console.log(err.errors[0]);
return false;
}
} catch (error) {
console.log(error);
return false;
Expand Down
19 changes: 10 additions & 9 deletions sistema/pesquisar/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,18 +319,19 @@ const execute = async () => {
}
});

autoCompleteJS.input.addEventListener('blur', async function (event) {
if (loading) return;
if (!event.target.value) {
await renderPerguntas();
}
});
let timeoutId;

autoCompleteJS.input.addEventListener('results', async function (event) {
clearTimeout(timeoutId);
if (loading) return;
await renderPerguntas({
titulo: event.detail.query,
});

timeoutId = setTimeout(async () => {
if (autoCompleteJS.input.value) {
await renderPerguntas({
titulo: event.detail.query,
});
}
}, 300);
});

autoCompleteJS.input.addEventListener('selection', async function (event) {
Expand Down