Skip to content

Commit 820438b

Browse files
committed
properties listing
1 parent 630d4ee commit 820438b

13 files changed

+366
-131
lines changed

.DS_Store

0 Bytes
Binary file not shown.

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
This project is the backend API for a properties renting app
44

5+
## API Endpoints UI
6+
7+
![Properties API](/readme/properties-api.png)
8+
59
## DB - ER Diagram
610

7-
![ER Diagram](/er-diagram.png)
11+
![ER Diagram](/readme/er-diagram.png)
812

913
## License
1014

app/Http/Controllers/Api/v1/PropertyController.php

+57-19
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,72 @@
99
use App\Models\Property;
1010
use App\Http\Resources\v1\PropertyResource;
1111
use Illuminate\Http\Response;
12+
use App\Http\Requests\GenericListingRequest;
1213

1314
class PropertyController extends Controller
1415
{
1516
/**
16-
* Display a listing of the resource.
17+
* List properties
1718
*/
18-
public function index()
19+
public function index(GenericListingRequest $request)
1920
{
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+
38+
$request->validate([
39+
/**
40+
* Fields properties
41+
* @example id,name,slug,owner_id,status_id,created_at,updated_at
42+
*/
43+
'fields[properties]' => 'string',
44+
45+
/**
46+
* Fields owner
47+
* @example id,name
48+
*/
49+
'fields[owner]' => 'string',
50+
51+
/**
52+
* Fields address
53+
* @example id,city_id,address_line,created_at,updated_at
54+
*/
55+
'fields[address]' => 'string',
56+
57+
/**
58+
* Filter name
59+
* @example house
60+
*/
61+
'filter[name]' => 'string',
62+
63+
/**
64+
* Relationships.
65+
* @example owner,status,address,address.city,address.city.country
66+
*/
67+
'include' => 'string',
68+
'sort' => 'in:created_at,-created_at',
69+
]);
70+
2071
$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-
])
72+
->allowedFields($allowedFields)
3773
->defaultSort('-created_at')
3874
->allowedIncludes(['owner', 'status', 'address', 'address.city', 'address.city.country'])
39-
->paginate()
75+
->allowedFilters(['name'])
76+
->allowedSorts('created_at', '-created_at')
77+
->paginate(request('per_page', 15))
4078
->appends(request()->query());
4179

4280
return PropertyResource::collection($properties)->response()->setStatusCode(Response::HTTP_OK);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
use Illuminate\Validation\Rule;
7+
8+
class GenericListingRequest extends FormRequest
9+
{
10+
/**
11+
* Determine if the user is authorized to make this request.
12+
*/
13+
public function authorize(): bool
14+
{
15+
return true;
16+
}
17+
18+
/**
19+
* Get the validation rules that apply to the request.
20+
*
21+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
22+
*/
23+
public function rules(): array
24+
{
25+
return [
26+
'per_page' => 'int|min:1|max:100',
27+
'page' => 'int|min:1'
28+
];
29+
}
30+
}

app/Http/Requests/v1/StorePropertyRequest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests;
3+
namespace App\Http\Requests\v1;
44

55
use Illuminate\Foundation\Http\FormRequest;
66

@@ -22,7 +22,6 @@ public function authorize(): bool
2222
public function rules(): array
2323
{
2424
return [
25-
//
2625
];
2726
}
2827
}

app/Http/Requests/v1/UpdatePropertyRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Requests;
3+
namespace App\Http\Requests\v1;
44

55
use Illuminate\Foundation\Http\FormRequest;
66

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": "^8.2",
13+
"dedoc/scramble": "^0.11.30",
1314
"laravel/framework": "^11.31",
1415
"laravel/sanctum": "^4.0",
1516
"laravel/tinker": "^2.9",

0 commit comments

Comments
 (0)