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
|
||||
|
||||
- xxx
|
||||
- Optimized that the endpoints `GET /objects/{entity}` and `GET /objects/{entity}/{objectId}` now also return Userfields for the entity `stock`
|
||||
|
@ -190,12 +190,9 @@ class GenericEntityApiController extends BaseApiController
|
||||
|
||||
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;
|
||||
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
|
||||
}
|
||||
|
||||
$object = $this->getDatabase()->{$args['entity']}($args['objectId']);
|
||||
@ -204,15 +201,21 @@ class GenericEntityApiController extends BaseApiController
|
||||
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);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->GenericErrorResponse($response, 'Entity does not exist or is not exposed');
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
$userfields = $this->getUserfieldsService()->GetFields($args['entity']);
|
||||
|
||||
$userfields = $this->getUserfieldsService()->GetFields($args['entity']);
|
||||
if (count($userfields) > 0)
|
||||
{
|
||||
$allUserfieldValues = $this->getUserfieldsService()->GetAllValues($args['entity']);
|
||||
@ -233,7 +236,14 @@ class GenericEntityApiController extends BaseApiController
|
||||
$userfieldKeyValuePairs = null;
|
||||
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)
|
||||
{
|
||||
$userfieldKeyValuePairs[$userfield->name] = $value->value;
|
||||
|
Loading…
x
Reference in New Issue
Block a user