api: able to update application

This commit is contained in:
Andras Bacsai 2024-06-25 15:05:51 +02:00
parent 54c4296a25
commit 41268fa20b

View File

@ -7,6 +7,7 @@
use App\Models\Application; use App\Models\Application;
use App\Models\Project; use App\Models\Project;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
class Applications extends Controller class Applications extends Controller
@ -45,6 +46,7 @@ public function application_by_uuid(Request $request)
public function update_by_uuid(Request $request) public function update_by_uuid(Request $request)
{ {
ray()->clearAll();
$teamId = get_team_id_from_token(); $teamId = get_team_id_from_token();
if (is_null($teamId)) { if (is_null($teamId)) {
return invalid_token(); return invalid_token();
@ -63,23 +65,57 @@ public function update_by_uuid(Request $request)
'message' => 'Application not found', 'message' => 'Application not found',
], 404); ], 404);
} }
ray($request->collect()); $allowedFields = ['name', 'domains'];
$validator = Validator::make($request->all(), [
'name' => 'string|max:255',
'domains' => 'string',
]);
$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.');
}
}
// if ($request->has('domains')) { return response()->json([
// $existingDomains = explode(',', $application->fqdn); 'message' => 'Validation failed',
// $newDomains = $request->domains; 'errors' => $errors,
// $filteredNewDomains = array_filter($newDomains, function ($domain) use ($existingDomains) { ], 422);
// return ! in_array($domain, $existingDomains); }
// });
// $mergedDomains = array_unique(array_merge($existingDomains, $filteredNewDomains)); if ($request->has('domains')) {
// $application->fqdn = implode(',', $mergedDomains); $fqdn = $request->domains;
// $application->custom_labels = base64_encode(implode("\n ", generateLabelsApplication($application))); $fqdn = str($fqdn)->replaceEnd(',', '')->trim();
// $application->save(); $fqdn = str($fqdn)->replaceStart(',', '')->trim();
// } $errors = [];
$fqdn = str($fqdn)->trim()->explode(',')->map(function ($domain) use (&$errors) {
if (filter_var($domain, FILTER_VALIDATE_URL) === false) {
$errors[] = 'Invalid domain: '.$domain;
}
return str($domain)->trim()->lower();
});
if (count($errors) > 0) {
return response()->json([
'message' => 'Validation failed',
'errors' => $errors,
], 422);
}
$fqdn = $fqdn->unique()->implode(',');
$application->fqdn = $fqdn;
$customLabels = str(implode('|coolify|', generateLabelsApplication($application)))->replace('|coolify|', "\n");
$application->custom_labels = base64_encode($customLabels);
$request->offsetUnset('domains');
}
$application->fill($request->all());
$application->save();
return response()->json([ return response()->json([
'message' => 'Application updated successfully.', 'message' => 'Application updated successfully.',
'application' => serialize_api_response($application), 'application' => $application,
]); ]);
} }