Skip to content

Commit e898d87

Browse files
committed
properties listing api endpoint
1 parent 3cccd08 commit e898d87

18 files changed

+622
-69
lines changed

.DS_Store

8 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\v1;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\v1\StorePropertyRequest;
7+
use App\Http\Requests\v1\UpdatePropertyRequest;
8+
use Spatie\QueryBuilder\QueryBuilder;
9+
use App\Models\Property;
10+
use App\Http\Resources\v1\PropertyResource;
11+
use Illuminate\Http\Response;
12+
13+
class PropertyController extends Controller
14+
{
15+
/**
16+
* Display a listing of the resource.
17+
*/
18+
public function index()
19+
{
20+
$properties = QueryBuilder::for(Property::class)
21+
->allowedFields([
22+
'id',
23+
'name',
24+
'slug',
25+
'owner_id',
26+
'status_id',
27+
'created_at',
28+
'updated_at',
29+
'owner.id',
30+
'owner.name',
31+
'status.id',
32+
'status.name',
33+
'address.id',
34+
'address.city_id',
35+
'address.address_line',
36+
])
37+
->defaultSort('-created_at')
38+
->allowedIncludes(['owner', 'status', 'address', 'address.city', 'address.city.country'])
39+
->paginate()
40+
->appends(request()->query());
41+
42+
return PropertyResource::collection($properties)->response()->setStatusCode(Response::HTTP_OK);
43+
}
44+
45+
/**
46+
* Store a newly created resource in storage.
47+
*/
48+
public function store(StorePropertyRequest $request)
49+
{
50+
//
51+
}
52+
53+
/**
54+
* Display the specified resource.
55+
*/
56+
public function show(Property $property)
57+
{
58+
//
59+
}
60+
61+
/**
62+
* Update the specified resource in storage.
63+
*/
64+
public function update(UpdatePropertyRequest $request, Property $property)
65+
{
66+
//
67+
}
68+
69+
/**
70+
* Remove the specified resource from storage.
71+
*/
72+
public function destroy(Property $property)
73+
{
74+
//
75+
}
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StorePropertyRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return false;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
//
26+
];
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class UpdatePropertyRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return false;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
//
26+
];
27+
}
28+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
use App\Http\Resources\v1\CountryResource;
8+
9+
class CityResource extends JsonResource
10+
{
11+
/**
12+
* Transform the resource into an array.
13+
*
14+
* @return array<string, mixed>
15+
*/
16+
public function toArray(Request $request): array
17+
{
18+
return [
19+
'id' => $this->id,
20+
'name' => $this->name,
21+
'country' => $this->whenLoaded('country', function() {
22+
return new CountryResource($this->country);
23+
})
24+
];
25+
}
26+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class CountryResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return [
18+
'id' => $this->id,
19+
'name' => $this->name,
20+
];
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
use App\Http\Resources\v1\CityResource;
8+
9+
class PropertyAddressResource extends JsonResource
10+
{
11+
/**
12+
* Transform the resource into an array.
13+
*
14+
* @return array<string, mixed>
15+
*/
16+
public function toArray(Request $request): array
17+
{
18+
return [
19+
'city_id' => $this->city_id,
20+
'city' => $this->whenLoaded('city', function() {
21+
return new CityResource($this->city);
22+
}),
23+
'address_line' => $this->address_line,
24+
];
25+
}
26+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
use App\Http\Resources\v1\PropertyAddressResource;
8+
use App\Http\Resources\v1\UserResource;
9+
use App\Http\Resources\v1\PropertyStatusResource;
10+
11+
class PropertyResource extends JsonResource
12+
{
13+
/**
14+
* Transform the resource into an array.
15+
*
16+
* @return array<string, mixed>
17+
*/
18+
public function toArray(Request $request): array
19+
{
20+
return [
21+
'id' => $this->id,
22+
'name' => $this->name,
23+
'slug' => $this->slug,
24+
'address' => $this->whenLoaded('address', function() {
25+
return new PropertyAddressResource($this->address);
26+
}),
27+
'owner' => $this->whenLoaded('owner', function() {
28+
return new UserResource($this->owner);
29+
}),
30+
'status' => $this->whenLoaded('status', function() {
31+
return new PropertyStatusResource($this->status);
32+
}),
33+
'created_at' => $this->created_at,
34+
'updated_at' => $this->updated_at,
35+
];
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class PropertyStatusResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return [
18+
'id' => $this->id,
19+
'name' => $this->name
20+
];
21+
}
22+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Http\Resources\v1;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class UserResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
return [
18+
'name' => $this->name,
19+
'email' => $this->email
20+
];
21+
}
22+
}

app/Models/User.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
use Illuminate\Foundation\Auth\User as Authenticatable;
88
use Illuminate\Notifications\Notifiable;
99
use App\Models\Relationships\UserRelationships;
10+
use Laravel\Sanctum\HasApiTokens;
1011

1112
class User extends Authenticatable
1213
{
1314
/** @use HasFactory<\Database\Factories\UserFactory> */
14-
use HasFactory, Notifiable, UserRelationships;
15+
use HasFactory, Notifiable, UserRelationships, HasApiTokens;
1516

1617
/**
1718
* The attributes that are mass assignable.

bootstrap/app.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
return Application::configure(basePath: dirname(__DIR__))
88
->withRouting(
99
web: __DIR__.'/../routes/web.php',
10+
api: __DIR__.'/../routes/api.php',
1011
commands: __DIR__.'/../routes/console.php',
1112
health: '/up',
1213
)

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"require": {
1212
"php": "^8.2",
1313
"laravel/framework": "^11.31",
14-
"laravel/tinker": "^2.9"
14+
"laravel/sanctum": "^4.0",
15+
"laravel/tinker": "^2.9",
16+
"spatie/laravel-query-builder": "^6.2"
1517
},
1618
"require-dev": {
1719
"fakerphp/faker": "^1.23",

0 commit comments

Comments
 (0)