-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBrandController.php
More file actions
187 lines (169 loc) · 6.02 KB
/
Copy pathBrandController.php
File metadata and controls
187 lines (169 loc) · 6.02 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace App\Http\Controllers\API;
use App\Brands;
use App\Helpers\Fixometer;
use App\Http\Controllers\Controller;
use App\Http\Resources\Brand;
use App\Http\Resources\BrandCollection;
use Auth;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class BrandController extends Controller
{
/**
* @OA\Get(
* path="/api/v2/brands",
* operationId="listBrandsv2",
* tags={"Brands"},
* summary="List Brands",
* description="Returns all device brands, ordered alphabetically. Public endpoint.",
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(ref="#/components/schemas/Brand")
* )
* )
* )
* )
*/
public function listBrandsv2()
{
$brands = Brands::orderBy('brand_name', 'asc')->get();
return BrandCollection::make($brands);
}
/**
* @OA\Get(
* path="/api/v2/brands/{id}",
* operationId="getBrandv2",
* tags={"Brands"},
* summary="Get a Brand",
* description="Returns a single brand by id.",
* @OA\Parameter(
* name="id",
* in="path",
* required=true,
* @OA\Schema(type="integer")
* ),
* @OA\Response(
* response=200,
* description="Successful operation",
* @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/Brand")
* )
* ),
* @OA\Response(response=404, description="Brand not found")
* )
*/
public function getBrandv2($id)
{
$brand = Brands::findOrFail($id);
return Brand::make($brand);
}
/**
* @OA\Post(
* path="/api/v2/brands",
* operationId="createBrandv2",
* tags={"Brands"},
* summary="Create a Brand",
* description="Create a new device brand. Administrator only.",
* security={{"apiToken":{}}},
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"brand_name"},
* @OA\Property(property="brand_name", type="string", maxLength=255, example="Sony")
* )
* ),
* @OA\Response(
* response=201,
* description="Brand created",
* @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/Brand")
* )
* ),
* @OA\Response(response=401, description="Unauthenticated"),
* @OA\Response(response=403, description="Forbidden"),
* @OA\Response(response=422, description="Validation failed")
* )
*/
public function createBrandv2(Request $request): JsonResponse
{
if (!Fixometer::hasRole(Auth::user(), 'Administrator')) {
return response()->json(['message' => 'Forbidden'], 403);
}
$validated = $request->validate([
'brand_name' => 'required|string|max:255|unique:brands,brand_name',
]);
$brand = Brands::create($validated);
return response()->json(['data' => (new Brand($brand))->toArray($request)], 201);
}
/**
* @OA\Put(
* path="/api/v2/brands/{id}",
* operationId="updateBrandv2",
* tags={"Brands"},
* summary="Update a Brand",
* description="Update a brand. Administrator only.",
* security={{"apiToken":{}}},
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"brand_name"},
* @OA\Property(property="brand_name", type="string", maxLength=255, example="Sony")
* )
* ),
* @OA\Response(
* response=200,
* description="Brand updated",
* @OA\JsonContent(
* @OA\Property(property="data", ref="#/components/schemas/Brand")
* )
* ),
* @OA\Response(response=401, description="Unauthenticated"),
* @OA\Response(response=403, description="Forbidden"),
* @OA\Response(response=404, description="Brand not found"),
* @OA\Response(response=422, description="Validation failed")
* )
*/
public function updateBrandv2(Request $request, $id)
{
if (!Fixometer::hasRole(Auth::user(), 'Administrator')) {
return response()->json(['message' => 'Forbidden'], 403);
}
$brand = Brands::findOrFail($id);
$validated = $request->validate([
'brand_name' => 'required|string|max:255|unique:brands,brand_name,' . $brand->id,
]);
$brand->update($validated);
return Brand::make($brand->fresh());
}
/**
* @OA\Delete(
* path="/api/v2/brands/{id}",
* operationId="deleteBrandv2",
* tags={"Brands"},
* summary="Delete a Brand",
* description="Delete a brand. Administrator only.",
* security={{"apiToken":{}}},
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
* @OA\Response(response=204, description="Brand deleted"),
* @OA\Response(response=401, description="Unauthenticated"),
* @OA\Response(response=403, description="Forbidden"),
* @OA\Response(response=404, description="Brand not found")
* )
*/
public function deleteBrandv2($id)
{
if (!Fixometer::hasRole(Auth::user(), 'Administrator')) {
return response()->json(['message' => 'Forbidden'], 403);
}
$brand = Brands::findOrFail($id);
$brand->delete();
return response()->noContent();
}
}