/** * Directs you to either the initial search page or to the search results page depending on get parameters * * @return \Illuminate\Http\Response */ public function index(Request $request) { $method = $request->get('method'); $sort = $request->get('sort'); $query = $request->get('q'); $useRestrictions = $request->get('restrictions'); if ($useRestrictions == null) { $useRestrictions = 1; } if ($query == null) { return view('food.index')->with(compact('method', 'query', 'useRestrictions', 'sort')); } else { $foods = null; $restrictions = []; if ($useRestrictions == 0) { Food::ObeyRestrictions(false); } else { if (Auth::check()) { $user = Auth::user(); $restrictions = $user->getRestrictions(); } } if ($method == 'similar') { $foods = Food::getNameSimilarTo($query, $restrictions); } else { if ($method == 'search') { $foods = Food::SearchByName($query, $restrictions); } else { if ($method == 'name') { $foods = Food::GetByName($query, $restrictions); } else { $foods = Food::SearchByName($query, $restrictions); } } } if ($sort == 'cal') { $foods = $foods->sortBy('calories'); } else { if ($sort == 'sugar') { $foods = $foods->sortBy(function ($food) { return $food->getSugar(); }); } else { if ($sort == 'fat') { $foods = $foods->sortBy(function ($food) { return $food->getFat(); }); } } } return view('food.searchresults')->with(compact('foods', 'method', 'query', 'useRestrictions', 'sort')); } }
public function testRestrictions() { $user = $this->spawnUser(); $r = App\Restriction::all()[0]; $user->addRestriction($r); $this->assertEquals($user->addRestriction($r), null); $this->assertEquals($r->getDisplayName(), "Nut Allergy"); //Spawns food and detaches + reattaches restriction $f = App\Food::GetNameSimilarTo("nuts", [])[0]; App\Food::ObeyRestrictions($r); $this->assertEquals(App\Food::ObeyRestrictions($r), null); if ($f->isRestricted($r)) { $f->removeRestriction($r); $this->assertEquals($f->removeRestriction($r), null); $this->assertEquals($f->isRestricted($r), false); } $f->addRestriction($r); $this->assertEquals($user->canEatFood($f), false); $this->assertEquals($f->isRestricted($r), true); $sample1 = $user->getFoodSuggestion(); $sample2 = $user->getFoodSuggestion(); $this->assertNotEquals($sample1, $sample2); $this->assertEquals($user->canEatFood($sample1), true); }