|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use App\Http\Requests\StorePropertyRequest; |
| 7 | +use App\Http\Requests\UpdatePropertyRequest; |
| 8 | +use App\Http\Requests\StoreNoteRequest; |
| 9 | +use App\Http\Resources\PropertyResource; |
| 10 | +use App\Http\Resources\CertificateResource; |
| 11 | +use App\Http\Resources\NoteResource; |
| 12 | +use App\Models\Property; |
| 13 | +use App\Models\Note; |
| 14 | + |
| 15 | +class PropertyController extends Controller |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Display a listing of the resource. |
| 19 | + * added filter GET /property?min_certificates=5 |
| 20 | + */ |
| 21 | + public function index() |
| 22 | + { |
| 23 | +// $props = Property::withCount('certificates') |
| 24 | +// ->orderBy('id') |
| 25 | +// ->paginate(15); |
| 26 | +// |
| 27 | +// return PropertyResource::collection($props); |
| 28 | + |
| 29 | + //should add validation |
| 30 | + $min = request()->integer('min_certificates'); // null if not provided |
| 31 | + |
| 32 | + $query = Property::withCount('certificates'); |
| 33 | + |
| 34 | + // Apply filter only if a number is passed |
| 35 | + if (!is_null($min)) { |
| 36 | + $query->having('certificates_count', '>=', $min); |
| 37 | + } |
| 38 | + |
| 39 | + return PropertyResource::collection( |
| 40 | + $query->paginate(20) |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Store a newly created resource in storage. |
| 46 | + */ |
| 47 | + public function store(StorePropertyRequest $request) |
| 48 | + { |
| 49 | + $property = Property::create($request->validated()); |
| 50 | + return (new PropertyResource($property)) |
| 51 | + ->response() |
| 52 | + ->setStatusCode(201); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Display the specified resource. |
| 57 | + */ |
| 58 | + public function show(Property $property) |
| 59 | + { |
| 60 | + $property->loadCount('certificates'); |
| 61 | + return new PropertyResource($property); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Update the specified resource in storage. |
| 66 | + */ |
| 67 | + public function update(UpdatePropertyRequest $request, Property $property) |
| 68 | + { |
| 69 | + $property->update($request->validated()); |
| 70 | + return new PropertyResource($property->fresh()->loadCount('certificates')); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Remove the specified resource from storage. |
| 75 | + */ |
| 76 | + public function destroy(Property $property) |
| 77 | + { |
| 78 | + $property->delete(); |
| 79 | + return response()->json(['message' => 'Deleted'], 200); |
| 80 | + } |
| 81 | + |
| 82 | + /* |
| 83 | + * get certs by property id |
| 84 | + */ |
| 85 | + public function certificates(Property $property) |
| 86 | + { |
| 87 | + $certs = $property->certificates()->orderBy('id')->paginate(15); |
| 88 | + return CertificateResource::collection($certs); |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + * get notes by property id |
| 93 | + */ |
| 94 | + public function notes(Property $property) |
| 95 | + { |
| 96 | + $notes = Note::where('model_type', 'Property') |
| 97 | + ->where('model_id', $property->id) |
| 98 | + ->latest() |
| 99 | + ->paginate(15); |
| 100 | + |
| 101 | + return NoteResource::collection($notes); |
| 102 | + } |
| 103 | + |
| 104 | + /* |
| 105 | + * save/store property notes |
| 106 | + */ |
| 107 | + public function storeNote(StoreNoteRequest $request, Property $property) |
| 108 | + { |
| 109 | + $note = Note::create([ |
| 110 | + 'model_type' => 'Property', |
| 111 | + 'model_id' => $property->id, |
| 112 | + 'note' => $request->validated()['note'], |
| 113 | + ]); |
| 114 | + |
| 115 | + return (new NoteResource($note)) |
| 116 | + ->response() |
| 117 | + ->setStatusCode(201); |
| 118 | + } |
| 119 | +} |
0 commit comments