about summary refs log tree commit diff
path: root/usth/ICT3.2/prac/5/3.patch
diff options
context:
space:
mode:
Diffstat (limited to 'usth/ICT3.2/prac/5/3.patch')
-rw-r--r--usth/ICT3.2/prac/5/3.patch59
1 files changed, 59 insertions, 0 deletions
diff --git a/usth/ICT3.2/prac/5/3.patch b/usth/ICT3.2/prac/5/3.patch
new file mode 100644
index 0000000..98081e2
--- /dev/null
+++ b/usth/ICT3.2/prac/5/3.patch
@@ -0,0 +1,59 @@
+From ede9ffb8df2b225fb2deaea958f9cc126f3352b6 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= <mcsinyx@disroot.org>
+Date: Sun, 11 Oct 2020 21:37:18 +0700
+Subject: [PATCH] Implement a simple RESTful API
+
+---
+ app/Config/Routes.php    |  2 +-
+ app/Controllers/Home.php | 21 ++++++++++++++++-----
+ 2 files changed, 17 insertions(+), 6 deletions(-)
+
+diff --git a/app/Config/Routes.php b/app/Config/Routes.php
+index 56839ca..312d31e 100644
+--- a/app/Config/Routes.php
++++ b/app/Config/Routes.php
+@@ -30,7 +30,7 @@ $routes->setAutoRoute(true);
+ 
+ // We get a performance increase by specifying the default
+ // route since we don't have to scan directories.
+-$routes->get('/', 'Home::index');
++$routes->get('/(:any)', 'Home::index/$1');
+ 
+ /**
+  * --------------------------------------------------------------------
+diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php
+index 8798cdd..3c80328 100644
+--- a/app/Controllers/Home.php
++++ b/app/Controllers/Home.php
+@@ -1,12 +1,23 @@
+ <?php namespace App\Controllers;
+ 
++function data($uri)
++{
++	return dirname(dirname(dirname(__DIR__))) . '/data/' . $uri . '.json';
++}
++
+ class Home extends BaseController
+ {
+-	public function index()
++	private $supported = ['<p>/user</p>', '<p>/user/{userId}/post</p>',
++	                      '<p>/post</p>', '<p>/post/{postId}/comment</p>'];
++
++	public function index($uri)
+ 	{
+-		return view('welcome_message');
++		if (!file_exists($file = data($uri))) {
++			echo "<p>Supported URIs:</p>\n";
++			return join("\n", $this->supported);
++		}
++		$this->response->setHeader('Access-Control-Allow-Origin', '*');
++		$this->response->setContentType('application/json');
++		readfile($file);
+ 	}
+-
+-	//--------------------------------------------------------------------
+-
+ }
+-- 
+2.28.0
+