Panen
This commit is contained in:
parent
62a5456f9d
commit
ab4da668bd
124
app/Http/Controllers/Api/PanenController.php
Normal file
124
app/Http/Controllers/Api/PanenController.php
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\JsonResponse;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
use Illuminate\Support\Facades\Gate;
|
||||||
|
|
||||||
|
use App\Models\Map\Panen;
|
||||||
|
|
||||||
|
class PanenController extends Controller
|
||||||
|
{
|
||||||
|
public function index(): JsonResponse
|
||||||
|
{
|
||||||
|
Gate::authorize('petani');
|
||||||
|
|
||||||
|
$user = Auth::user();
|
||||||
|
// Mengasumsikan farmer_id di data panen merujuk pada profile->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
68
app/Models/Map/Panen.php
Normal file
68
app/Models/Map/Panen.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models\Map;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Panen extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
// Nama tabel sesuai DDL sebelumnya
|
||||||
|
protected $table = 'map_panen';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Atribut yang dapat diisi (mass assignable).
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'land_id',
|
||||||
|
'plant_id',
|
||||||
|
'farmer_id',
|
||||||
|
'commodity_name',
|
||||||
|
'weight_kg',
|
||||||
|
'harvest_date',
|
||||||
|
'notes',
|
||||||
|
'sync_status',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Casting tipe data agar konsisten saat diakses di Frontend/JSON.
|
||||||
|
*/
|
||||||
|
protected $casts = [
|
||||||
|
'weight_kg' => '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');
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -30,6 +30,7 @@
|
|||||||
Route::apiResource('/lahan', \App\Http\Controllers\Api\LahanController::class)->except(['create', 'edit']);
|
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('/tanaman', \App\Http\Controllers\Api\TanamanController::class)->except(['create', 'edit']);
|
||||||
Route::apiResource('/inspeksi', \App\Http\Controllers\Api\InspeksiController::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')
|
Route::middleware('auth:sanctum')
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user