--- /dev/null
+<?php
+
+/**
+ * Bookmark short summary.
+ *
+ * Bookmark description.
+ *
+ * @version 1.0
+ * @author Ferrand
+ */
+class BookmarkAPI extends RestAPI {
+ use dontAllowPut, dontAllowPutCollection;
+ use dontAllowDeleteCollection;
+ use dontAllowPost;
+ use dontAllowGet, dontAllowGetCollection;
+
+ protected function GETCollectionAction() {
+
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Bookmark');
+ $response->execute();
+
+ if($response === false) {
+ $this->respondeError();
+ return;
+ }
+
+ $data = array();
+ while (($row = $response->fetch()) !== false) {
+ $serverBookmark = new ServerBookmark();
+ $serverBookmark->id = $row['id'];
+ $serverBookmark->username = $row['userId'];
+ $serverBookmark->projectID = $row['projectId'];
+ $serverBookmark->creationDate = $row['creationDate'];
+
+ array_push($data, $serverBookmark);
+ }
+ $this->responde(200, $data);
+
+ }
+
+ protected function GETAction() {
+
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Bookmark
+ WHERE id = :id
+ ');
+ $response->execute(['id' => $this->element]);
+
+ if($response === false) {
+ $this->respondeError();
+ return; // end this
+ }
+
+ $row = $response->fetch(PDO::FETCH_ASSOC) ;
+ if($row === false) {
+ $this->respondeNoResult();
+ return; // end this
+ }
+
+ $serverBookmark = new ServerBookmark();
+ $serverBookmark->id = $row['id'];
+ $serverBookmark->username = $row['userId'];
+ $serverBookmark->projectID = $row['projectId'];
+ $serverBookmark->creationDate = $row['creationDate'];
+
+ $this->responde(200, $serverBookmark);
+
+ }
+
+
+
+
+ protected function POSTCollectionAction() {
+ // TODO
+ $dbh = $this->databaseHelper();
+
+ $data = $this->getBodyData();
+ $user = $this->authentication();
+
+ /*
+ if($user === null or $user['pseudo'] !== $data->username) {
+ $this->respondeUnauthorized();
+ return; // end this!
+ }
+ */
+
+ $response = $dbh->prepare('SELECT * FROM pub_Bookmark
+ WHERE userId = :userId and projectId = :projectId
+ ');
+ $response->execute([':userId' => $data->username, ':projectId' => $data->projectID]);
+
+ if($response === false) {
+ $this->respondeError();
+ return; // end this
+ }
+
+ $row = $response->fetch(PDO::FETCH_ASSOC) ;
+ if($row !== false) {
+ $this->respondeNotAcceptable();
+ return; // end this
+ }
+
+ $request = 'INSERT INTO `pub_Bookmark` (
+ `id` , `creationDate`, `userId` , `projectId`)
+ VALUES ("" , CURRENT_DATE(), :userId, :projectId);';
+
+ $response = $dbh->prepare($request);
+ $response->execute([
+ ':userId' => $data->username,
+ ':projectId' => $data->projectID,
+ ]);
+
+ if($response === false) { // Server/database fails
+ $this->respondeError();
+ }
+
+ $rowAffected = new RowAffected();
+ $rowAffected->id = $dbh->lastInsertId();
+
+ $this->responde(201, $rowAffected);
+ }
+
+ protected function DELETEAction() {
+
+
+ //
+ // On ne peut pas passser de body dans une methode delete
+ //
+
+ $dbh = $this->databaseHelper();
+
+ $user = $this->authentication();
+
+ /*
+ if($user === null or $user['pseudo'] !== $data->username) {
+ $this->respondeUnauthorized();
+ return; // end this!
+ }
+ */
+
+ /*
+ $response = $dbh->prepare('SELECT * FROM pub_Bookmark
+ WHERE userId = :userId and projectId = :projectId
+ ');
+ $response->execute([':userId' => $data->username, ':projectId' => $data->projectID]);
+
+
+
+ if($response === false) {
+ $this->respondeError();
+ return; // end this
+ }
+
+ // Le bookmark n'existe pas
+ $row = $response->fetch(PDO::FETCH_ASSOC) ;
+ if($row == false) {
+ $this->respondeNotAcceptable();
+ return; // end this
+ }
+ */
+ $request = 'delete from `pub_Bookmark` where id = :bookmarkId';
+ $response = $dbh->prepare($request);
+ $response->execute([
+ 'bookmarkId' => $this->element
+ ]);
+
+ if($response === false) { // Server/database fails
+ $this->respondeError();
+ }
+
+ $this->responde(200,null);
+
+ }
+}