add: repo.emergency (emergency default 'getAll()' with limit 'getById', 'create', 'update', 'remove')

This commit is contained in:
xdrm-brackets 2017-12-08 02:20:05 +01:00
parent 66d8eb39eb
commit d8156b3f55
1 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,148 @@
<?php
namespace database\repo;
use \database\core\Repo_i;
class emergency extends Repo_i{
/* (1) Return all emergencies in database
*
* @limit<int> Nb max voulu
*
* @return emergencies<array> 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<int> The emergency UID
*
* @return emergency<array> 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<int|null> The user ID if defined
* @name<String> The user name (if not connected)
* @timestamp<int> The timestamp
* @message<String> The message content
* @type<int> The message type
* @latitude<int> Sender latitude
* @longitude<int> Sender longitude
* @dep<int> Departement id
*
* @return id_created<int> 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<int> The emergency UID
* @new_msg<String> New message content
*
* @return updated<bool> 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<int> The emergency UID
*
* @return removed<bool> 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();
}
}