From e96e8f6fecf3bee4c838bde5817df721fec6d74e Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Tue, 23 Jul 2024 11:48:38 +0200 Subject: [PATCH] feat: add patch request to projects --- .../Controllers/Api/ProjectController.php | 107 +++++++++++++++++- openapi.yaml | 40 +++++++ routes/api.php | 1 + 3 files changed, 147 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/ProjectController.php b/app/Http/Controllers/Api/ProjectController.php index aa3e5552b..6ae1825e9 100644 --- a/app/Http/Controllers/Api/ProjectController.php +++ b/app/Http/Controllers/Api/ProjectController.php @@ -247,7 +247,112 @@ public function create_project(Request $request) 'uuid' => $project->uuid, 'name' => $project->name, 'description' => $project->description, - ])->status(201); + ])->setStatusCode(201); + } + + #[OA\Patch( + summary: 'Update Project', + description: 'Update Project.', + path: '/projects/{uuid}', + security: [ + ['bearerAuth' => []], + ], + tags: ['Projects'], + requestBody: new OA\RequestBody( + required: true, + description: 'Project updated.', + content: new OA\MediaType( + mediaType: 'application/json', + schema: new OA\Schema( + type: 'object', + properties: [ + 'name' => ['type' => 'string', 'description' => 'The name of the project.'], + 'description' => ['type' => 'string', 'description' => 'The description of the project.'], + ], + ), + ), + ), + responses: [ + new OA\Response( + response: 201, + description: 'Project updated.', + content: [ + new OA\MediaType( + mediaType: 'application/json', + schema: new OA\Schema( + type: 'object', + properties: [ + 'uuid' => ['type' => 'string', 'example' => 'og888os'], + 'name' => ['type' => 'string', 'example' => 'Project Name'], + 'description' => ['type' => 'string', 'example' => 'Project Description'], + ] + ) + ), + ]), + new OA\Response( + response: 401, + ref: '#/components/responses/401', + ), + new OA\Response( + response: 400, + ref: '#/components/responses/400', + ), + new OA\Response( + response: 404, + ref: '#/components/responses/404', + ), + ] + )] + public function update_project(Request $request) + { + $allowedFields = ['name', 'description']; + + $teamId = getTeamIdFromToken(); + if (is_null($teamId)) { + return invalidTokenResponse(); + } + + $return = validateIncomingRequest($request); + if ($return instanceof \Illuminate\Http\JsonResponse) { + return $return; + } + $validator = customApiValidator($request->all(), [ + 'name' => 'string|max:255|nullable', + 'description' => 'string|nullable', + ]); + + $extraFields = array_diff(array_keys($request->all()), $allowedFields); + if ($validator->fails() || ! empty($extraFields)) { + $errors = $validator->errors(); + if (! empty($extraFields)) { + foreach ($extraFields as $field) { + $errors->add($field, 'This field is not allowed.'); + } + } + + return response()->json([ + 'message' => 'Validation failed.', + 'errors' => $errors, + ], 422); + } + $uuid = $request->uuid; + if (! $uuid) { + return response()->json(['message' => 'Uuid is required.'], 422); + } + + $project = Project::whereTeamId($teamId)->whereUuid($uuid)->first(); + if (! $project) { + return response()->json(['message' => 'Project not found.'], 404); + } + + $project->update($request->only($allowedFields)); + + return response()->json([ + 'uuid' => $project->uuid, + 'name' => $project->name, + 'description' => $project->description, + ])->setStatusCode(201); + } #[OA\Delete( diff --git a/openapi.yaml b/openapi.yaml index 98bb37914..23c00e3e4 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -3114,6 +3114,46 @@ paths: security: - bearerAuth: [] + patch: + tags: + - Projects + summary: 'Update Project' + description: 'Update Project.' + operationId: 2db343bd6fc14c658cb51a2b73b2f842 + requestBody: + description: 'Project updated.' + required: true + content: + application/json: + schema: + properties: + name: + type: string + description: 'The name of the project.' + description: + type: string + description: 'The description of the project.' + type: object + responses: + '201': + description: 'Project updated.' + content: + application/json: + schema: + properties: + uuid: { type: string, example: og888os } + name: { type: string, example: 'Project Name' } + description: { type: string, example: 'Project Description' } + type: object + '401': + $ref: '#/components/responses/401' + '400': + $ref: '#/components/responses/400' + '404': + $ref: '#/components/responses/404' + security: + - + bearerAuth: [] '/projects/{uuid}/{environment_name}': get: tags: diff --git a/routes/api.php b/routes/api.php index 2224722e4..bb5c836ec 100644 --- a/routes/api.php +++ b/routes/api.php @@ -42,6 +42,7 @@ Route::get('/projects/{uuid}/{environment_name}', [ProjectController::class, 'environment_details']); Route::post('/projects', [ProjectController::class, 'create_project']); + Route::patch('/projects/{uuid}', [ProjectController::class, 'update_project']); Route::delete('/projects/{uuid}', [ProjectController::class, 'delete_project']); Route::get('/security/keys', [SecurityController::class, 'keys']);