diff --git a/README.md b/README.md index bd2da52..f8ccef8 100644 --- a/README.md +++ b/README.md @@ -83,15 +83,16 @@ Let's run a few queries against our database. phpapp=# \dt+ ``` -We should see two tables, like +We should see three tables, like ``` List of relations Schema | Name | Type | Owner | Size | Description --------+-------------------+-------+----------+------------+------------- public | actions | table | aforward | 8192 bytes | + public | clients | table | aforward | 8192 bytes | public | schema_migrations | table | aforward | 8192 bytes | -(2 rows) +(3 rows) ``` Let's look at the data within the `schema_migrations` table. @@ -107,9 +108,39 @@ The output should look similar to: --------------------------------------+---------------------------- 20200202110100-create-migrations.sql | 2020-02-02 11:39:55.014702 20200202110200-create-actions.sql | 2020-02-02 11:39:55.014702 -(2 rows) + 20200322173700-create-clients.sql | 2020-03-22 18:15:50.238449 +(3 rows) ``` +#### seeding database + +You can run the following to insert some sample data into your database. + +```sql +INSERT INTO clients + (name, data) +VALUES + ('Big Co.', '{"credits": 100}'::json), + ('Small Co.', '{"credits": 100}'::json); +``` + +You can verify the automatically generated tokens, for example: + +```sql +SELECT name, + token, + data +FROM clients; +``` + +Returning something similar to: + + name | token | data +-----------+----------------------------------+------------------ + Big Co. | d7d85f7eac7360d725b44d327445473e | {"credits": 100} + Small Co. | 9f8983a8494c8a003e064374ffb77cb6 | {"credits": 100} + + ## Running To start the PHP server, run the following from diff --git a/db/migrations/20200322173700-create-clients.sql b/db/migrations/20200322173700-create-clients.sql new file mode 100644 index 0000000..0969847 --- /dev/null +++ b/db/migrations/20200322173700-create-clients.sql @@ -0,0 +1,14 @@ +CREATE SEQUENCE clients_id_seq; +CREATE TABLE clients ( + id int DEFAULT nextval('clients_id_seq'), + name varchar(255), + token varchar(100) NOT NULL DEFAULT md5(random()::text), + data jsonb, + inserted_at timestamp DEFAULT NOW(), + updated_at timestamp DEFAULT NOW(), + PRIMARY KEY (id), + UNIQUE (token) +); + +INSERT INTO schema_migrations (migration, migrated_at) +VALUES ('20200322173700-create-clients.sql', NOW()::timestamp); diff --git a/db/schema.sql b/db/schema.sql index a8b774a..b6a74fa 100644 --- a/db/schema.sql +++ b/db/schema.sql @@ -17,6 +17,18 @@ CREATE TABLE actions ( PRIMARY KEY (id) ); +CREATE SEQUENCE clients_id_seq; +CREATE TABLE clients ( + id int DEFAULT nextval('clients_id_seq'), + name varchar(255), + token varchar(100) NOT NULL DEFAULT md5(random()::text), + data jsonb, + inserted_at timestamp DEFAULT NOW(), + updated_at timestamp DEFAULT NOW(), + PRIMARY KEY (id), + UNIQUE (token) +); + CREATE TABLE schema_migrations ( migration varchar(255), migrated_at timestamp DEFAULT NOW(), @@ -28,4 +40,5 @@ INSERT INTO schema_migrations (migration) VALUES ('20200202110100-create-migrations.sql'), - ('20200202110200-create-actions.sql'); \ No newline at end of file + ('20200202110200-create-actions.sql'), + ('20200322173700-create-clients.sql'); \ No newline at end of file diff --git a/public/api.php b/public/api.php new file mode 100644 index 0000000..95a49cf --- /dev/null +++ b/public/api.php @@ -0,0 +1,57 @@ + "Invalid token.", + "token" => $token, + "type" => $type, + ]; + echo json_encode($reply); + exit; + } +} else { + http_response_code(400); + $reply = [ + "error" => "Please provide a valid Authentication header for this API.", + "headers" => $headers, + ]; + echo json_encode($reply); + exit; +} + +if (!isset($headers["X-Men"])) { + http_response_code(400); + $reply = [ + "error" => "Please provide an X-Men mutant and reveal their human name.", + "headers" => $headers, + ]; + echo json_encode($reply); + exit; +} + +http_response_code(200); +switch($mutant = $headers["X-Men"]) { +case "Wolverine": + $name = "Logan"; + break; +case "Magento": + $name = "Eric"; + break; +default: + $name = "Unknown"; +} +$reply = ["mutant" => $mutant, "name" => $name]; +echo json_encode($reply); \ No newline at end of file