Skip to content

Commit 023edf3

Browse files
committed
Add api end points
1 parent f760dde commit 023edf3

File tree

11 files changed

+461
-2
lines changed

11 files changed

+461
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,32 @@ testing
2424
# How To Deploy
2525

2626
### For first time only !
27-
- `git clone https://github.com/refactorian/laravel-docker.git`
28-
- `cd laravel-docker`
27+
- `git clone https://github.com/newma/laravel-test.git`
28+
- `cd laravel-test`
2929
- `docker compose up -d --build`
3030
- `docker compose exec php bash`
3131
- `composer setup`
32+
- `php artisan migrate:fresh`
33+
- `php artisan db:seed`
34+
35+
### Your request to write the raw query:
36+
````
37+
# The n+1 version
38+
SELECT *
39+
FROM properties
40+
WHERE (
41+
SELECT COUNT(*)
42+
FROM certificates
43+
WHERE certificates.property_id = properties.id
44+
) > 5;
45+
46+
# Optimised version
47+
SELECT p.*, COUNT(c.id) AS cert_count
48+
FROM properties p
49+
JOIN certificates c ON c.property_id = p.id
50+
GROUP BY p.id
51+
HAVING COUNT(c.id) > 5;
52+
````
3253

3354
### From the second time onwards
3455
- `docker compose up -d`
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use App\Http\Requests\StoreCertificateRequest;
7+
use App\Http\Requests\StoreNoteRequest;
8+
use App\Http\Resources\CertificateResource;
9+
use App\Http\Resources\NoteResource;
10+
use App\Models\Certificate;
11+
use App\Models\Note;
12+
13+
class CertificateController extends Controller
14+
{
15+
/**
16+
* Display a listing of the resource.
17+
*/
18+
public function index()
19+
{
20+
$certs = Certificate::orderBy('id')->paginate(15);
21+
return CertificateResource::collection($certs);
22+
}
23+
24+
/**
25+
* Store a newly created resource in storage.
26+
*/
27+
public function store(StoreCertificateRequest $request)
28+
{
29+
$cert = Certificate::create($request->validated());
30+
return (new CertificateResource($cert))
31+
->response()
32+
->setStatusCode(201);
33+
}
34+
35+
/**
36+
* Display the specified resource.
37+
*/
38+
public function show(Certificate $certificate)
39+
{
40+
return new CertificateResource($certificate);
41+
}
42+
43+
/**
44+
* Update the specified resource in storage.
45+
*/
46+
public function update(Request $request, string $id)
47+
{
48+
//
49+
}
50+
51+
/**
52+
* Remove the specified resource from storage.
53+
*/
54+
public function destroy(string $id)
55+
{
56+
//
57+
}
58+
59+
/*
60+
* get notes by cert
61+
*/
62+
public function notes(Certificate $certificate)
63+
{
64+
$notes = Note::where('model_type', 'Certificate')
65+
->where('model_id', $certificate->id)
66+
->latest()
67+
->paginate(15);
68+
69+
return NoteResource::collection($notes);
70+
}
71+
72+
/*
73+
* store note for cert
74+
*/
75+
public function storeNote(StoreNoteRequest $request, Certificate $certificate)
76+
{
77+
$note = Note::create([
78+
'model_type' => 'Certificate',
79+
'model_id' => $certificate->id,
80+
'note' => $request->validated()['note'],
81+
]);
82+
83+
return (new NoteResource($note))
84+
->response()
85+
->setStatusCode(201);
86+
}
87+
88+
89+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class StoreCertificateRequest 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+
'stream_name' => 'required|string|max:50',
26+
'property_id' => 'required|integer|exists:properties,id',
27+
'issue_date' => 'required|date',
28+
'next_due_date' => 'required|date|after_or_equal:issue_date',
29+
];
30+
}
31+
}
Lines changed: 28 additions & 0 deletions
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 StoreNoteRequest 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+
'note' => 'required|string|max:5000',
26+
];
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
'organisation' => 'required|string|max:255',
26+
'property_type' => 'required|string|max:50',
27+
'parent_property_id' => 'nullable|integer|exists:properties,id',
28+
'uprn' => 'required|string|max:255',
29+
'address' => 'required|string|max:255',
30+
'town' => 'nullable|string|max:255',
31+
'postcode' => 'required|string|max:20',
32+
'live' => 'boolean',
33+
];
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
'organisation' => 'sometimes|string|max:255',
26+
'property_type' => 'sometimes|string|max:50',
27+
'parent_property_id' => 'nullable|integer|exists:properties,id',
28+
'uprn' => 'sometimes|string|max:255',
29+
'address' => 'sometimes|string|max:255',
30+
'town' => 'nullable|string|max:255',
31+
'postcode' => 'sometimes|string|max:20',
32+
'live' => 'boolean',
33+
34+
];
35+
}
36+
}

0 commit comments

Comments
 (0)