Skip to content

Commit

Permalink
Merge pull request #182 from rrafaelc/fix/filter
Browse files Browse the repository at this point in the history
Fix/filter
  • Loading branch information
rrafaelc authored Dec 5, 2023
2 parents b9b484b + 6abd164 commit 73c32aa
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 19 deletions.
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

0 comments on commit 73c32aa

Please sign in to comment.