Commit 8b4f58a6 authored by Jack Stupple's avatar Jack Stupple

remove endpoints we dont care about, documentation and modify get_player to just get the session id

parent 7268da43
......@@ -27,7 +27,7 @@ class Character extends Model
public function questions()
{
$player = get_player($_SERVER['HTTP_SESSION_ID'], false);
$player = get_player(false);
if ($player->language_id) {
$language = \App\Language::where('id', $player->language_id)->firstOrFail();
......@@ -46,7 +46,7 @@ class Character extends Model
public function status()
{
$player = get_player($_SERVER['HTTP_SESSION_ID'], false);
$player = get_player(false);
return $this->hasOne(\App\CharacterSocial::class)
->where('language_id', $player ? $player->language_id : \App\Language::where('slug', 'en')->first()->id)
......@@ -55,7 +55,7 @@ class Character extends Model
public function profile_picture()
{
$player = get_player($_SERVER['HTTP_SESSION_ID'], false);
$player = get_player(false);
return $this->hasOne(\App\CharacterPicture::class)
->where('season_roundup_position', '=', $player ? $player->season_roundup_position : 0);
......
......@@ -18,43 +18,4 @@ class AnswerController extends Controller
// ensure that any secret fields are appropriately populated in the model
return Answer::where('id', $answer_id)->with('modifiers')->firstOrFail();
}
public function destroy(Request $request, $answer_id)
{
// if the user was not deleted then do not return a success header
$deleted = Answer::where('id', $answer_id)->firstOrFail()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
public function make(Request $request)
{
$this->validate($request, [
'language_id' => 'required|numeric|exists:languages,id',
'title' => 'required|min:10|max:255'
]);
$answer = new Answer();
$answer->language_id = $request->input('language_id');
$answer->title = $request->input('title');
$answer->save();
return $answer;
}
public function update(Request $request, $answer_id)
{
$this->validate($request, [
'language_id' => 'numeric|exists:languages,id',
'title' => 'min:10|max:255'
]);
$answer = Answer::where('id', $answer_id)->firstOrFail();
$answer->language_id = $request->input('language_id', $answer->language_id);
$answer->title = $request->input('title', $answer->title);
$answer->save();
return $answer;
}
}
\ No newline at end of file
......@@ -12,44 +12,4 @@ class AnswerModifierController extends Controller
{
return Modifier::where('model', '=', 'answer')->where('model_id', '=', $answer_id)->get();
}
public function make(Request $request, $answer_id)
{
$this->validate($request, [
'amount' => 'required|numeric',
'modifier_category_id' => 'required|exists:modifier_categories,id'
]);
$modifier = new Modifier;
$modifier->model = 'answer';
$modifier->model_id = $answer_id;
$modifier->modifier_category_id = $request->input('modifier_category_id');
$modifier->amount = $request->input('amount');
$modifier->save();
return $modifier;
}
public function update(Request $request, $modifier_id)
{
$this->validate($request, [
'amount' => 'numeric',
'modifier_category_id' => 'min:1|max:100'
]);
$modifier = Modifier::where('id', $modifier_id)->firstOrFail();
$modifier->amount = $request->input('amount', $modifier->amount);
$modifier->modifier_category_id = $request->input('modifier_category_id', $modifier->modifier_category_id);
$modifier->save();
return $modifier;
}
public function destroy(Request $request, $modifier_id)
{
$deleted = Modifier::where('id', $modifier_id)->firstOrFail()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
}
\ No newline at end of file
......@@ -17,7 +17,6 @@ class CharacterController extends Controller
public function show(Request $request, $character_id)
{
$player = get_player($request);
return Character::where('id', $character_id)->with([
'baseModifiers',
'baseModifiers.modifierCategory',
......@@ -28,45 +27,4 @@ class CharacterController extends Controller
'profile_picture'
])->firstOrFail();
}
public function destroy($character_id)
{
$deleted = Character::where('id', $character_id)->firstOrFail()->delete();
return response(json_encode(['deleted' => $deleted]), 204);
}
public function make(Request $request)
{
$this->validate($request, [
'enabled' => 'date_format:Y-m-d H:i:s|nullable',
'profile_picture' => 'string',
'name' => 'required|string|min:1|max:100'
]);
$character = new Character;
$character->name = $request->input('name');
$character->enabled = $request->input('enabled', false);
$character->profile_picture = $request->input('profile_picture', null);
$character->save();
return $character;
}
public function update(Request $request, $character_id)
{
$this->validate($request, [
'enabled' => 'date_format:Y-m-d H:i:s|nullable',
'profile_picture' => 'string',
'name' => 'string|min:1|max:100'
]);
$character = Character::where('id', $character_id)->firstOrFail();
$character->enabled = $request->input('enabled', $character->enabled);
$character->profile_picture = $request->input('profile_picture', $character->profile_picture);
$character->name = $request->input('name', $character->name);
$character->save();
return $character;
}
}
......@@ -9,7 +9,7 @@ class CharacterQuestionController extends Controller
{
public function index(Request $request)
{
$player = get_player($request);
$player = get_player();
return \App\Character::where('id', $player->character_id)
->with([
......
......@@ -13,41 +13,4 @@ class ModifierCategoryController extends Controller
{
return ModifierCategory::all();
}
public function make(Request $request)
{
$this->validate($request, [
'name' => 'required|string|min:1|max:255',
'income_outgoing_savings' => 'required'
]);
$modifier_category = new ModifierCategory;
$modifier_category->name = $request->input('name');
$modifier_category->income_outgoing_savings = $request->input('income_outgoing_savings');
$modifier_category->save();
return $modifier_category;
}
public function update(Request $request, $modifier_category_id)
{
$this->validate($request, [
'name' => 'string|min:1|max:255'
]);
$modifier_category = ModifierCategory::where('id', $modifier_category_id)->firstOrFail();
$modifier_category->name = $request->input('name', $modifier_category->name);
$modifier_category->income_outgoing_savings = $request->input('income_outgoing_savings', $modifier_category->income_outgoing_savings);
$modifier_category->save();
return $modifier_category;
}
public function destroy(Request $request, $modifier_category_id)
{
$deleted = ModifierCategory::where('id', $modifier_category_id)->firstOrFail()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
}
\ No newline at end of file
......@@ -5,8 +5,8 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
class PaperworkController extends Controller {
public function index(Request $request) {
$player = get_player($request);
public function index() {
$player = get_player();
if (!$player || !$player->character) {
return [];
}
......
......@@ -8,16 +8,16 @@ use Illuminate\Http\Request;
class PlayerController extends Controller
{
public function show(Request $request)
public function show()
{
// ensure that any secret fields are appropriately populated in the model
return get_player($request);
return get_player();
}
public function destroy(Request $request)
public function destroy()
{
// if the user was not deleted then do not return a success header
$deleted = get_player($request)->delete();
$deleted = get_player()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
......@@ -51,7 +51,7 @@ class PlayerController extends Controller
'calendar_intro_completed' => 'nullable'
]);
$player = get_player($request);
$player = get_player();
$budget_intro_completed = $request->input('budget_intro_completed', 'empty');
if ($budget_intro_completed === 'empty') {
......@@ -79,6 +79,6 @@ class PlayerController extends Controller
$player->increment('playthrough');
}
return get_player($request); // we refresh the component to get all the lazy elements too
return get_player(); // we refresh the component to get all the lazy elements too
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ use Illuminate\Http\Request;
class PlayerHistoryController extends Controller
{
public function index(Request $request) {
$player = get_player($request);
$player = get_player();
return \App\PlayerHistory::where('player_id', $player->id)->where('playthrough', $player->playthrough)->with(['answer', 'season_roundup', 'question', 'answer.modifiers', 'season_roundup.modifiers'])->get();
}
......@@ -25,7 +25,7 @@ class PlayerHistoryController extends Controller
'season_roundup_id' => 'nullable|numeric|exists:season_roundups,id'
]);
$player = get_player($request);
$player = get_player();
$player_history = new \App\PlayerHistory;
$player_history->answer_id = $request->input('answer_id');
......@@ -40,7 +40,7 @@ class PlayerHistoryController extends Controller
public function destroy(Request $request)
{
$player = get_player($request);
$player = get_player();
$player->increment('playthrough');
$player->budget_intro_completed = null;
$player->calendar_intro_completed = null;
......
......@@ -18,43 +18,4 @@ class QuestionController extends Controller
// ensure that any secret fields are appropriately populated in the model
return Question::where('id', $question_id)->with(['options', 'options.modifiers'])->firstOrFail();
}
public function destroy(Request $request, $question_id)
{
// if the user was not deleted then do not return a success header
$deleted = Question::where('id', $question_id)->firstOrFail()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
public function make(Request $request)
{
$this->validate($request, [
'language_id' => 'required|numeric|exists:languages,id',
'title' => 'required|min:10|max:255'
]);
$question = new Question();
$question->language_id = $request->input('language_id');
$question->title = $request->input('title');
$question->save();
return $question;
}
public function update(Request $request, $question_id)
{
$this->validate($request, [
'language_id' => 'numeric|exists:languages,id',
'title' => 'min:10|max:255'
]);
$question = Question::where('id', $question_id)->firstOrFail();
$question->language_id = $request->input('language_id', $question->language_id);
$question->title = $request->input('title', $question->title);
$question->save();
return $question;
}
}
\ No newline at end of file
......@@ -6,8 +6,8 @@ use App\Resource;
class ResourceController extends Controller
{
public function index(Request $request) {
$player = get_player($request);
public function index() {
$player = get_player();
$subquery = \DB::table('player_history')
->select('resource_resource_category.resource_id')
......@@ -70,7 +70,7 @@ class ResourceController extends Controller
public function getRoundup(Request $request)
{
$player = get_player($request);
$player = get_player();
$last_3_items = $player->history()->where('answer_id', '!=', null)->orderBy('ID', 'DESC')->limit(3)->with(['answer.resourceCategories'])->get();
$resource_categories = [];
......
......@@ -9,7 +9,7 @@ use Illuminate\Http\Request;
class SeasonRoundupController extends Controller
{
public function index(Request $request) {
$player = get_player($request);
$player = get_player();
return \App\SeasonRoundup::with('modifiers')
->where('character_id', $player->character_id)
......@@ -19,7 +19,7 @@ class SeasonRoundupController extends Controller
}
public function next(Request $request) {
$player = get_player($request);
$player = get_player();
$roundup_position = floor($player->progress / 3);
$history = PlayerHistory::select('season_roundup_id')
......@@ -84,25 +84,4 @@ class SeasonRoundupController extends Controller
return $season_roundup;
}
public function update(Request $request, $season_roundup_id) {
$this->validate($request, [
'roundup_text' => 'string|min:1|max:255',
'modifiers' => 'array'
]);
$season_roundup = SeasonRoundup::where('id', '=', $season_roundup_id)->firstOrFail();
$season_roundup->roundup_text = $request->input('roundup_text', $season_roundup->roundup_text);
$season_roundup->save();
return $season_roundup;
}
public function destroy(Request $request, $season_roundup_id) {
$deleted = SeasonRoundup::where('id', '=', $season_roundup_id)->firstOrFail()->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
}
......@@ -12,51 +12,4 @@ class SeasonRoundupModifierController extends Controller
{
return Modifier::where('model', '=', 'season_roundup')->where('model_id', '=', $season_roundup_id)->get();
}
public function make(Request $request, $season_roundup_id)
{
$this->validate($request, [
'amount' => 'required|numeric',
'modifier_category_id' => 'required|exists:modifier_categories,id'
]);
$modifier = new Modifier;
$modifier->model = 'season_roundup';
$modifier->model_id = $season_roundup_id;
$modifier->modifier_category_id = $request->input('modifier_category_id');
$modifier->amount = $request->input('amount');
$modifier->save();
return $modifier;
}
public function update(Request $request, $season_roundup_id, $modifier_id)
{
$this->validate($request, [
'amount' => 'numeric',
'modifier_category_id' => 'exists:modifier_categories,id'
]);
$modifier = Modifier::where('id', $modifier_id)
->where('model_id', '=', $season_roundup_id)
->where('model', '=', 'season_roundup')
->firstOrFail();
$modifier->amount = $request->input('amount', $modifier->amount);
$modifier->modifier_category_id = $request->input('modifier_category_id', $modifier->modifier_category_id);
$modifier->save();
return $modifier;
}
public function destroy(Request $request, $season_roundup_id, $modifier_id)
{
$deleted = Modifier::where('id', $modifier_id)
->where('model_id', '=', $season_roundup_id)
->where('model', '=', 'season_roundup')
->firstOrFail()
->delete();
// 204 means completed but no content
return response(json_encode(['deleted' => $deleted]), 204);
}
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ class SummaryController extends Controller
{
public function index(Request $request)
{
$player = get_player($request);
$player = get_player();
$summary = \App\Summary::where('character_id', $player->character_id)
->with(['answers'])->firstOrFail();
......
......@@ -23,7 +23,7 @@ class Authenticate
*/
public function handle($request, Closure $next)
{
$player = get_player($request);
$player = get_player();
if (!$player) {
return response('Could not authenticate player', 401);
......
......@@ -5,15 +5,16 @@ define('APP_ROOT', dirname(__DIR__));
static $_PLAYER;
// @todo: write phpunit test for this method
function get_player($session_id, $joins = true)
function get_player($joins = true)
{
// seems dodgy but a simple singleton? to avoid the history being retrieved fresh everytime in 1 request
if (!empty($_PLAYER)) {
return $_PLAYER;
}
if (is_a($session_id, \Illuminate\Http\Request::class)) {
$session_id = $session_id->header('Session-Id');
$session_id = isset($_SERVER['HTTP_SESSION_ID']) ? $_SERVER['HTTP_SESSION_ID'] : null;
if (!$session_id) {
return false;
}
$player = \App\Player::where('session_id', $session_id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment