Skip to content

Commit e96e8f6

Browse files
committed
feat: add patch request to projects
1 parent 38299ab commit e96e8f6

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

app/Http/Controllers/Api/ProjectController.php

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,112 @@ public function create_project(Request $request)
247247
'uuid' => $project->uuid,
248248
'name' => $project->name,
249249
'description' => $project->description,
250-
])->status(201);
250+
])->setStatusCode(201);
251+
}
252+
253+
#[OA\Patch(
254+
summary: 'Update Project',
255+
description: 'Update Project.',
256+
path: '/projects/{uuid}',
257+
security: [
258+
['bearerAuth' => []],
259+
],
260+
tags: ['Projects'],
261+
requestBody: new OA\RequestBody(
262+
required: true,
263+
description: 'Project updated.',
264+
content: new OA\MediaType(
265+
mediaType: 'application/json',
266+
schema: new OA\Schema(
267+
type: 'object',
268+
properties: [
269+
'name' => ['type' => 'string', 'description' => 'The name of the project.'],
270+
'description' => ['type' => 'string', 'description' => 'The description of the project.'],
271+
],
272+
),
273+
),
274+
),
275+
responses: [
276+
new OA\Response(
277+
response: 201,
278+
description: 'Project updated.',
279+
content: [
280+
new OA\MediaType(
281+
mediaType: 'application/json',
282+
schema: new OA\Schema(
283+
type: 'object',
284+
properties: [
285+
'uuid' => ['type' => 'string', 'example' => 'og888os'],
286+
'name' => ['type' => 'string', 'example' => 'Project Name'],
287+
'description' => ['type' => 'string', 'example' => 'Project Description'],
288+
]
289+
)
290+
),
291+
]),
292+
new OA\Response(
293+
response: 401,
294+
ref: '#/components/responses/401',
295+
),
296+
new OA\Response(
297+
response: 400,
298+
ref: '#/components/responses/400',
299+
),
300+
new OA\Response(
301+
response: 404,
302+
ref: '#/components/responses/404',
303+
),
304+
]
305+
)]
306+
public function update_project(Request $request)
307+
{
308+
$allowedFields = ['name', 'description'];
309+
310+
$teamId = getTeamIdFromToken();
311+
if (is_null($teamId)) {
312+
return invalidTokenResponse();
313+
}
314+
315+
$return = validateIncomingRequest($request);
316+
if ($return instanceof \Illuminate\Http\JsonResponse) {
317+
return $return;
318+
}
319+
$validator = customApiValidator($request->all(), [
320+
'name' => 'string|max:255|nullable',
321+
'description' => 'string|nullable',
322+
]);
323+
324+
$extraFields = array_diff(array_keys($request->all()), $allowedFields);
325+
if ($validator->fails() || ! empty($extraFields)) {
326+
$errors = $validator->errors();
327+
if (! empty($extraFields)) {
328+
foreach ($extraFields as $field) {
329+
$errors->add($field, 'This field is not allowed.');
330+
}
331+
}
332+
333+
return response()->json([
334+
'message' => 'Validation failed.',
335+
'errors' => $errors,
336+
], 422);
337+
}
338+
$uuid = $request->uuid;
339+
if (! $uuid) {
340+
return response()->json(['message' => 'Uuid is required.'], 422);
341+
}
342+
343+
$project = Project::whereTeamId($teamId)->whereUuid($uuid)->first();
344+
if (! $project) {
345+
return response()->json(['message' => 'Project not found.'], 404);
346+
}
347+
348+
$project->update($request->only($allowedFields));
349+
350+
return response()->json([
351+
'uuid' => $project->uuid,
352+
'name' => $project->name,
353+
'description' => $project->description,
354+
])->setStatusCode(201);
355+
251356
}
252357

253358
#[OA\Delete(

openapi.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,46 @@ paths:
31143114
security:
31153115
-
31163116
bearerAuth: []
3117+
patch:
3118+
tags:
3119+
- Projects
3120+
summary: 'Update Project'
3121+
description: 'Update Project.'
3122+
operationId: 2db343bd6fc14c658cb51a2b73b2f842
3123+
requestBody:
3124+
description: 'Project updated.'
3125+
required: true
3126+
content:
3127+
application/json:
3128+
schema:
3129+
properties:
3130+
name:
3131+
type: string
3132+
description: 'The name of the project.'
3133+
description:
3134+
type: string
3135+
description: 'The description of the project.'
3136+
type: object
3137+
responses:
3138+
'201':
3139+
description: 'Project updated.'
3140+
content:
3141+
application/json:
3142+
schema:
3143+
properties:
3144+
uuid: { type: string, example: og888os }
3145+
name: { type: string, example: 'Project Name' }
3146+
description: { type: string, example: 'Project Description' }
3147+
type: object
3148+
'401':
3149+
$ref: '#/components/responses/401'
3150+
'400':
3151+
$ref: '#/components/responses/400'
3152+
'404':
3153+
$ref: '#/components/responses/404'
3154+
security:
3155+
-
3156+
bearerAuth: []
31173157
'/projects/{uuid}/{environment_name}':
31183158
get:
31193159
tags:

routes/api.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
Route::get('/projects/{uuid}/{environment_name}', [ProjectController::class, 'environment_details']);
4343

4444
Route::post('/projects', [ProjectController::class, 'create_project']);
45+
Route::patch('/projects/{uuid}', [ProjectController::class, 'update_project']);
4546
Route::delete('/projects/{uuid}', [ProjectController::class, 'delete_project']);
4647

4748
Route::get('/security/keys', [SecurityController::class, 'keys']);

0 commit comments

Comments
 (0)