/** * @param Entry $entry * @return array */ public function transform(Entry $entry) { $array = ['id' => $entry->id, 'date' => $entry->date, 'quantity' => $entry->quantity, 'calories' => $entry->getCalories(), 'food' => ['id' => $entry->food->id, 'name' => $entry->food->name], 'unit' => ['id' => $entry->unit->id, 'name' => $entry->unit->name]]; if ($entry->recipe) { $array['recipe'] = ['id' => $entry->recipe->id, 'name' => $entry->recipe->name]; } return $array; }
/** * * @param $date * @return int|mixed */ public function getCaloriesForDay($date) { $calories_for_day = 0; //Get the user's food entries for the date $entries = Entry::forCurrentUser()->where('date', $date)->get(); //Get the calories for the entries foreach ($entries as $entry) { $calories_for_day += $entry->getCalories(); } return $calories_for_day; }
/** * @test * @return void */ public function it_can_delete_a_menu_entry() { DB::beginTransaction(); $this->logInUser(); $entry = Entry::first(); $response = $this->call('DELETE', '/api/menuEntries/' . $entry->id); $this->assertEquals(204, $response->getStatusCode()); $response = $this->call('DELETE', '/api/menuEntries/' . $entry->id); $this->assertEquals(404, $response->getStatusCode()); DB::rollBack(); }
<?php use App\Models\Menu\Entry; use App\Models\Menu\Food; Route::get('/test', function () { $entry = Entry::first(); //dd($entry); return $entry->getCalories(); });
/** * * @param Entry $entry * @param $index */ private function attachUnit(Entry $entry, $index) { $unit_ids = collect($entry->food->units)->lists('id')->all(); $entry->unit()->associate(Unit::find($unit_ids[$index])); }
/** * Get a user's menu (food/recipe) entries for one day * @param $date * @return array */ public function getEntriesForTheDay($date) { $entries = Entry::forCurrentUser()->where('date', $date)->get(); return transform(createCollection($entries, new MenuEntryTransformer()))['data']; }
/** * * @param Entry $entry * @return \Illuminate\Http\Response * @throws \Exception */ public function destroy(Entry $entry) { $entry->delete(); return $this->responseNoContent(); }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * @return void */ public function boot(Router $router) { parent::boot($router); Route::bind('exercises', function ($id) { return Exercise::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseEntries', function ($id) { return ExerciseEntry::forCurrentUser()->findOrFail($id); }); Route::bind('menuEntries', function ($id) { return MenuEntry::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseSeries', function ($id) { return ExerciseSeries::forCurrentUser()->findOrFail($id); }); Route::bind('foods', function ($id) { return Food::forCurrentUser()->findOrFail($id); }); Route::bind('recipes', function ($id) { return Recipe::forCurrentUser()->findOrFail($id); }); Route::bind('exerciseTags', function ($id) { return Tag::forCurrentUser()->where('for', 'exercise')->findOrFail($id); }); Route::bind('recipeTags', function ($id) { return Tag::forCurrentUser()->where('for', 'recipe')->findOrFail($id); }); Route::bind('foodUnits', function ($id) { return Unit::forCurrentUser()->where('for', 'food')->findOrFail($id); }); Route::bind('exerciseUnits', function ($id) { return Unit::forCurrentUser()->where('for', 'exercise')->findOrFail($id); }); Route::bind('timers', function ($id) { return Timer::forCurrentUser()->findOrFail($id); }); Route::bind('activities', function ($id) { return Activity::forCurrentUser()->findOrFail($id); }); Route::bind('weights', function ($idOrDate) { if (strrpos($idOrDate, '-')) { //parameter is the date of the entry $weight = Weight::forCurrentUser()->where('date', $idOrDate)->first(); } else { //parameter is the id of the entry $weight = Weight::forCurrentUser()->findOrFail($idOrDate); } return $weight; }); /** * $parameter is either the id or the date */ Route::bind('journal', function ($parameter) { /** * @VP: * Is there a better way to check if the $parameter is an * id or a date? When I tried using Carbon to create an object from * the parameter, it threw an exception when the $parameter was the id, * whereas I just wanted a boolean. */ if (strrpos($parameter, '-')) { //$parameter is the date of the entry $journal = Journal::forCurrentUser()->where('date', $parameter)->first(); } else { //$parameter is the id of the entry $journal = Journal::forCurrentUser()->findOrFail($parameter); } return $journal; }); }