diff --git a/app/Http/Controllers/Api/PanenController.php b/app/Http/Controllers/Api/PanenController.php new file mode 100644 index 0000000..69d4cb1 --- /dev/null +++ b/app/Http/Controllers/Api/PanenController.php @@ -0,0 +1,124 @@ +id atau user_id + $listPanen = Panen::where('farmer_id', $user->profile->id)->get(); + + return response()->json($listPanen); + } + + public function store(Request $request): JsonResponse + { + Gate::authorize('petani'); + + $validated = $request->validate([ + 'land_id' => ['required', 'integer'], + 'plant_id' => ['required', 'integer'], + 'commodity_name' => ['required', 'string', 'max:255'], + 'weight_kg' => ['required', 'numeric'], + 'harvest_date' => ['required', 'date'], + 'notes' => ['nullable', 'string'], + ]); + + $panen = Panen::create([ + 'land_id' => $validated['land_id'], + 'plant_id' => $validated['plant_id'], + 'farmer_id' => Auth::user()->profile->id, + 'commodity_name' => $validated['commodity_name'], + 'weight_kg' => $validated['weight_kg'], + 'harvest_date' => $validated['harvest_date'], + 'notes' => $validated['notes'] ?? null, + 'sync_status' => 'synced' + ]); + + return response()->json($panen, 201); + } + + public function show(string $id): JsonResponse + { + Gate::authorize('petani'); + $panen = Panen::findOrFail($id); + return response()->json($panen); + } + + public function update(Request $request, string $id): JsonResponse + { + Gate::authorize('petani'); + $panen = Panen::findOrFail($id); + + $validated = $request->validate([ + 'commodity_name' => ['string', 'max:255'], + 'weight_kg' => ['numeric'], + 'harvest_date' => ['date'], + 'notes' => ['nullable', 'string'], + ]); + + $panen->update($validated); + return response()->json($panen); + } + + public function destroy(string $id): JsonResponse + { + Gate::authorize('petani'); + Panen::findOrFail($id)->delete(); + return response()->json(null, 204); + } + + /** + * Digunakan untuk sinkronisasi massal dari data lokal (offline) + */ + public function batchUpsert(Request $request): JsonResponse + { + Gate::authorize('petani'); + $profileId = Auth::user()->profile->id; + + $validated = $request->validate([ + 'panens' => ['required', 'array'], + 'panens.*.id' => ['nullable', 'integer'], + 'panens.*.land_id' => ['required', 'integer'], + 'panens.*.plant_id' => ['required', 'integer'], + 'panens.*.commodity_name' => ['required', 'string'], + 'panens.*.weight_kg' => ['required', 'numeric'], + 'panens.*.harvest_date' => ['required', 'date'], + ]); + + $dataUpsert = []; + foreach ($validated['panens'] as $item) { + $dataUpsert[] = [ + 'id' => $item['id'] ?? null, + 'land_id' => $item['land_id'], + 'plant_id' => $item['plant_id'], + 'farmer_id' => $profileId, + 'commodity_name' => $item['commodity_name'], + 'weight_kg' => $item['weight_kg'], + 'harvest_date' => $item['harvest_date'], + 'sync_status' => 'synced', + 'created_at' => now(), + 'updated_at' => now(), + ]; + } + + // Melakukan upsert berdasarkan ID + Panen::upsert($dataUpsert, ['id'], [ + 'weight_kg', 'commodity_name', 'harvest_date', 'sync_status', 'updated_at' + ]); + + return response()->json(['status' => 'success'], 200); + } +} \ No newline at end of file diff --git a/app/Models/Map/Panen.php b/app/Models/Map/Panen.php new file mode 100644 index 0000000..bacb71e --- /dev/null +++ b/app/Models/Map/Panen.php @@ -0,0 +1,68 @@ + 'float', + 'harvest_date' => 'date:Y-m-d', + 'land_id' => 'integer', + 'plant_id' => 'integer', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + + /** + * Relasi ke Lahan (Many-to-One) + */ + public function lahan(): BelongsTo + { + return $this->belongsTo(Lahan::class, 'land_id'); + } + + /** + * Relasi ke Tanaman (Many-to-One) + */ + public function tanaman(): BelongsTo + { + // Pastikan nama model Tanaman sudah sesuai (misal: Tanaman atau Plant) + return $this->belongsTo(Tanaman::class, 'plant_id'); + } + + /** + * Relasi ke Profile Petani (Many-to-One) + */ + public function petani(): BelongsTo + { + // farmer_id merujuk ke tabel profiles (profile_id) + return $this->belongsTo(\App\Models\Profile::class, 'farmer_id'); + } +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 6e74b61..713c600 100644 --- a/routes/api.php +++ b/routes/api.php @@ -30,6 +30,7 @@ Route::apiResource('/lahan', \App\Http\Controllers\Api\LahanController::class)->except(['create', 'edit']); Route::apiResource('/tanaman', \App\Http\Controllers\Api\TanamanController::class)->except(['create', 'edit']); Route::apiResource('/inspeksi', \App\Http\Controllers\Api\InspeksiController::class)->except(['create', 'edit']); + Route::apiResource('/panen', \App\Http\Controllers\Api\PanenController::class)->except(['create', 'edit']); }); Route::middleware('auth:sanctum')