-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
55 changed files
with
1,964 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\User; | ||
use App\Models\Dropbox; | ||
use App\Models\DropboxLog; | ||
use App\Models\Station; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Validation\Rule; | ||
|
||
class OperationController extends Controller | ||
{ | ||
use StationQueryTrait; | ||
|
||
public function index(Request $request) | ||
{ | ||
$this->authorize('viewAny', Station::class); | ||
|
||
$station = new Station; | ||
|
||
$station = $this->stationQuery($station, $request); | ||
|
||
$stations = $station->with('dropboxes.activeLog')->paginate(5); | ||
|
||
return view('operation.index', compact('stations')); | ||
} | ||
|
||
public function replace(Station $station, Request $request) | ||
{ | ||
// Temporary | ||
$this->authorize('update', $station); | ||
|
||
$request->validate([ | ||
'dropbox_id' => ['required', Rule::exists('dropboxes', 'id')->where('station_id', $station->id)], | ||
'empty_weight' => 'numeric|required', | ||
'filled_weight' => 'numeric|nullable', | ||
'timestamp' => 'date|required', | ||
]); | ||
|
||
$dropbox = $station->dropboxes()->find($request->dropbox_id); | ||
|
||
if ($request->filled('filled_weight') && $dropbox->active_log_id) { | ||
$this->createInspection($dropbox, $request->filled_weight, $request->timestamp, $request->user()); | ||
} | ||
|
||
// Create a replacement dropbox log | ||
// When a dropboxLog created, the related dropbox active_log_id will be automatically updated | ||
$log = $dropbox->dropboxLogs()->create([ | ||
'activity' => 'replacement', | ||
'weight' => $request->empty_weight, | ||
'starts_at' => $request->timestamp, | ||
'user_id' => $request->user()->id | ||
]); | ||
|
||
// Update station last_operation_at to now | ||
$dropbox->station->update(['last_operation_at' => $request->timestamp]); | ||
|
||
return $log; | ||
} | ||
|
||
public function show(Station $station) | ||
{ | ||
$this->authorize('view', $station); | ||
|
||
$dropboxes = $station->dropboxes()->with(['dropboxLogs' => function ($query) { | ||
$query->orderBy('starts_at')->whereNull('parent_id')->with(['user', 'children' => function ($children) | ||
{ | ||
$children->with('user')->orderBy('ends_at', 'asc'); | ||
}]); | ||
}])->get(); | ||
|
||
// Uncomment line below to see dropboxes data structure | ||
// return $dropboxes; | ||
|
||
return view('operation.show', compact('dropboxes', 'station')); | ||
} | ||
|
||
public function inspect(Station $station, Request $request) | ||
{ | ||
// Temporary | ||
$this->authorize('update', $station); | ||
|
||
$request->validate([ | ||
'dropbox_id' => ['required', Rule::exists('dropboxes', 'id')->where('station_id', $station->id)->whereNotNull('active_log_id')], | ||
'filled_weight' => 'numeric|required', | ||
'timestamp' => 'date|required', | ||
]); | ||
|
||
$dropbox = $station->dropboxes()->find($request->dropbox_id); | ||
|
||
$log = $this->createInspection($dropbox, $request->filled_weight, $request->timestamp, $request->user()); | ||
|
||
// Update station last_operation_at to now | ||
$dropbox->station->update(['last_operation_at' => $request->timestamp]); | ||
|
||
return $log; | ||
} | ||
|
||
public function destroy(DropboxLog $dropboxLog) | ||
{ | ||
$dropboxLog->load('dropbox.station'); | ||
|
||
$this->authorize('update', $dropboxLog->dropbox->station); | ||
|
||
$dropboxLog->delete(); | ||
|
||
return response()->noContent(); | ||
} | ||
|
||
/** | ||
* Create a inspection typed log | ||
* | ||
* @param Dropbox $dropbox | ||
* @param $weight | ||
* @param $timestamp | ||
* @return DropboxLog | ||
*/ | ||
protected function createInspection(Dropbox $dropbox, $weight, $timestamp, User $user) | ||
{ | ||
// Create a new dropbox log with parent_id = $dropbox->active_log_id, type = inspection | ||
$log = $dropbox->dropboxLogs()->create([ | ||
'activity' => 'inspection', | ||
'final_weight' => $weight, | ||
'ends_at' => $timestamp, | ||
'parent_id' => $dropbox->active_log_id, | ||
'user_id' => $user->id | ||
]); | ||
|
||
$latestLog = $log->parent->children()->orderBy('ends_at', 'desc')->first(); | ||
|
||
// Update parent final_weight, ends_at | ||
$log->parent->update([ | ||
'final_weight' => $latestLog->final_weight, | ||
'ends_at' => $latestLog->ends_at, | ||
]); | ||
|
||
return $log; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\User; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\Validation\Rule; | ||
|
||
class ProfileController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
/** @var User */ | ||
$user = $request->user(); | ||
|
||
// Appending $user->has_no_password attribute | ||
$user->append('has_no_password'); | ||
|
||
return view('auth.profile', compact('user'));; | ||
} | ||
|
||
public function update(Request $request) | ||
{ | ||
/** @var User */ | ||
$user = $request->user(); | ||
|
||
$data = $request->validate([ | ||
'name' => 'string|max:255|min:3', | ||
]); | ||
|
||
$user->update($data); | ||
|
||
return $user->fresh(); | ||
} | ||
|
||
public function updatePassword(Request $request) | ||
{ | ||
/** @var User */ | ||
$user = $request->user(); | ||
|
||
$request->validate([ | ||
'old_password' => [ | ||
Rule::requiredIf($user->has_no_password == false), | ||
function ($attribute, $value, $fail) use ($user) | ||
{ | ||
if (Hash::check($value, $user->password) == false) { | ||
$fail('The ' . $attribute . ' is not match.'); | ||
} | ||
} | ||
], | ||
'password' => 'required|string|min:8|confirmed' | ||
]); | ||
|
||
$user->update([ | ||
'password' => Hash::make($request->password) | ||
]); | ||
|
||
return response([ | ||
'success' => true | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\App; | ||
|
||
class StaticPageController extends Controller | ||
{ | ||
public function about($lang = 'id') | ||
{ | ||
App::setLocale($lang); | ||
|
||
return view('about', compact('lang')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.