Kaynağa Gözat

New Description Routes

EduLascala 5 ay önce
ebeveyn
işleme
c89fc5a83a

+ 37 - 0
controllers/DescriptionCreateController.php

@@ -0,0 +1,37 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\DescriptionModel;
+use Psr\Http\Message\ServerRequestInterface;
+use Respect\Validation\Validator as v;
+use Respect\Validation\Exceptions\ValidationException;
+
+class DescriptionCreateController
+{
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string) $request->getBody(), true) ?? [];
+
+        try {
+            v::key('product_id', v::intType()->positive())
+             ->key('company_id', v::intType()->positive())
+             ->key('description_text', v::stringType()->notEmpty())
+             ->assert($body);
+        } catch (ValidationException $e) {
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $model = new DescriptionModel();
+        $result = $model->addDescription(
+            (int)$body['product_id'],
+            (int)$body['company_id'],
+            (string)$body['description_text']
+        );
+
+        return $result
+            ? ResponseLib::sendOk(['created' => true])
+            : ResponseLib::sendFail("Erro ao inserir descrição", [], "E_DB")->withStatus(500);
+    }
+}

+ 30 - 0
controllers/DescriptionGetController.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\DescriptionModel;
+use Psr\Http\Message\ServerRequestInterface;
+use Respect\Validation\Validator as v;
+use Respect\Validation\Exceptions\ValidationException;
+
+class DescriptionGetController
+{
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string) $request->getBody(), true) ?? [];
+
+        try {
+            v::key('company_id', v::intType()->positive())->assert($body);
+        } catch (ValidationException $e) {
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $companyId = (int) $body['company_id'];
+
+        $model = new DescriptionModel();
+        $data = $model->getDescriptionsByCompany($companyId);
+
+        return ResponseLib::sendOk($data);
+    }
+}

+ 35 - 0
controllers/DescriptionUpdateController.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Controllers;
+
+use Libs\ResponseLib;
+use Models\DescriptionModel;
+use Psr\Http\Message\ServerRequestInterface;
+use Respect\Validation\Validator as v;
+use Respect\Validation\Exceptions\ValidationException;
+
+class DescriptionUpdateController
+{
+    public function __invoke(ServerRequestInterface $request)
+    {
+        $body = json_decode((string) $request->getBody(), true) ?? [];
+
+        try {
+            v::key('description_id', v::intType()->positive())
+             ->key('description_text', v::stringType()->notEmpty())
+             ->assert($body);
+        } catch (ValidationException $e) {
+            return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
+        }
+
+        $descriptionId = (int) $body['description_id'];
+        $descriptionText = (string) $body['description_text'];
+
+        $model = new DescriptionModel();
+        $success = $model->updateDescription($descriptionId, $descriptionText);
+
+        return $success
+            ? ResponseLib::sendOk(['updated' => true])
+            : ResponseLib::sendFail("Erro ao atualizar descrição", [], "E_DB")->withStatus(500);
+    }
+}

+ 26 - 17
migrations/migrations_v1.sql

@@ -1,10 +1,10 @@
-CREATE TABLE "company" (
+CREATE TABLE IF NOT EXISTS "company" (
     "company_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_name" TEXT NOT NULL,
     "company_flag" TEXT NOT NULL
 );
 
-CREATE TABLE "role" (
+CREATE TABLE IF NOT EXISTS "role" (
     "role_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "role_name" TEXT NOT NULL,
@@ -13,12 +13,21 @@ CREATE TABLE "role" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "status" (
+CREATE TABLE IF NOT EXISTS "status" (
     "status_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "status_status" TEXT NOT NULL
 );
 
-CREATE TABLE "user" (
+CREATE TABLE IF NOT EXISTS "description" (
+  description_id INTEGER PRIMARY KEY AUTOINCREMENT,
+  description_text TEXT NOT NULL,
+  product_id INTEGER NOT NULL,
+  company_id INTEGER NOT NULL,
+  FOREIGN KEY (product_id) REFERENCES product(product_id),
+  FOREIGN KEY (company_id) REFERENCES company(company_id)
+);
+
+CREATE TABLE IF NOT EXISTS "user" (
     "user_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "user_name" TEXT NOT NULL,
     "user_email" TEXT NOT NULL UNIQUE,
@@ -30,7 +39,7 @@ CREATE TABLE "user" (
     FOREIGN KEY ("role_id") REFERENCES "role" ("role_id")
 );
 
-CREATE TABLE "table" (
+CREATE TABLE IF NOT EXISTS "table" (
     "table_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "table_number" TEXT NOT NULL,
@@ -40,7 +49,7 @@ CREATE TABLE "table" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "category" (
+CREATE TABLE IF NOT EXISTS "category" (
     "category_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "category_name" TEXT NOT NULL,
@@ -48,7 +57,7 @@ CREATE TABLE "category" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "product" (
+CREATE TABLE IF NOT EXISTS "product" (
     "product_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "category_id" INTEGER NOT NULL,
@@ -60,7 +69,7 @@ CREATE TABLE "product" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "order" (
+CREATE TABLE IF NOT EXISTS "order" (
     "order_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "table_id" INTEGER NOT NULL,
     "user_id" INTEGER NOT NULL,
@@ -76,7 +85,7 @@ CREATE TABLE "order" (
     FOREIGN KEY ("status_id") REFERENCES "status" ("status_id")
 );
 
-CREATE TABLE "order_item" (
+CREATE TABLE IF NOT EXISTS "order_item" (
     "order_item_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "order_id" INTEGER NOT NULL,
     "product_id" INTEGER NOT NULL,
@@ -86,7 +95,7 @@ CREATE TABLE "order_item" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "sale" (
+CREATE TABLE IF NOT EXISTS "sale" (
     "sale_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "order_id" INTEGER NOT NULL,
@@ -99,7 +108,7 @@ CREATE TABLE "sale" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE "fee" (
+CREATE TABLE IF NOT EXISTS "fee" (
     "fee_id" INTEGER PRIMARY KEY AUTOINCREMENT,
     "company_id" INTEGER NOT NULL,
     "fee_fixed" TEXT NOT NULL,
@@ -108,10 +117,10 @@ CREATE TABLE "fee" (
     FOREIGN KEY ("company_id") REFERENCES "company" ("company_id")
 );
 
-CREATE TABLE api_key (
-    api_key_id INTEGER PRIMARY KEY AUTOINCREMENT,
-    user_id INTEGER NOT NULL,
-    api_key_user TEXT NOT NULL,
-    api_key_secret TEXT NOT NULL,
-    FOREIGN KEY (user_id) REFERENCES user(user_id)
+CREATE TABLE IF NOT EXISTS "api_key" (
+    "api_key_id" INTEGER PRIMARY KEY AUTOINCREMENT,
+    "user_id" INTEGER NOT NULL,
+    "api_key_user" TEXT NOT NULL,
+    "api_key_secret" TEXT NOT NULL,
+    FOREIGN KEY ("user_id") REFERENCES "user" ("user_id")
 );

+ 53 - 0
models/DescriptionModel.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace Models;
+
+class DescriptionModel
+{
+    private \PDO $pdo;
+
+    public function __construct()
+    {
+        $dbFile = $_ENV['DB_FILE'];
+        $dbPath = __DIR__ . '/../' . $dbFile;
+        $this->pdo = new \PDO("sqlite:" . $dbPath);
+        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
+    }
+
+    public function getDescriptionsByCompany(int $companyId): array
+{
+    $stmt = $this->pdo->prepare("
+        SELECT d.description_id, d.description_text, d.product_id
+        FROM description d
+        WHERE d.company_id = :company_id
+    ");
+    $stmt->execute(['company_id' => $companyId]);
+
+    return $stmt->fetchAll(\PDO::FETCH_ASSOC);
+}
+
+    public function addDescription(int $productId, int $companyId, string $text): bool
+    {
+        $stmt = $this->pdo->prepare("
+            INSERT INTO description (description_text, product_id, company_id)
+            VALUES (:text, :product_id, :company_id)
+        ");
+        return $stmt->execute([
+            'text' => $text,
+            'product_id' => $productId,
+            'company_id' => $companyId,
+        ]);
+    }
+
+    public function updateDescription(int $descriptionId, string $text): bool
+    {
+        $stmt = $this->pdo->prepare("
+            UPDATE description SET description_text = :text
+            WHERE description_id = :id
+        ");
+        return $stmt->execute([
+            'text' => $text,
+            'id' => $descriptionId,
+        ]);
+    }
+}

+ 13 - 7
public/index.php

@@ -44,41 +44,47 @@ $app->get('/hmachelloworld', $cors, $authHmac, \Controllers\HelloController::cla
 $app->get('/jwthelloworld', $cors, $authJwt,  \Controllers\HelloController::class);
 
 
-//Rotas User
+// User Routes
 $app->post('/login', $cors, \Controllers\LoginController::class);
 $app->post('/register', $cors, \Controllers\RegisterController::class);
 $app->post('/user/get', $cors, $authJwt, \Controllers\UserGetController::class);
 $app->post('/user/delete', $cors, $authJwt, \Controllers\UserDeleteController::class);
 
 
-//Rotas Category
+// Category Routes
 $app->post('/category/get', $cors, \Controllers\CategoryGetController::class);
 $app->post('/category/create', $cors, $authJwt, \Controllers\CategoryCreateController::class);
 $app->post('/category/delete', $cors, $authJwt, \Controllers\CategoryDeleteController::class);
 $app->post('/category/updatef', $cors, $authJwt, \Controllers\CategoryUpdateFlagController::class);
 
-//Rotas Product 
+// Product Routes
 $app->post('/product/get', $cors, \Controllers\ProductGetController::class);
 $app->post('/product/create', $cors, $authJwt, \Controllers\ProductCreateController::class);
 $app->post('/product/update', $cors, $authJwt, \Controllers\ProductUpdateController::class);
 $app->post('/product/delete', $cors, $authJwt, \Controllers\ProductDeleteController::class);
 
-//Rotas Table
-$app->post('/table/get', $cors, $authJwt, \Controllers\TableGetController::class); 
+// Table Routes
+$app->post('/table/get', $cors, $authJwt, \Controllers\TableGetController::class);
 $app->post('/table/create', $cors, $authJwt, \Controllers\TableCreateController::class);
 $app->post('/table/delete', $cors, $authJwt, \Controllers\TableDeleteController::class);
 $app->post('/table/update', $cors, $authJwt, \Controllers\TableUpdateController::class);
 
-// Order
+// Order Routes
 $app->post('/order/create', $cors, $authJwt, \Controllers\OrderCreateController::class);
 $app->post('/order/delete', $cors, $authJwt, \Controllers\OrderDeleteController::class);
 $app->post('/order/get', $cors, $authJwt, \Controllers\OrderGetController::class);
 $app->post('/order/update', $cors, $authJwt, \Controllers\OrderUpdateController::class);
 
-// Order Item
+// Order Item Routes
 $app->post('/order_item/create', $cors, $authJwt, \Controllers\OrderItemCreateController::class);
 $app->post('/order_item/delete', $cors, $authJwt, \Controllers\OrderItemDeleteController::class);
 $app->post('/order_item/get', $cors, $authJwt, \Controllers\OrderItemGetController::class);
 
+// Description Routes
+$app->post('/description/get', $cors, \Controllers\DescriptionGetController::class);
+$app->post('/description/create', $cors, $authJwt, \Controllers\DescriptionCreateController::class);
+$app->post('/description/update', $cors, $authJwt, \Controllers\DescriptionUpdateController::class);
+
+
 
 $app->run();