-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSqlUpdate.php
More file actions
78 lines (66 loc) · 2.57 KB
/
SqlUpdate.php
File metadata and controls
78 lines (66 loc) · 2.57 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
<?php
/*
* Fusio - Self-Hosted API Management for Builders.
* For the current version and information visit <https://www.fusio-project.org/>
*
* Copyright (c) Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Fusio\Adapter\Sql\Action;
use Fusio\Engine\ContextInterface;
use Fusio\Engine\ParametersInterface;
use Fusio\Engine\RequestInterface;
use PSX\Http\Environment\HttpResponseInterface;
use PSX\Http\Exception as StatusCode;
use PSX\Record\Record;
/**
* Action which allows you to create an API endpoint based on any database
* table
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://www.fusio-project.org/
*/
class SqlUpdate extends SqlManipulationAbstract
{
public function getName(): string
{
return 'SQL-Update';
}
public function handle(RequestInterface $request, ParametersInterface $configuration, ContextInterface $context): HttpResponseInterface
{
$connection = $this->getConnection($configuration);
$tableName = $this->getTableName($configuration);
$mapping = $this->getMapping($configuration);
$table = $this->getTable($connection, $tableName);
$key = $this->getPrimaryKey($table);
$body = Record::from($request->getPayload());
$data = $this->getData($body, $connection, $table, false, $mapping);
$existingId = $this->findExistingId($connection, $key, $table, $request);
$connection->beginTransaction();
try {
$connection->update($table->getName(), $data, [$key => $existingId]);
$this->insertRelations($connection, $existingId, $body, $mapping);
$connection->commit();
} catch (\Exception $e) {
$connection->rollBack();
throw $e;
}
return $this->response->build(200, [], [
'success' => true,
'message' => 'Entry successfully updated',
'id' => (string) $existingId,
]);
}
}