--- /dev/null
+<?php
+
+/**
+ * FundingAPI short summary.
+ *
+ * FundingAPI description.
+ *
+ * @version 1.0
+ * @author Ferrand
+ */
+class FundingAPI extends RestAPI {
+ use dontAllowPut, dontAllowPutCollection;
+ use dontAllowDelete, dontAllowDeleteCollection;
+ use dontAllowPost;
+ use dontAllowGetCollection;
+
+ protected function GETAction() {
+ $dbh = $this->databaseHelper();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Funding
+ 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
+ }
+
+
+ $serverFunding = new ServerFunding();
+ $serverFunding->id = $row['id'];
+ $serverFunding->transactionId = $row['transactionId'];
+ $serverFunding->value = $row['value'];
+ $serverFunding->username = $row['userId'];
+ $serverFunding->projectID = $row['projectId'];
+
+ $this->responde(200, $serverFunding);
+ }
+
+ 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!
+ }
+ */
+
+ // Attention, on peut financer plusieurs fois un projet !!
+
+ $dbh->beginTransaction();
+
+ $response = $dbh->prepare('SELECT * FROM pub_Project
+ WHERE id = :projectId
+ ');
+ $response->execute([':projectId' => $data->projectID]);
+
+ if($response === false) {
+ $this->respondeError();
+ $dbh->rollBack();
+ return; // end this
+ }
+
+ $row = $response->fetch(PDO::FETCH_ASSOC) ;
+ if($row === false) {
+ $this->respondeNotAcceptable();
+ $dbh->rollBack();
+ return; // end this
+ }
+
+ $curentFunding = $row['currentFunding'];
+
+ $response = $dbh->prepare('UPDATE pub_Project
+ SET currentFunding = :value, `lastModification` = NOW()
+ WHERE id = :id
+ ');
+
+ $response->execute([
+ ':id' => $data->projectID,
+ ':value' => ($curentFunding + $data->value)
+ ]);
+
+ if($response === false) {
+ $this->respondeError();
+ $dbh->rollBack();
+ return; // end this
+ }
+
+
+ $request = 'INSERT INTO `pub_Funding` (
+ `id` , `transactionId`, `creationDate`, `userId` , `projectId`, `value`)
+ VALUES ("" , "", NOW(), :userId, :projectId, :value);';
+
+ $response = $dbh->prepare($request);
+ $response->execute([
+ ':userId' => $data->username,
+ ':projectId' => $data->projectID,
+ ':value' => $data->value
+ ]);
+
+ if($response === false) { // Server/database fails
+ $this->respondeError();
+ $dbh->rollBack();
+ return; // end this
+ }
+
+ $rowAffected = new RowAffected();
+ $rowAffected->id = $dbh->lastInsertId();
+
+ $dbh->commit();
+
+
+ $this->responde(201, $rowAffected);
+ }
+
+
+}