Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Domain/BookableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -1451,6 +1451,14 @@ public function GetRemovedAttributes()
return $this->_removedAttributeValues;
}

/**
* @return array|AttributeValue[]
*/
public function GetAttributeValues(): array
{
return [...$this->_attributeValues];
}

/**
* @param $customAttributeId
* @return mixed
Expand Down
21 changes: 18 additions & 3 deletions WebServices/Controllers/ResourceSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public function Create($request, $session)
);
$resourceId = $this->repository->Add($newResource);

$resource = $this->BuildResource($request, $resourceId);
$resource = $this->BuildResource(
request: $request,
resourceId: $resourceId
);
$this->repository->Update($resource);

return new ResourceControllerResult($resourceId, null);
Expand All @@ -69,7 +72,11 @@ public function Update($resourceId, $request, $session)
return new ResourceControllerResult(null, $errors);
}
$oldResource = $this->repository->LoadById($resourceId);
$resource = $this->BuildResource($request, $resourceId);
$resource = $this->BuildResource(
request: $request,
resourceId: $resourceId,
existingResource: $oldResource
);
$resource->SetImages($oldResource->GetImages());
$this->repository->Update($resource);

Expand All @@ -96,9 +103,10 @@ public function Delete($resourceId, $session)
/**
* @param ResourceRequest $request
* @param int $resourceId
* @param BookableResource|null $existingResource
* @return BookableResource
*/
private function BuildResource($request, $resourceId)
private function BuildResource(ResourceRequest $request, int $resourceId, ?BookableResource $existingResource = null): BookableResource
Copy link

Copilot AI Jul 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method signature change introduces inconsistency between Create and Update calls. In Create, existingResource will always be null, making the parameter unnecessary. Consider creating a separate method like BuildResourceFromExisting() for the Update case to maintain clearer API semantics.

Copilot uses AI. Check for mistakes.
{
$resource = new BookableResource(
$resourceId,
Expand All @@ -124,6 +132,13 @@ private function BuildResource($request, $resourceId)
);
$resource->SetSortOrder($request->sortOrder);

// Copy existing attributes before calling ChangeAttributes, so that we know what attributes have changed
if ($existingResource !== null) {
foreach ($existingResource->GetAttributeValues() as $attributeValue) {
$resource->WithAttribute($attributeValue);
}
}

$attributes = [];
foreach ($request->GetCustomAttributes() as $attribute) {
$attributes[] = new AttributeValue($attribute->attributeId, $attribute->attributeValue);
Expand Down