Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose BFF API #251

Merged
merged 14 commits into from
Nov 18, 2018
Merged
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
2 changes: 1 addition & 1 deletion app/Election.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function createTotallyOrderedBallot($nBallot) {
return new CandidateList($candidate);
}, $totalOrder);

return new NBallot(1, ...$totalOrder);
return new Ballot(...$totalOrder);
}

public static function ballotToText($nBallot) {
Expand Down
72 changes: 72 additions & 0 deletions app/Http/Controllers/BffElectionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controller as BaseController;
use Carbon\Carbon;
use App\Election;
use App\Elector;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use PivotLibre\Tideman\BffElectionRunner;

class BffElectionController extends BaseController
{
/**
* Run a Ranked Pairs election via BFF ballots.
*
* @SWG\Post(
* tags={"BFF Election Result"},
* path="/open/try",
* summary="Run an election using BFF ballots",
* operationId="calculateResult",
* consumes={"application/x-www-form-urlencoded"},
* produces={"text/plain"},
* @SWG\Parameter(
* name="ballots",
* in="formData",
* description="One or more BFF ballots, one on each line",
* required=true,
* type="string"
* ),
* @SWG\Parameter(
* name="tieBreaker",
* in="formData",
* description="One BFF Ballot for resolving intermediate ties",
* required=true,
* type="string"
* ),
* @SWG\Response(response="200", description="Success", @SWG\Schema(
* type="string",
* description="JSON with a key `result` whose value is a BFF election result"
* )),
* @SWG\Response(response="400", description="Bad Request")
* )
*
* @return \Illuminate\Http\Response
*/
public function calculateResult(Request $request)
{
if (!$request->has('tieBreaker')) {
abort(400, "tieBreaker is a required parameter");
} else {
$tieBreaker = $request->input('tieBreaker');
}
if (!$request->has('ballots')) {
abort(400, "ballots is a required parameter");
} else {
$ballots = $request->input('ballots');
}

$runner = new BffElectionRunner();
$runner->setTieBreaker($tieBreaker);
$result = $runner->run($ballots);
return $result;
}

public function form()
{
return view('bff_form');
}
}
10 changes: 5 additions & 5 deletions app/Http/Controllers/CandidateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct()
*
* @SWG\Get(
* tags={"Candidates"},
* path="/elections/{electionId}/candidates",
* path="/api/elections/{electionId}/candidates",
* summary="View candidates for an election",
* operationId="candidateIndex",
* @SWG\Parameter(
Expand Down Expand Up @@ -51,7 +51,7 @@ public function index(Election $election)
*
* @SWG\Get(
* tags={"Candidates"},
* path="/elections/{electionId}/candidates/{candidateId}",
* path="/api/elections/{electionId}/candidates/{candidateId}",
* summary="Get information about a candidate",
* operationId="getCandidateById",
* @SWG\Parameter(
Expand Down Expand Up @@ -89,7 +89,7 @@ public function show(Election $election, Candidate $candidate)
*
* @SWG\Post(
* tags={"Candidates"},
* path="/elections/{electionId}/candidates",
* path="/api/elections/{electionId}/candidates",
* summary="Add a candidate",
* consumes={"application/json"},
* produces={"application/json"},
Expand Down Expand Up @@ -149,7 +149,7 @@ public function store(Request $request, Election $election)
*
* @SWG\Patch(
* tags={"Candidates"},
* path="/elections/{electionId}/candidates/{candidateId}",
* path="/api/elections/{electionId}/candidates/{candidateId}",
* summary="Update a candidate",
* consumes={"application/json"},
* produces={"application/json"},
Expand Down Expand Up @@ -206,7 +206,7 @@ public function update(Request $request, Election $election, Candidate $candidat
*
* @SWG\Delete(
* tags={"Candidates"},
* path="/elections/{electionId}/candidates/{candidateId}",
* path="/api/elections/{electionId}/candidates/{candidateId}",
* summary="Delete a candidate",
* consumes={"application/json"},
* produces={"application/json"},
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @SWG\Swagger(
* basePath="/api",
* basePath="/",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this because I wanted the /open-prefixed services to be featured in the Swagger documentation, not just the /api-prefixed services.

* @SWG\Info(
* title="Pivot API",
* version="0.1.0"
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/ElectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ElectionController extends Controller
/**
* @SWG\Get(
* tags={"Election"},
* path="/elections",
* path="/api/elections",
* operationId="electionIndex",
* summary="View all elections",
* @SWG\Response(response="200", description="Success", @SWG\Schema(
Expand Down Expand Up @@ -87,7 +87,7 @@ public function store(Request $request)
*
* * @SWG\Get(
* tags={"Election"},
* path="/elections/{electionId}",
* path="/api/elections/{electionId}",
* summary="View information about an election",
* operationId="getElectionById",
* @SWG\Parameter(
Expand Down Expand Up @@ -338,4 +338,4 @@ public function batch_candidates_view(Request $request, $election_id)
$this->authorize('view', $election);
return $election->candidates;
}
}
}
4 changes: 2 additions & 2 deletions app/Http/Controllers/ElectorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ElectorController extends Controller
*
* @SWG\Get(
* tags={"Electors"},
* path="/elections/{electionId}/electors",
* path="/api/elections/{electionId}/electors",
* summary="View the electorate for an election",
* operationId="electorIndex",
* @SWG\Parameter(
Expand Down Expand Up @@ -69,7 +69,7 @@ public function electors_for_self(Election $election)
*
* @SWG\Get(
* tags={"Electors"},
* path="/elections/{electionId}/electors/{electorId}",
* path="/api/elections/{electionId}/electors/{electorId}",
* summary="Get information about an elector",
* operationId="getElectorById",
* @SWG\Parameter(
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/InviteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class InviteController extends Controller
*
* @SWG\Get(
* tags={"Invites"},
* path="/elections/{electionId}/invite",
* path="/api/elections/{electionId}/invite",
* summary="View electors who have not accepted their invite yet",
* operationId="inviteIndex",
* @SWG\Parameter(
Expand Down Expand Up @@ -46,7 +46,7 @@ public function index(Election $election)
*
* @SWG\Post(
* tags={"Invites"},
* path="/elections/{electionId}/invite",
* path="/api/elections/{electionId}/invite",
* summary="Send an invite",
* operationId="createInvite",
* consumes={"application/json"},
Expand Down Expand Up @@ -94,7 +94,7 @@ public function store(Request $request, Election $election)
*
* @SWG\Post(
* tags={"Invites"},
* path="/invite/accept",
* path="/api/invite/accept",
* summary="Accept an invite",
* consumes={"application/json"},
* produces={"application/json"},
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/ResultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ResultController extends Controller
*
* @SWG\Get(
* tags={"Election"},
* path="/election/{electionId}/result",
* path="/api/election/{electionId}/result",
* summary="View results for an election",
* operationId="getElectionResults",
* @SWG\Parameter(
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"laravel/framework": "5.4.*",
"laravel/passport": "^3.0",
"laravel/tinker": "~1.0",
"pivot-libre/tideman": "0.2.0",
"pivot-libre/tideman": "0.4.0",
"aws/aws-sdk-php": "~3.0",
"guzzlehttp/guzzle": "~6.0"
},
Expand Down Expand Up @@ -60,6 +60,7 @@
"config": {
"preferred-install": "dist",
"sort-packages": true,
"optimize-autoloader": true
"optimize-autoloader": true,
"discard-changes": true
}
}
Loading