public function postStartGardening(StartGardeningRequest $request) { $user = Auth::user(); // if canStartGardening + 2 days < now => redirect if ($user->canStartGardening->addDays(2) < Carbon::now()) { session()->flash('errorMessages', ['Sorry but your time to start gardening has expired.']); return redirect()->route('dashboard'); } // update the user with the shipping address and paymnent info and set canStartGardening to null $request->merge(array('canStartGardening' => null)); $user->update($request->only('street', 'streetnumber', 'city', 'postalcode', 'country', 'payment_method', 'card_number', 'cvc', 'canStartGardening')); // create the new plant and attach it to the plantPlot $plant = new Plant(); $plant->plantCharacter_id = $request->get('plant_character'); $plant->plantSpecies_id = $request->get('plant_species'); $plant->isPremium = $request->get('tier') == 'premium' ? true : false; $plant->save(); // find an empty plantPlot and fill it with the new plant $plantPlot = PlantPlot::getEmptyPlot(); $plantPlot->isPlanted = true; $plantPlot->plant_id = $plantPlot->id; // attach the plant to the plantPlot $plantPlot->save(); // attach the plant to the user $user->plants()->attach($plant, ['isOwner' => true, 'hasAccepted' => true]); $plantSpecies = PlantSpecies::findOrFail($request->get('plant_species')); // flash success message $request->session()->flash('successMessages', ['Congratulations! It\'s a ' . $plantSpecies->name . '.']); return redirect()->route('dashboard'); }
/** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function store(Request $request) { if ($request->input('plant_id') == null) { return response()->json("invalid", "500"); } $user = User::where('username', '=', $request->input('username'))->first(); $task = new TaskList(); $task->description = $request->input('description'); $task->date = date("Y-m-d", strtotime($request->input('date'))); $task->time = $request->input('time'); $task->status = "Remaining"; $task->user_id = $user->id; $task->plant_id = $request->input('plant_id'); $typeActivity[] = $request->input('type'); $task->save(); $worker = $request->input('worker'); foreach ($worker as $uid) { if ($uid != "0") { $notificationControl = new notificationController(); $task->workerMember()->attach($uid); $detailPlantPlot = Plant::where("id", "=", $task->plant_id)->with("plot")->first(); $message = "" . $user->name . " " . $user->surname . "has assigned you a task!"; $notificationControl->sentTaskToFarmWorker($message, $uid); } } foreach ($typeActivity as $id) { if ($id != "0") { $task->activityType()->attach($id); } } return $task; // }
public function getFertilizeHistory(Request $request) { $plant = Plant::findOrFail($request->get('plant_id')); // set moisture / watering history according to selected week $lastPlantMoisture = $plant->moisture()->orderBy('date', 'DESC')->first(); if ($request->has('week')) { $selectedWeekWatering = $request->get('week'); } else { if (isset($lastPlantMoisture)) { $selectedWeekWatering = $lastPlantMoisture->date->weekOfYear; } else { $selectedWeekWatering = null; } } $allPlantMoisture = $plant->moisture; $plantMoisture = []; foreach ($allPlantMoisture as $moisture) { if ($moisture->date->weekOfYear == $selectedWeekWatering) { array_push($plantMoisture, $moisture->value); } } $firstweek = $plant->moisture()->first() ? $plant->moisture()->first()->date->weekOfYear : null; $plantMoistureLength = count($plantMoisture); if ($firstweek && $selectedWeekWatering == $firstweek && $plantMoistureLength < 7) { for ($i = 0; $i < 7 - $plantMoistureLength; $i++) { array_unshift($plantMoisture, $plantMoisture[0]); } // double last value array_push($plantMoisture, $plantMoisture[count($plantMoisture) - 1]); } return $plantMoisture; }
/** * Remove the specified resource from storage. * * @param int $id * @return Response */ public function destroy($id) { if ($id == null) { return null; } Plant::find($id)->delete(); return $id; }
public function testDestroyPlant() { $id = \App\Plant::where("name", "like", "Test plant")->first()->id; $plantController = new \App\Http\Controllers\plantController(); $plantController->destroy($id); $this->assertNull($plantController->show($id)); $this->assertNull($plantController->destroy(null)); }
public function testDestroy() { $Activity3 = array("description" => "Test store activity updated", "date" => "2015-10-5", "time" => "23:00:00", "plant_id" => \App\Plant::where("name", "=", "TestActivityPlant")->first()->id, "weather" => "fews cloud"); $ActivityController = new \App\Http\Controllers\activityController(); $ActivityController->destroy(activity::where("description", "=", "Test store activity updated")->first()->id); $this->notSeeInDatabase("activity", $Activity3); $this->assertNull($ActivityController->destroy(null)); }
public function testDestroy() { $taskID = \App\TaskList::where("plant_id", "=", \App\Plant::where("name", "=", "TestActivityPlant")->first()->id)->where("time", "=", "16:00:00")->first()->id; $Task3 = array("id" => $taskID, "description" => "TestTask", "date" => "2015-12-06", "time" => "16:00:00", "plant_id" => \App\Plant::where("name", "=", "TestActivityPlant")->first()->id, "user_id" => 66); $this->action('DELETE', 'TaskListController@destroy', array("id" => $taskID)); $this->notSeeInDatabase("tasklist", $Task3); $this->action('DElETE', 'TaskListController@destroy', null); $this->assertResponseStatus(405); }
/** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { $plant = Plant::findOrFail(Route::input('id')); // authorize when the user is the owner of the plant return Auth::user()->id == $plant->users()->wherePivot('isOwner', true)->first()->id; }