Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.2.0' into main
- Loading branch information
Showing
58 changed files
with
14,654 additions
and
401 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Field; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class FieldController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
$query = new Field(); | ||
|
||
if ($request->filled('user_id')) { | ||
$query = $query->where('user_id', $request->user_id); | ||
} | ||
|
||
$query = $query->with('address:id,addressable_id,addressable_type,province,city,district', 'user:name,id', 'user.organizations:id,name')->withLastPlanting(); | ||
|
||
$fields = $query->latest()->paginate(); | ||
|
||
if ($request->wantsJson()) { | ||
return $fields; | ||
} | ||
|
||
return Inertia::render('Field/Index', [ | ||
'initialFields' => $fields, | ||
'queries' => $request->except('page') | ||
]); | ||
} | ||
|
||
public function create(Request $request) | ||
{ | ||
$this->authorize('create', Field::class); | ||
|
||
return Inertia::render('Field/Create'); | ||
} | ||
|
||
public function store(Request $request) | ||
{ | ||
$this->authorize('create', Field::class); | ||
|
||
$request->validate([ | ||
'user_id' => 'required|exists:users,id', | ||
'area' => 'required|numeric', | ||
'description' => 'nullable|string', | ||
'latitude' => 'nullable|numeric|between:-90,90', | ||
'longitude' => 'nullable|numeric|between:-180,180', | ||
'province' => 'string|nullable', | ||
'city' => 'string|nullable', | ||
'district' => 'string|nullable', | ||
'subdistrct' => 'string|nullable' | ||
]); | ||
|
||
/** @var Field */ | ||
$field = Field::create($request->only(['user_id', 'area', 'description', 'longitude', 'latitude'])); | ||
|
||
$field->address()->create($request->only(['province', 'city', 'district', 'subdistrict'])); | ||
|
||
return redirect()->route('field.show', $field)->with('status', __('messages.success')); | ||
} | ||
|
||
public function show($field) | ||
{ | ||
$field = Field::withLastPlanting()->with(['user.organizations:id,name', 'address'])->findOrFail($field); | ||
|
||
$this->authorize('view', $field); | ||
|
||
if ($field->lastPlanting) { | ||
$field->last_activity = $field->lastPlanting->onfarms()->latest()->first(); | ||
} | ||
|
||
return Inertia::render('Field/Show', [ | ||
'field' => $field, | ||
]); | ||
} | ||
|
||
public function edit(Field $field) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
return Inertia::render('Field/Edit', [ | ||
'field' => $field->load('address', 'user.organizations') | ||
]); | ||
} | ||
|
||
public function update(Field $field, Request $request) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
$request->validate([ | ||
'area' => 'numeric', | ||
'description' => 'nullable|string', | ||
'latitude' => 'nullable|numeric|between:-90,90', | ||
'longitude' => 'nullable|numeric|between:-180,180', | ||
'province' => 'string|nullable', | ||
'city' => 'string|nullable', | ||
'district' => 'string|nullable', | ||
'subdistrct' => 'string|nullable' | ||
]); | ||
|
||
$field->update($request->only(['area', 'description', 'longitude', 'latitude'])); | ||
$field->address->update($request->only(['province', 'city', 'district', 'subdistrict'])); | ||
|
||
return redirect()->route('field.show', $field)->with('status', __('messages.success')); | ||
} | ||
|
||
public function destroy(Field $field) | ||
{ | ||
$this->authorize('delete', $field); | ||
|
||
$field->delete(); | ||
|
||
return redirect()->route('field.index')->with('status', __('messages.deleted')); | ||
} | ||
} |
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,111 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Field; | ||
use App\Models\Onfarm; | ||
use App\Models\Planting; | ||
use Illuminate\Http\Request; | ||
use Inertia\Inertia; | ||
|
||
class FieldPlantingController extends Controller | ||
{ | ||
public function index(Field $field, Request $request) | ||
{ | ||
$this->authorize('view', $field); | ||
|
||
$plantings = $field->plantings()->withLastOnfarm()->latest()->paginate(); | ||
|
||
return Inertia::render('Field/Planting/Index', [ | ||
'plantings' => $plantings, | ||
'field' => $field->load('user', 'address') | ||
]); | ||
} | ||
|
||
public function create(Field $field, Request $request) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
return Inertia::render('Field/Planting/Create', [ | ||
'field' => $field->load('user', 'address'), | ||
'varieties' => Planting::getSeedVarieties() | ||
]); | ||
} | ||
|
||
public function show(Field $field, $planting, Request $request) | ||
{ | ||
$this->authorize('view', $field); | ||
|
||
$planting = $field->plantings()->with(['onfarms' => function ($query) | ||
{ | ||
$query->with('media:id,model_id,model_type,name,disk')->orderBy('started_at', 'desc')->orderBy('id', 'desc'); | ||
}, 'field.user'])->findOrFail($planting); | ||
|
||
return Inertia::render('Field/Planting/Show', [ | ||
'planting' => $planting, | ||
'can' => [ | ||
'update' => $request->user()->can('update', $planting->field) | ||
] | ||
]); | ||
} | ||
|
||
public function edit(Field $field, $planting) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
$planting = $field->plantings()->findOrFail($planting); | ||
|
||
return Inertia::render('Field/Planting/Edit', [ | ||
'planting' => $planting, | ||
'field' => $field->load('address', 'user'), | ||
'varieties' => Planting::getSeedVarieties() | ||
]); | ||
} | ||
|
||
public function store(Field $field, Request $request) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
$request->validate([ | ||
'started_at' => 'required|date', | ||
'seed_variety' => 'required|string', | ||
'seed_quantity' => 'required|numeric', | ||
]); | ||
|
||
$planting = $field->plantings()->create([ | ||
'started_at' => $request->started_at, | ||
'seed_variety' => $request->seed_variety, | ||
'seed_quantity' => $request->seed_quantity, | ||
]); | ||
|
||
return redirect()->route('field.planting.show', [$field->id, $planting->id])->with('status', __('messages.success')); | ||
} | ||
|
||
public function update(Request $request, Field $field, $planting) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
$planting = $field->plantings()->findOrFail($planting); | ||
|
||
$data = $request->validate([ | ||
'started_at' => 'date', | ||
'seed_variety' => 'string', | ||
'seed_quantity' => 'numeric', | ||
]); | ||
|
||
$planting->update($data); | ||
|
||
return redirect()->route('field.planting.show', [$field, $planting])->with('status', __('messages.success')); | ||
} | ||
|
||
public function destroy(Field $field, $planting) | ||
{ | ||
$this->authorize('update', $field); | ||
|
||
$planting = $field->plantings()->findOrFail($planting); | ||
|
||
$planting->delete(); | ||
|
||
return redirect()->route('field.planting.index', $field)->with('status', __('messages.deleted')); | ||
} | ||
} |
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,41 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Onfarm; | ||
use Illuminate\Http\Request; | ||
|
||
class OnfarmMediaController extends Controller | ||
{ | ||
public function show(Onfarm $onfarm, $media) | ||
{ | ||
$this->authorize('view', $onfarm->planting->field); | ||
|
||
$media = $onfarm->media()->findOrFail($media); | ||
|
||
return response()->file($media->getPath()); | ||
} | ||
|
||
public function store(Onfarm $onfarm, Request $request) | ||
{ | ||
$this->authorize('update', $onfarm->planting->field); | ||
|
||
$request->validate([ | ||
'media' => 'required|file', | ||
'collection' => 'required' | ||
]); | ||
|
||
$media = $onfarm->addMediaFromRequest('media')->toMediaCollection($request->collection); | ||
|
||
return redirect()->back(); | ||
} | ||
|
||
public function destroy(Onfarm $onfarm, $media) | ||
{ | ||
$this->authorize('update', $onfarm->planting->field); | ||
|
||
$onfarm->media()->find($media)->delete(); | ||
|
||
return redirect()->back(); | ||
} | ||
} |
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.