From 87487b023232e511b98ccbcde34de2f31f0da538 Mon Sep 17 00:00:00 2001 From: nkamuo Date: Thu, 14 Sep 2023 00:26:50 +0100 Subject: [PATCH] Update Address and Product --- .../Addressing/Input/AddressUpdateInput.php | 8 ++ .../Resolver/ClientAddressResolver.php | 64 ++++++++++++-- .../Catalog/Input/ProductUpdateInput.php | 8 ++ .../Resolver/ClientProductResolver.php | 88 ++++++++++++++----- 4 files changed, 142 insertions(+), 26 deletions(-) create mode 100644 src/GraphQL/Addressing/Input/AddressUpdateInput.php create mode 100644 src/GraphQL/Catalog/Input/ProductUpdateInput.php diff --git a/src/GraphQL/Addressing/Input/AddressUpdateInput.php b/src/GraphQL/Addressing/Input/AddressUpdateInput.php new file mode 100644 index 0000000..25e73bc --- /dev/null +++ b/src/GraphQL/Addressing/Input/AddressUpdateInput.php @@ -0,0 +1,8 @@ +security->isGranted('view', $address)) { - throw new UserError( - message: "Permision Denied: You may not view this resource" - ); - } + // if (!$this->security->isGranted('view', $address)) { + // throw new UserError( + // message: "Permision Denied: You may not view this resource" + // ); + // } @@ -128,4 +129,57 @@ public function createNewAddress(AddressCreationInput $input): Address return $address; } + + + + + + #[GQL\Mutation()] + #[GQL\Arg( + name: 'id', + type: 'Ulid!' + )] + #[GQL\Arg( + name: 'input', + type: 'AddressUpdateInput!' + )] + public function updateAddress(Ulid $id, AddressUpdateInput $input): Address + { + $address = $this->getAddressById($id); + $user = $this->getUser(); + + $input->build($address); + + $this->entityManager->persist($address); + $this->entityManager->flush(); + + return $address; + } + + + + private function getUser(): User + { + $user = $this->security->getUser(); + if (!($user instanceof User)) { + throw new UserError("Permission Denied: You may not perform this operation"); + } + return $user; + } + + + private function getAddressById(Ulid $id): Address + { + $user = $this->getUser(); + $address = $this->addressRepository->find($id); + if ($address === null) { + throw new UserError( + message: "Cannot find address with [id:$id]" + ); + } + if ($address->getOwner() !== $user) { + throw new UserError("Permission Denied: You may not perform this operation"); + } + return $address; + } } diff --git a/src/GraphQL/Catalog/Input/ProductUpdateInput.php b/src/GraphQL/Catalog/Input/ProductUpdateInput.php new file mode 100644 index 0000000..26b85ba --- /dev/null +++ b/src/GraphQL/Catalog/Input/ProductUpdateInput.php @@ -0,0 +1,8 @@ +productRepository->find($id); - if ($product === null) { - throw new UserError( - message: "Cannot find product with [id:$id]" - ); - } + $product = $this->getProductById($id); - if (!$this->security->isGranted('view', $product)) { - throw new UserError( - message: "Permision Denied: You may not view this resource" - ); - } + // if (!$this->security->isGranted('view', $product)) { + // throw new UserError( + // message: "Permision Denied: You may not view this resource" + // ); + // } @@ -73,11 +72,8 @@ public function getProductConnection( ?String $sort, ): ProductConnection { - $user = $this->security->getUser(); - if (!($user instanceof User)) { - throw new UserError("Permission Denied: You may not perform this operation"); - } - + $user = $this->getUser(); + $cb = new ConnectionBuilder( null, fn ($edges, PageInfoInterface $pageInfo) => new ProductConnection($edges, $pageInfo), @@ -107,13 +103,14 @@ public function getProductConnection( } - + #[GQL\Mutation()] - public function createNewProduct(ProductCreationInput $input): Product{ - + public function createNewProduct(ProductCreationInput $input): Product + { + $user = $this->security->getUser(); - if(!($user instanceof User)){ + if (!($user instanceof User)) { throw new UserError("Permission Denied: You may not perform this operation"); } @@ -126,4 +123,53 @@ public function createNewProduct(ProductCreationInput $input): Product{ return $product; } + + + + #[GQL\Mutation()] + #[GQL\Arg( + name: 'id', + type: 'Ulid!' + )] + #[GQL\Arg( + name: 'input', + type: 'ProductUpdateInput!' + )] + public function updateProduct(Ulid $id, ProductUpdateInput $input): Product + { + $product = $this->getProductById($id); + $input->build($product); + + $this->entityManager->persist($product); + $this->entityManager->flush(); + + return $product; + } + + + + + + private function getUser(): User{ + $user = $this->security->getUser(); + if (!($user instanceof User)) { + throw new UserError("Permission Denied: You may not perform this operation"); + } + return $user; + } + + + private function getProductById(Ulid $id): Product{ + $user = $this->getUser(); + $product = $this->productRepository->find($id); + if ($product === null) { + throw new UserError( + message: "Cannot find product with [id:$id]" + ); + } + if($product->getOwner() !== $user){ + throw new UserError("Permission Denied: You may not perform this operation"); + } + return $product; + } }