diff --git a/API.md b/API.md new file mode 100644 index 0000000..a20505c --- /dev/null +++ b/API.md @@ -0,0 +1,67 @@ +# VerlainePoll's API + +## Methods + +### Create a poll +``` +POST /polls +``` + +| Field | Description | Optional | +| -------------- | ------------------------------------ | ---------- | +| `title` | The question. | no | +| `options` | All the options. Array of strings. | no | +| `settings` | A Settings object. | yes | + +Return a Poll. + +### Retrieve a poll + +``` +GET /polls/:id +``` + +Return a Poll. + +### Vote + +``` +POST /polls/:id/vote +``` +| Field | Description | Optional | +| -------------- | -------------------------------------------- | ---------- | +| `options` | Options you want to vote for. Array of ids. | no | + +Return a Poll. + +### Delete a poll + +``` +DELETE /polls/:id +``` + +Return a Poll. + +## Entities + +### Poll + +| Attribute | Description | Nullable | +| --------------- | ------------------------------------ | ---------- | +| `id` | | no | +| `title` | The question. | no | +| `options` | All the options. Array of Options. | no | +| `settings` | A Settings object. | no | +| `creation_date` | Creation date. | no | + +### Options + +| Attribute | Description | Nullable | +| --------------- | ------------------------------------ | ---------- | +| `label` | The option. | no | + +### Settings + +| Attribute | Description | Nullable | +| --------------- | ------------------------------------ | ---------- | +| `unique_ip` | One vote per IP address. Boolean. | yes | diff --git a/README.md b/README.md index f32fa59..300c44c 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Clone the repository : mkdir db && touch db/polls.db && composer install ``` -Uncomment the dba extention in `php.ini` : +Uncomment the `dba` extention in `php.ini` : ``` extension=dba ``` @@ -25,3 +25,5 @@ location / ``` ## API + +See [API.md](API.md). \ No newline at end of file diff --git a/index.php b/index.php index 6784763..5716fc3 100644 --- a/index.php +++ b/index.php @@ -3,14 +3,19 @@ require __DIR__ . "/vendor/autoload.php"; require __DIR__ . "/models/Poll.php"; require __DIR__ . "/config/app.php"; -function format_poll($poll) +function format_poll($poll, $with_delete_token = false) { - return [ + $array = [ "id" => $poll->id, "title" => $poll->title, "creation_date" => $poll->creation_date, "options" => $poll->options, ]; + + if ($with_delete_token === true) + $array['delete_token'] = $poll->delete_token; + + return $array; } Flight::route("POST /polls", function () { @@ -20,7 +25,7 @@ Flight::route("POST /polls", function () { $request_json = $request->data; $poll = Poll::create_poll($request_json); if ($poll) - Flight::json(format_poll($poll), 201); + Flight::json(format_poll($poll, true), 201); else Flight::halt(403, "
Your poll :poll_title is ready!
+ See the poll!