-
Notifications
You must be signed in to change notification settings - Fork 323
fix(api): prevent duplicate custom attribute values when updating res… #685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
|
@@ -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); | ||
|
|
||
|
|
@@ -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 | ||
|
||
| { | ||
| $resource = new BookableResource( | ||
| $resourceId, | ||
|
|
@@ -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); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.