refactoring

This commit is contained in:
Yuri Kuznetsov
2021-05-13 14:43:55 +03:00
parent db4663cbf9
commit f1306dfbee
5 changed files with 45 additions and 31 deletions
+33 -12
View File
@@ -36,6 +36,7 @@ use Espo\Core\Exceptions\{
use Espo\Core\{
Record\Collection as RecordCollection,
Api\Request,
Select\SearchParams,
};
use StdClass;
@@ -93,19 +94,9 @@ class Record extends RecordBase
}
if (!empty($data->massRelate)) {
if (!is_array($data->where)) {
throw new BadRequest();
}
$searchParams = $this->fetchMassLinkSearchParamsFromRequest($request);
$where = json_decode(json_encode($data->where), true);
$selectData = null;
if (isset($data->selectData) && is_array($data->selectData)) {
$selectData = json_decode(json_encode($data->selectData), true);
}
return $this->getRecordService()->massLink($id, $link, $where, $selectData);
return $this->getRecordService()->massLink($id, $link, $searchParams);
}
$foreignIdList = [];
@@ -191,4 +182,34 @@ class Record extends RecordBase
return true;
}
private function fetchMassLinkSearchParamsFromRequest(Request $request): SearchParams
{
$data = $request->getParsedBody();
$where = $data->where ?? null;
if ($where !== null) {
$where = json_decode(json_encode($where), true);
}
$params = json_decode(json_encode(
$data->searchParams ?? $data->selectData ?? (object) []
), true);
if ($where !== null && !is_array($where)) {
throw new BadRequest("Bad 'where.");
}
if ($where !== null) {
$params['where'] = array_merge(
$params['where'] ?? [],
$where
);
}
unset($params['select']);
return SearchParams::fromRaw($params);
}
}