Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions db/migrations/20200322173700-create-clients.sql
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 14 additions & 1 deletion db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -28,4 +40,5 @@ INSERT INTO schema_migrations
(migration)
VALUES
('20200202110100-create-migrations.sql'),
('20200202110200-create-actions.sql');
('20200202110200-create-actions.sql'),
('20200322173700-create-clients.sql');
57 changes: 57 additions & 0 deletions public/api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

$headers = getallheaders();

header("Content-Type: application/json");

if (isset($headers["Authentication"])) {
list($type, $token) = explode(" ", $headers["Authentication"], 2);

$dbconn = pg_connect("host=localhost port=5432 dbname=phpapp");
$sql = 'SELECT name, data FROM clients WHERE token = $1';
$result = pg_query_params($dbconn, $sql, [$token]);
$data = pg_fetch_all($result);

if (empty($data)) {
http_response_code(401);
$reply = [
"error" => "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);