Skip to content

Commit

Permalink
Update Address and Product
Browse files Browse the repository at this point in the history
  • Loading branch information
nkamuo committed Sep 13, 2023
1 parent 49000de commit 87487b0
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 26 deletions.
8 changes: 8 additions & 0 deletions src/GraphQL/Addressing/Input/AddressUpdateInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace App\GraphQL\Addressing\Input;
use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Input()]
class AddressUpdateInput extends AddressInput{

}
64 changes: 59 additions & 5 deletions src/GraphQL/Addressing/Resolver/ClientAddressResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Entity\Addressing\Address;
use App\Entity\Addressing\UserAddress;
use App\GraphQL\Addressing\Input\AddressCreationInput;
use App\GraphQL\Addressing\Input\AddressUpdateInput;
use App\GraphQL\Addressing\Type\AddressConnection;
use App\GraphQL\Addressing\Type\AddressEdge;
use App\Repository\Addressing\AddressRepository;
Expand Down Expand Up @@ -56,11 +57,11 @@ public function getAddressItem(
);
}

if (!$this->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"
// );
// }



Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions src/GraphQL/Catalog/Input/ProductUpdateInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace App\GraphQL\Catalog\Input;
use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Input()]
class ProductUpdateInput extends ProductInput{

}
88 changes: 67 additions & 21 deletions src/GraphQL/Catalog/Resolver/ClientProductResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Entity\Catalog\Product;
use App\Entity\Catalog\UserProduct;
use App\GraphQL\Catalog\Input\ProductCreationInput;
use App\GraphQL\Catalog\Input\ProductUpdateInput;
use App\GraphQL\Catalog\Type\ProductConnection;
use App\GraphQL\Catalog\Type\ProductEdge;
use App\Repository\Catalog\ProductRepository;
Expand All @@ -24,7 +25,10 @@
use Symfony\Component\Uid\Ulid;


#[GQL\Provider()]
#[GQL\Provider(
targetQueryTypes: ['ClientQuery'],
targetMutationTypes: ['ClientMutation']
)]
class ClientProductResolver
{

Expand All @@ -46,18 +50,13 @@ public function getProductItem(
#[GQL\Arg(type: 'Ulid')] Ulid $id
): Product {

$product = $this->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"
// );
// }



Expand All @@ -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),
Expand Down Expand Up @@ -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");
}

Expand All @@ -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;
}
}

0 comments on commit 87487b0

Please sign in to comment.