feat: add patch request to projects

This commit is contained in:
Andras Bacsai 2024-07-23 11:48:38 +02:00
parent 38299ab507
commit e96e8f6fec
3 changed files with 147 additions and 1 deletions

View File

@ -247,7 +247,112 @@ public function create_project(Request $request)
'uuid' => $project->uuid, 'uuid' => $project->uuid,
'name' => $project->name, 'name' => $project->name,
'description' => $project->description, '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( #[OA\Delete(

View File

@ -3114,6 +3114,46 @@ paths:
security: security:
- -
bearerAuth: [] 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}': '/projects/{uuid}/{environment_name}':
get: get:
tags: tags:

View File

@ -42,6 +42,7 @@
Route::get('/projects/{uuid}/{environment_name}', [ProjectController::class, 'environment_details']); Route::get('/projects/{uuid}/{environment_name}', [ProjectController::class, 'environment_details']);
Route::post('/projects', [ProjectController::class, 'create_project']); Route::post('/projects', [ProjectController::class, 'create_project']);
Route::patch('/projects/{uuid}', [ProjectController::class, 'update_project']);
Route::delete('/projects/{uuid}', [ProjectController::class, 'delete_project']); Route::delete('/projects/{uuid}', [ProjectController::class, 'delete_project']);
Route::get('/security/keys', [SecurityController::class, 'keys']); Route::get('/security/keys', [SecurityController::class, 'keys']);