From 69df30d57c394d82958504952fa48bf13085da59 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 8 Dec 2017 02:20:05 +0100 Subject: [PATCH] add: repo.emergency (emergency default 'getAll()' with limit 'getById', 'create', 'update', 'remove') --- build/database/repo/emergency.php | 148 ++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 build/database/repo/emergency.php diff --git a/build/database/repo/emergency.php b/build/database/repo/emergency.php new file mode 100644 index 0000000..026ff62 --- /dev/null +++ b/build/database/repo/emergency.php @@ -0,0 +1,148 @@ + Nb max voulu + * + * @return emergencies The emergency list + * FALSE on error + * + ---------------------------------------------------------*/ + public function getAll(int $limit){ + + /* (1) Statement */ + $st = $this->pdo->query("SELECT * FROM `emergency` ORDER BY `timestamp` DESC LIMIT $limit"); + + /* (2) Fetched data */ + return $st->fetchAll(); + + } + + + /* (2) Return a emergency by its `id_emergency` + * + * @id_emergency The emergency UID + * + * @return emergency The emergency if found + * FALSE on error + * + ---------------------------------------------------------*/ + public function getById(int $id_emergency){ + + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("SELECT * FROM `emergency` WHERE `id_emergency` = :id_emergency LIMIT 1"); + + /* (2) Bind variables */ + $pst->bindParam(':id_emergency', $id_emergency, \PDO::PARAM_INT); + + /* (3) Execute */ + if( !$pst->execute() ) return false; // if error -> send FALSE + + /* (4) Fetched data */ + return $pst->fetch(); + + } + + + /* (3) Creates a new emergency + * + * @id_user The user ID if defined + * @name The user name (if not connected) + * @timestamp The timestamp + * @message The message content + * @type The message type + * @latitude Sender latitude + * @longitude Sender longitude + * @dep Departement id + * + * @return id_created UID of the created emergency + * FALSE on error + * + ---------------------------------------------------------*/ + public function create($id_user, String $name, int $timestamp, String $message, int $type, float $latitude, float $longitude, int $dep){ + + /* (2) Create the emergency + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("INSERT INTO `emergency`(`id_emergency`, `id_user`, `timestamp`, `name`, `message`, `type`, `latitude`, `longitude`, `dep`) + VALUES(DEFAULT, :id_user, :timestamp, :name, :message, :type, :latitude, :longitude, :dep)"); + + /* (3) Bind variables */ + $pst->bindParam(':id_user', $id_user, \PDO::PARAM_INT); + $pst->bindParam(':timestamp', $timestamp, \PDO::PARAM_INT); + $pst->bindParam(':name', $name, \PDO::PARAM_STR, 50); + $pst->bindParam(':message', $message, \PDO::PARAM_STR); + $pst->bindParam(':type', $type, \PDO::PARAM_INT); + $pst->bindParam(':latitude', $latitude, \PDO::PARAM_STR); + $pst->bindParam(':longitude', $longitude, \PDO::PARAM_STR); + $pst->bindParam(':dep', $dep, \PDO::PARAM_STR, 2); + + /* (4) Execute -> if error return FALSE */ + if( !$pst->execute() ) return false; + + + /* (2) Get the id + ---------------------------------------------------------*/ + /* (1) Get last inserted id */ + return $this->pdo->lastInsertId; + + } + + + /* (4) Updates an emergency + * + * @id_emergency The emergency UID + * @new_msg New message content + * + * @return updated True if updated + * + ---------------------------------------------------------*/ + public function update(int $id_emergency, String $new_msg){ + + /* (1) Update the emergency + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("UPDATE `emergency` SET `message` = :new_msg WHERE `id_emergency` = :id_emergency"); + + /* (3) Bind variables */ + $pst->bindParam(':id_emergency', $id_emergency, \PDO::PARAM_INT); + $pst->bindParam(':new_msg', $new_msg, \PDO::PARAM_STR); + + /* (4) Execute -> if error return FALSE */ + return $pst->execute(); + + } + + + /* (5) Removes an emergency + * + * @id_emergency The emergency UID + * + * @return removed True has been removed + * + ---------------------------------------------------------*/ + public function remove(int $id_emergency){ + + /* (1) Update the emergency + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("DELETE FROM `emergency` WHERE `id_emergency` = :id_emergency"); + + /* (2) Bind variables */ + $pst->bindParam(':id_emergency', $id_emergency, \PDO::PARAM_INT); + + /* (3) Execute -> if error return FALSE */ + return $pst->execute(); + + } + + + + + } \ No newline at end of file