public function run() { Activity::truncate(); $this->faker = Faker::create(); $users = User::all(); $activities = [['name' => 'sleep', 'color' => '#5cb85c'], ['name' => 'work', 'color' => '#5bc0de']]; foreach ($users as $user) { $this->user = $user; foreach ($activities as $activity) { $temp = new Activity(['name' => $activity['name'], 'color' => $activity['color']]); $temp->user()->associate($user); $temp->save(); } } }
/** * Create a day's sleep */ private function createDaySleep($index, $finishMinutes) { $today = Carbon::today(); $this->date = $today->subDays($index); $entry = new Timer(['start' => $this->date->hour(16)->format('Y-m-d H:i:s'), 'finish' => $this->date->hour(17)->minute($finishMinutes)->format('Y-m-d H:i:s')]); $entry->user()->associate($this->user); $entry->activity()->associate(Activity::where('name', 'sleep')->where('user_id', $this->user->id)->first()); $entry->save(); }
/** * * @param Request $request * @param Timer $timer * @return Response */ public function update(Request $request, Timer $timer) { // Create an array with the new fields merged $data = array_compare($timer->toArray(), $request->only(['start', 'finish'])); $timer->update($data); if ($request->has('activity_id')) { $timer->activity()->associate(Activity::findOrFail($request->get('activity_id'))); $timer->save(); } // dd($timer); $finishDate = $this->calculateFinishDate($timer); $timer = $this->transform($this->createItem($timer, new TimerTransformer(['date' => $finishDate])))['data']; return response($timer, Response::HTTP_OK); }
/** * For when the user gives both start and finish times * @test * @return void */ public function it_can_insert_a_manual_timer_entry() { DB::beginTransaction(); $this->logInUser(); $timer = ['start' => '2015-12-01 21:00:00', 'finish' => '2015-12-01 22:10:05', 'activity_id' => Activity::where('name', 'work')->first()->id]; $response = $this->call('POST', '/api/timers', $timer); // dd($response); $content = json_decode($response->getContent(), true); // dd($content); $this->checkTimerKeysExist($content); $this->assertEquals('2015-12-01 21:00:00', $content['start']); $this->assertEquals('2015-12-01 22:10:05', $content['finish']); $this->assertEquals('01/12/15', $content['startDate']); $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); DB::rollBack(); }
/** * Todo: check values are correct? Check it doesn't error if there is no timer in progress? * @test * @return void */ public function it_checks_for_timer_in_progress() { DB::beginTransaction(); $this->logInUser(); $timer = ['start' => '2015-12-01 21:00:00', 'activity_id' => Activity::where('name', 'work')->first()->id]; $response = $this->call('POST', '/api/timers', $timer); $content = json_decode($response->getContent(), true); $this->assertEquals(Response::HTTP_CREATED, $response->getStatusCode()); $response = $this->call('GET', '/api/timers/checkForTimerInProgress'); $content = json_decode($response->getContent(), true); $this->assertArrayHasKey('id', $content); $this->assertArrayHasKey('start', $content); $this->assertArrayHasKey('startDate', $content); $this->assertArrayHasKey('activity', $content); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); DB::rollBack(); }
/** * * @param $date * @return array */ public function calculateTotalMinutesForWeek($date) { Carbon::setWeekStartsAt(Carbon::SUNDAY); Carbon::setWeekEndsAt(Carbon::SATURDAY); $startOfWeek = Carbon::createFromFormat('Y-m-d', $date)->startOfWeek(); $endOfWeek = Carbon::createFromFormat('Y-m-d', $date)->endOfWeek(); //For calculating total untracked time $totalMinutesForAllActivities = 0; $activities = Activity::forCurrentUser()->get(); foreach ($activities as $activity) { $activity->totalMinutesForWeek = $activity->calculateTotalMinutesForWeek($startOfWeek, $endOfWeek); $activity->totalMinutesForAllTime(); $activity->averageMinutesPerDayForWeek = $activity->calculateAverageMinutesPerDayForWeek($date); $totalMinutesForAllActivities += $activity->totalMinutesForWeek; } $activities[] = $this->getUntrackedTimeForWeek($totalMinutesForAllActivities, $date); return $activities; }
/** * * @param Activity $activity * @return Response */ public function destroy(Activity $activity) { try { $activity->delete(); return response([], Response::HTTP_NO_CONTENT); } catch (\Exception $e) { //Integrity constraint violation if ($e->getCode() === '23000') { $message = 'Activity could not be deleted. It is in use.'; } else { $message = 'There was an error'; } return response(['error' => $message, 'status' => Response::HTTP_BAD_REQUEST], Response::HTTP_BAD_REQUEST); } }
/** * @param Activity $activity * @return array */ public function transform(Activity $activity) { $array = ['id' => $activity->id, 'name' => $activity->name, 'totalMinutes' => $activity->totalMinutesForAllTime(), 'color' => $activity->color]; return $array; }
/** * 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; }); }
/** * @test * @return void */ public function it_can_delete_an_activity() { DB::beginTransaction(); $this->logInUser(); $activity = Activity::first(); $response = $this->call('DELETE', '/api/activities/' . $activity->id); $this->assertEquals(204, $response->getStatusCode()); $response = $this->call('DELETE', '/api/activity/' . $activity->id); $this->assertEquals(404, $response->getStatusCode()); DB::rollBack(); }