Skip to content

Commit

Permalink
feat: pergunta sugerida respondido por
Browse files Browse the repository at this point in the history
  • Loading branch information
rrafaelc committed Nov 12, 2023
1 parent bd48846 commit 2b94c67
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
5 changes: 4 additions & 1 deletion Banco de dados SQL/script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ CREATE TABLE pergunta_sugerida (
pergunta TEXT NOT NULL,
email TEXT,
telefone TEXT,
respondido_por INT(11),
foi_respondido BOOLEAN NOT NULL DEFAULT false,
criado_em DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
atualizado_em DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
atualizado_em DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY (respondido_por) REFERENCES usuario(id) ON DELETE SET NULL
);

INSERT INTO pergunta (pergunta, resposta) VALUES
Expand Down
17 changes: 13 additions & 4 deletions api/src/Controllers/PerguntaSugeridaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ private function processResourceRequest(string $method, string $id): void

switch ($method) {
case "GET":
$usuarioLogado = $this->authController->verifyAccessToken($this->config, $this->token);

if (!$usuarioLogado) return;

echo json_encode($perguntaSugerida);
break;
case "DELETE":
case "PUT":
$usuarioLogado = $this->authController->verifyAccessToken($this->config, $this->token);

if (!$usuarioLogado) return;
Expand All @@ -46,21 +50,26 @@ private function processResourceRequest(string $method, string $id): void
return;
}

$this->gateway->delete($id);
$perguntaSugeridaAtualizada = $this->gateway->update($id, $usuarioLogado["id"]);

http_response_code(204);
http_response_code(200);
echo json_encode($perguntaSugeridaAtualizada);
break;

default:
http_response_code(405);
header("Allow: GET, DELETE");
header("Allow: GET, PUT");
}
}

private function processCollectionRequest(string $method): void
{
switch ($method) {
case "GET":
$usuarioLogado = $this->authController->verifyAccessToken($this->config, $this->token);

if (!$usuarioLogado) return;

echo json_encode($this->gateway->getAll());
break;
case "POST":
Expand Down
15 changes: 13 additions & 2 deletions api/src/Gateways/PerguntaSugeridaGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,24 @@ public function create(array $data): array | false
return $perguntaSugerida;
}

public function delete(string $id): void
public function update(string $id, string $userId)
{
$sql = "DELETE FROM pergunta_sugerida
$sql = "UPDATE pergunta_sugerida
SET respondido_por = :respondido_por, foi_respondido = :foi_respondido
WHERE id = :id";

$stmt = $this->conn->prepare($sql);
$stmt->bindValue(":respondido_por", $userId, PDO::PARAM_INT);
$stmt->bindValue(":foi_respondido", true, PDO::PARAM_BOOL);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();

$sql = "SELECT * FROM pergunta_sugerida WHERE id = :id";
$stmt = $this->conn->prepare($sql);
$stmt->bindValue(":id", $id, PDO::PARAM_INT);
$stmt->execute();
$perguntaSugerida = $stmt->fetch(PDO::FETCH_ASSOC);

return $perguntaSugerida;
}
}
28 changes: 24 additions & 4 deletions postman/Faq Fatec - Api.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "d7f7e3e2-6c89-4c08-9b6e-b1b09d836e9a",
"_postman_id": "38a65899-11b9-4ad7-a011-1066151ef974",
"name": "Faq Fatec - Api",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "30845207"
"_exporter_id": "23523136"
},
"item": [
{
Expand Down Expand Up @@ -609,6 +609,16 @@
{
"name": "Listar",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
Expand All @@ -626,6 +636,16 @@
{
"name": "Buscar",
"request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{token}}",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
Expand All @@ -642,7 +662,7 @@
"response": []
},
{
"name": "Deletar",
"name": "Responder",
"request": {
"auth": {
"type": "bearer",
Expand All @@ -654,7 +674,7 @@
}
]
},
"method": "DELETE",
"method": "PUT",
"header": [],
"url": {
"raw": "{{base_url}}/pergunta-sugerida/1",
Expand Down

0 comments on commit 2b94c67

Please sign in to comment.