-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPlatformConnectionController.php
More file actions
95 lines (87 loc) · 3.99 KB
/
PlatformConnectionController.php
File metadata and controls
95 lines (87 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Requests\PlatformConnectionRequest;
use App\Http\Requests\ShowPlatformConnectionRequest;
use App\Http\Requests\StorePlatformConnectionRequest;
use App\Http\Requests\UpdatePlatformConnectionRequest;
use App\Models\PlatformConnection;
use App\Http\Resources\PlatformConnectionResource;
class PlatformConnectionController extends Controller
{
/* Get all platform connections */
//*************************************************************
// GET: /api/v1/platform_connection/
// Request $req example:
// { "consumer_id": 1 }
// *************************************************************
public function index(): \Illuminate\Http\Resources\Json\AnonymousResourceCollection
{
return PlatformConnectionResource::collection(PlatformConnection::all());
}
/* Create a new platform connection */
//*************************************************************
// POST: /api/v1/platform_connection/
// PlatformConnectionRequest $req example:
// { "consumer_id": 1, "provider_id": 1, "state": "enabled" }
// *************************************************************
public function store(StorePlatformConnectionRequest $req): PlatformConnectionResource
{
$validated = $req->validated();
$exists = PlatformConnection::where($validated)->first();
if ($exists) {
return response()->json(['error' => 'Platform connection already exists'], 401);
}
try {
$platform_connection = PlatformConnection::create($req->validated());
return new PlatformConnectionResource($platform_connection);
} catch (\Exception) {
return response()->json(INVALID_REQUEST_BODY, 401);
}
}
// Get a specific platform connection by consumer or provider id
// *************************************************************
// GET: /api/v1/platform_connection/{id}
// Request $req example:
// "consumer_id": 1 || "provider_id": 1
// *************************************************************
public function show(ShowPlatformConnectionRequest $req): PlatformConnectionResource
{
try {
$validated = $req->validated();
return new PlatformConnectionResource(PlatformConnection::where($validated)->first());
} catch (\Exception) {
return response()->json(INVALID_REQUEST_BODY, 401);
}
}
// Update a platform connection (The only possible update here would be the state)
// *************************************************************
// PUT: /api/v1/platform_connection/{request_body}
// Request $req example:
// { "consumer_id": 1, "provider_id": 1, "state": "enabled" }
// *************************************************************
public function update(UpdatePlatformConnectionRequest $req): PlatformConnectionResource
{
$validated = $req->validated();
$PlatformConnection = PlatformConnection::where($validated)->first();
$PlatformConnection->state = $validated['state'];
if (!$PlatformConnection) {
return response()->json(['error' => 'No matching platform connection found'], 401);
}
return new PlatformConnectionResource($PlatformConnection->save());
}
// Delete a platform connection
// *************************************************************
// DELETE: /api/v1/platform_connection/{request_body}
// Request $req example:
// { "consumer_id": 1, "provider_id": 1 }
// *************************************************************
public function delete(ShowPlatformConnectionRequest $req): \Illuminate\Http\JsonResponse
{
try {
PlatformConnectionRequest::where($req->validated())->delete();
return response()->json(['success' => 'Platform connection deleted successfully'], 200);
} catch (\Exception) {
return response()->json(INVALID_REQUEST_BODY, 401);
}
}
}