Procházet zdrojové kódy

route /token/get now also requires commodities_name as a filter

EduLascala před 2 týdny
rodič
revize
6a2d0ff28f
2 změnil soubory, kde provedl 16 přidání a 2 odebrání
  1. 4 2
      controllers/TokenGetController.php
  2. 12 0
      models/TokenModel.php

+ 4 - 2
controllers/TokenGetController.php

@@ -23,13 +23,15 @@ class TokenGetController
 
         try {
             val::key('token_uf', val::stringType()->notEmpty()->length(1, 100))
+                ->key('commodities_name', val::stringType()->notEmpty()->length(1, 255))
                 ->assert($body);
         } catch (ValidationException $e) {
             return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400);
         }
 
-        $uf = (string)($body['token_uf'] ?? '');
-        $tokens = $this->model->getByUf($uf);
+        $uf = (string)$body['token_uf'];
+        $commoditiesName = (string)$body['commodities_name'];
+        $tokens = $this->model->getByUfAndCommodityName($uf, $commoditiesName);
 
         return $tokens
             ? ResponseLib::sendOk($tokens)

+ 12 - 0
models/TokenModel.php

@@ -21,5 +21,17 @@ class TokenModel
         $stmt->execute(['uf' => $uf]);
         return $stmt->fetchAll(\PDO::FETCH_ASSOC);
     }
+
+    public function getByUfAndCommodityName(string $uf, string $commoditiesName): array
+    {
+        $sql = 'SELECT t.token_id, t.token_external_id, t.token_commodities_amount, t.token_commodities_value, t.token_uf, t.token_city, t.token_content, t.token_flag, t.wallet_id, t.chain_id, t.commodities_id, t.cpr_id, t.user_id
+                FROM "token" t
+                JOIN "commodities" c ON c.commodities_id = t.commodities_id
+                WHERE t.token_uf = :uf AND LOWER(TRIM(c.commodities_name)) = LOWER(TRIM(:name))
+                ORDER BY t.token_id';
+        $stmt = $this->pdo->prepare($sql);
+        $stmt->execute(['uf' => $uf, 'name' => $commoditiesName]);
+        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
+    }
 }