Procházet zdrojové kódy

Saving Loading Apps from/to DB

Ecconia před 7 roky
rodič
revize
01196fb94f
3 změnil soubory, kde provedl 72 přidání a 0 odebrání
  1. 3 0
      src/DB/Database.php
  2. 28 0
      src/DummyData.php
  3. 41 0
      src/Handler/MyHandler.php

+ 3 - 0
src/DB/Database.php

@@ -93,12 +93,15 @@
 		protected function error($msg, $sql, $code) {
 			//Functions::log(Functions::LOG_ERROR, $msg . '<br /><br /><code class="important">' . $sql . '</code>');
 			//TODO: Throw exception!
+			/*
 			die('
 				<h1>Database Error ('.$code.')</h1>
 
 				'.(!is_null($sql) ? '<code>' . $sql . '</code>' : '').'
 				<p>'.$msg.'</p>
 			');
+			*/
+			throw new Exception('FUUUU');
 		}
 	}
 ?>

+ 28 - 0
src/DummyData.php

@@ -4,12 +4,27 @@ namespace RS;
 
 use RS\Application\AppPattern;
 use RS\Application\Application;
+use RS\DB\DatabasePool;
 use RS\User;
 
 class DummyData {
 	public static function dummyBase() {
 		$devPattern = new AppPattern();
 
+		$db = DatabasePool::getDB();
+		$res = $db->query('SELECT *, UNIX_TIMESTAMP(date) AS date FROM dev_applications');
+
+		while($app = $db->fetchObject($res)) {
+			//echo 'Application:' . PHP_EOL;
+
+			//foreach($app as $key => $value) {
+			//	echo "->" . $key . ': ' . $value . PHP_EOL;
+			//}
+
+			$applications[] = new Application(new User($app->creator), $devPattern, $app->date, json_decode($app->answers), $app->state);
+		}
+
+		/*
 		$applications[] = new Application(new User('Joke'), $devPattern, 1439202084, [
 			'LalaLand', # Country
 			'69', # Age
@@ -34,6 +49,18 @@ class DummyData {
 			'', # Wanna mention something else?
 		], true);
 
+		$applications[] = new Application(new User('lamplord_dave'), $devPattern, 1528202384, [
+			'5', # Country
+			'red', # Age
+			'25 1/2 hours per day', # Addiction
+			'cuz i want to.', # Why joining this server?
+			'i can make a working lamp', # Your current rs knowledge?
+			'a working lamp', # Stuff you built in the past?
+			'a lamp', # What you gonna build?
+			'rechts!', # Links?
+			'i like lamps', # Wanna mention something else?
+		], false);
+
 		$applications[] = new Application(new User('Jack'), $devPattern, 1529202084, [
 			'', # Country
 			'', # Age
@@ -69,6 +96,7 @@ class DummyData {
 			'', # Links?
 			'', # Wanna mention something?
 		], null);
+		*/
 
 		return $applications;
 	}

+ 41 - 0
src/Handler/MyHandler.php

@@ -17,6 +17,8 @@ use Psr\Http\Message\{
 use RS\DummyData;
 use RS\Application\AppPattern;
 
+use RS\DB\DatabasePool;
+
 class MyHandler extends Handler {
 	public function soos(ServerRequestInterface $request, ResponseInterface $response, \stdClass $args): ResponseInterface {
 		$lel[] = 'A';
@@ -134,6 +136,44 @@ class MyHandler extends Handler {
 		return $response->withJson($obj);
 	}
 
+	public function applicationPost(ServerRequestInterface $request, ResponseInterface $response, \stdClass $args): ResponseInterface {
+
+		//TODO: Backend
+		// Validate struct, expected is:
+		// (token) + pattern Version + array of answers
+		// Validate the amount of answers ~ pattern amount
+		// Validate correct version of pattern
+		// Validate with regex
+
+		//TODO: Frontend
+		// Escape?
+		// Convert characters?
+
+		/*
+		$response->write('Input:{');
+
+		foreach ($request->getParsedBody() as $key => $value) {
+			$response->write('[' . $key . ':' . $value . ']');
+		}
+		return $response->write("}");
+		*/
+
+		$db = DatabasePool::getDB();
+
+		$parsed = $request->getParsedBody();
+
+		$username = $parsed['creator'];
+		$answers = $parsed['answers'];
+		
+		try {
+			$db->query('INSERT INTO `dev_applications` (`creator`, `date`, `answers`) VALUES (:creator, now(), :answers)', [$username, json_encode($answers)]);
+		} catch (Exception $e) {
+			//return $response->write($e);
+		}
+
+		return $response;//->write('Lol.');
+	}
+
 	public static function getRoutes(): array {
 		return [
 			new Route('GET', '/soos', 'soos'),
@@ -143,6 +183,7 @@ class MyHandler extends Handler {
 			new Route('GET', '/dummies', 'dummies'),
 			new Route('GET', '/application/{id:[0-9]+}', 'application'),
 			new Route('GET', '/app-pattern', 'appPattern'),
+			new Route('POST', '/application', 'applicationPost'),
 		];
 	}
 }