mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 09:39:57 +00:00
Handle stock
userfields in generic entity interaction API endpoints (closes #2381)
This commit is contained in:
parent
dc05476d09
commit
8f52aaeadc
@ -60,4 +60,4 @@
|
|||||||
|
|
||||||
### API
|
### API
|
||||||
|
|
||||||
- xxx
|
- Optimized that the endpoints `GET /objects/{entity}` and `GET /objects/{entity}/{objectId}` now also return Userfields for the entity `stock`
|
||||||
|
@ -190,28 +190,31 @@ class GenericEntityApiController extends BaseApiController
|
|||||||
|
|
||||||
public function GetObject(Request $request, Response $response, array $args)
|
public function GetObject(Request $request, Response $response, array $args)
|
||||||
{
|
{
|
||||||
if ($this->IsValidExposedEntity($args['entity']) && !$this->IsEntityWithNoListing($args['entity']))
|
if (!$this->IsValidExposedEntity($args['entity']) || $this->IsEntityWithNoListing($args['entity']))
|
||||||
{
|
|
||||||
$userfields = $this->getUserfieldsService()->GetValues($args['entity'], $args['objectId']);
|
|
||||||
if (count($userfields) === 0)
|
|
||||||
{
|
|
||||||
$userfields = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
$object = $this->getDatabase()->{$args['entity']}($args['objectId']);
|
|
||||||
if ($object == null)
|
|
||||||
{
|
|
||||||
return $this->GenericErrorResponse($response, 'Object not found', 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$object['userfields'] = $userfields;
|
|
||||||
|
|
||||||
return $this->ApiResponse($response, $object);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
|
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$object = $this->getDatabase()->{$args['entity']}($args['objectId']);
|
||||||
|
if ($object == null)
|
||||||
|
{
|
||||||
|
return $this->GenericErrorResponse($response, 'Object not found', 404);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Handle this somehow more generically
|
||||||
|
$referencingId = $args['objectId'];
|
||||||
|
if ($args['entity'] == 'stock')
|
||||||
|
{
|
||||||
|
$referencingId = $object->stock_id;
|
||||||
|
}
|
||||||
|
$userfields = $this->getUserfieldsService()->GetValues($args['entity'], $referencingId);
|
||||||
|
if (count($userfields) === 0)
|
||||||
|
{
|
||||||
|
$userfields = null;
|
||||||
|
}
|
||||||
|
$object['userfields'] = $userfields;
|
||||||
|
|
||||||
|
return $this->ApiResponse($response, $object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function GetObjects(Request $request, Response $response, array $args)
|
public function GetObjects(Request $request, Response $response, array $args)
|
||||||
@ -222,8 +225,8 @@ class GenericEntityApiController extends BaseApiController
|
|||||||
}
|
}
|
||||||
|
|
||||||
$objects = $this->queryData($this->getDatabase()->{$args['entity']}(), $request->getQueryParams());
|
$objects = $this->queryData($this->getDatabase()->{$args['entity']}(), $request->getQueryParams());
|
||||||
$userfields = $this->getUserfieldsService()->GetFields($args['entity']);
|
|
||||||
|
|
||||||
|
$userfields = $this->getUserfieldsService()->GetFields($args['entity']);
|
||||||
if (count($userfields) > 0)
|
if (count($userfields) > 0)
|
||||||
{
|
{
|
||||||
$allUserfieldValues = $this->getUserfieldsService()->GetAllValues($args['entity']);
|
$allUserfieldValues = $this->getUserfieldsService()->GetAllValues($args['entity']);
|
||||||
@ -233,7 +236,14 @@ class GenericEntityApiController extends BaseApiController
|
|||||||
$userfieldKeyValuePairs = null;
|
$userfieldKeyValuePairs = null;
|
||||||
foreach ($userfields as $userfield)
|
foreach ($userfields as $userfield)
|
||||||
{
|
{
|
||||||
$value = FindObjectInArrayByPropertyValue(FindAllObjectsInArrayByPropertyValue($allUserfieldValues, 'object_id', $object->id), 'name', $userfield->name);
|
// TODO: Handle this somehow more generically
|
||||||
|
$userfieldReference = 'id';
|
||||||
|
if ($args['entity'] == 'stock')
|
||||||
|
{
|
||||||
|
$userfieldReference = 'stock_id';
|
||||||
|
}
|
||||||
|
|
||||||
|
$value = FindObjectInArrayByPropertyValue(FindAllObjectsInArrayByPropertyValue($allUserfieldValues, 'object_id', $object->{$userfieldReference}), 'name', $userfield->name);
|
||||||
if ($value)
|
if ($value)
|
||||||
{
|
{
|
||||||
$userfieldKeyValuePairs[$userfield->name] = $value->value;
|
$userfieldKeyValuePairs[$userfield->name] = $value->value;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user