From aa5b97dcc802465062e2d237c62ddd43cc55de86 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Fri, 8 Dec 2017 02:22:23 +0100 Subject: [PATCH] add: repo.event (event default 'getAll()' with limit 'getById', 'create', 'update', 'remove') --- build/database/repo/event.php | 148 ++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 build/database/repo/event.php diff --git a/build/database/repo/event.php b/build/database/repo/event.php new file mode 100644 index 0000000..a9fcdd8 --- /dev/null +++ b/build/database/repo/event.php @@ -0,0 +1,148 @@ + Nb max voulu + * + * @return events The event list + * FALSE on error + * + ---------------------------------------------------------*/ + public function getAll(int $limit){ + + /* (1) Statement */ + $st = $this->pdo->query("SELECT * FROM `event` ORDER BY `timestamp` DESC LIMIT $limit"); + + /* (2) Fetched data */ + return $st->fetchAll(); + + } + + + /* (2) Return a event by its `id_event` + * + * @id_event The event UID + * + * @return event The event if found + * FALSE on error + * + ---------------------------------------------------------*/ + public function getById(int $id_event){ + + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("SELECT * FROM `event` WHERE `id_event` = :id_event LIMIT 1"); + + /* (2) Bind variables */ + $pst->bindParam(':id_event', $id_event, \PDO::PARAM_INT); + + /* (3) Execute */ + if( !$pst->execute() ) return false; // if error -> send FALSE + + /* (4) Fetched data */ + return $pst->fetch(); + + } + + + /* (3) Creates a new event + * + * @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 event + * 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 event + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("INSERT INTO `event`(`id_event`, `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 event + * + * @id_event The event UID + * @new_msg New message content + * + * @return updated True if updated + * + ---------------------------------------------------------*/ + public function update(int $id_event, String $new_msg){ + + /* (1) Update the event + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("UPDATE `event` SET `message` = :new_msg WHERE `id_event` = :id_event"); + + /* (3) Bind variables */ + $pst->bindParam(':id_event', $id_event, \PDO::PARAM_INT); + $pst->bindParam(':new_msg', $new_msg, \PDO::PARAM_STR); + + /* (4) Execute -> if error return FALSE */ + return $pst->execute(); + + } + + + /* (5) Removes an event + * + * @id_event The event UID + * + * @return removed True has been removed + * + ---------------------------------------------------------*/ + public function remove(int $id_event){ + + /* (1) Update the event + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("DELETE FROM `event` WHERE `id_event` = :id_event"); + + /* (2) Bind variables */ + $pst->bindParam(':id_event', $id_event, \PDO::PARAM_INT); + + /* (3) Execute -> if error return FALSE */ + return $pst->execute(); + + } + + + + + } \ No newline at end of file