Esempio n. 1
0
 public function insert($data)
 {
     $hydrator = new ObjectProperty();
     //Hidrate StdClass to array
     $data->user_id = $this->usersRepository->getAuthenticated()->getId();
     $data->created_at = (new \DateTime())->format('Y-m-d');
     $data->total = 0;
     $items = $data->item;
     unset($data->item);
     $orderData = $hydrator->extract($data);
     $tableGateway = $this->repository->getTableGateway();
     try {
         $tableGateway->getAdapter()->getDriver()->getConnection()->beginTransaction();
         $order_id = $this->repository->insert($orderData);
         foreach ($items as $key => $item) {
             $product = $this->productsRepository->find($item['product_id']);
             $item['order_id'] = $order_id;
             $item['price'] = $product->getPrice();
             $item['total'] = $items[$key]['total'] = $item['quantity'] * $item['price'];
             $total += $item['total'];
             $this->repository->insertItem($item);
         }
         $this->repository->update(['total' => $total], $order_id);
         $tableGateway->getAdapter()->getDriver()->getConnection()->commit();
         return ['order_id' => $order_id];
     } catch (\Exception $e) {
         $tableGateway->getAdapter()->getDriver()->getConnection()->rollback();
         return 'error';
     }
 }
 public function insert($data)
 {
     $data->user_id = $this->userRepository->getAuthenticated()->getId();
     $data->created_at = (new \DateTime())->format('Y-m-d');
     $data->total = 0;
     $items = $data->item;
     unset($data->item);
     $hydrator = new ObjectProperty();
     $orderData = $hydrator->extract($data);
     try {
         $this->repository->beginTransaction();
         $orderId = $this->repository->insert($orderData);
         $total = 0;
         foreach ($items as $key => $item) {
             $product = $this->productRepository->find($item["product_id"]);
             $item['order_id'] = $orderId;
             $item['price'] = $product->getPrice();
             $item['total'] = $items[$key]["total"] = $item['quantity'] * $product->getPrice();
             $total += $item['total'];
             $this->repository->insertItem($item);
         }
         $this->repository->update(['total' => $total], $orderId);
         $this->repository->commitTransaction();
     } catch (Exception $e) {
         error_log($e->getCode());
         error_log($e->getFile());
         error_log($e->getLine());
         error_log($e->getMessage());
         $this->repository->rollbackTransaction();
         return false;
     }
     return $orderId;
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     $user = $this->userRepository->findByUsername($this->getIdentity()->getRoleId());
     if ($user->getRole() != "admin") {
         return new ApiProblem("403", "The user has not access to this info.");
     }
     return $this->repository->update($id, (array) $data);
 }
Esempio n. 4
0
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     try {
         $this->authService->hasRole('admin');
         return $this->productsRepository->update($id, $data);
     } catch (\Exception $e) {
         return new ApiProblem($e->getCode(), $e->getMessage());
     }
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     $usuarioLogado = $this->getUsuarioLogado();
     if ($usuarioLogado->getRole() === 'admin') {
         return $this->repository->update($id, $data);
     } else {
         return new ApiProblem(403, "Apenas usuários 'admin' podem editar os produtos");
     }
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     $userRepository = $this->repository->getUsersRepository();
     $user = $userRepository->findByUsername($this->getIdentity()->getRoleId());
     if ($user->getRole() == "admin") {
         return $this->repository->update($id, $data);
     }
     return new ApiProblem(403, 'Sem autorização para Editar');
 }
 public function findByName($name)
 {
     return $this->productRepository->findByName($name);
 }
 /**
  * Update a resource
  *
  * @param  mixed $id
  * @param  mixed $data
  * @return ApiProblem|mixed
  */
 public function update($id, $data)
 {
     return $this->repository->update($id, $data);
     //return new ApiProblem(405, 'The PUT method has not been defined for individual resources');
 }