Advertisement
fahmihilmansyah

alamat.router.php

Jul 13th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. <?php
  2. // use lib\Logic;
  3. use Slim\Http\Request;
  4. use Slim\Http\Response;
  5. use Firebase\JWT\JWT;
  6. use Tuupola\Base62;
  7. use Rakit\Validation\Validator;
  8. use PDO\PDOException;
  9.  
  10. $app->get('/alamat/{nik}',  function ($request, $response, $args) use ($app)
  11. {          
  12.     try {
  13.  
  14.         $model  = new Model\AlamatDev();
  15.         $data   = $model->getAlamat(!empty($args['nik'])?$args['nik']:$this->jwt->user);
  16.        
  17.         if(!empty($data)){
  18.             $status = 'success';
  19.             $message = 'data ditemukan';       
  20.         }else{
  21.             $status = 'failed';
  22.             $message = 'data tidak ditemukan';
  23.         }
  24.  
  25.         $result = array('status' => $status, 'message' => $message, 'data' => $data);
  26.         return $response->withStatus(200)
  27.         ->withHeader("Content-Type", "application/json")
  28.         ->write(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  29.  
  30.     } catch (PDOException $e) {    
  31.         $app->response()->status(400);
  32.         $app->response()->header('X-Status-Reason', $e->getMessage());
  33.     }
  34.    
  35. });
  36.  
  37. $app->put('/alamat',  function ($request, $response, $args) use ($app)
  38. {          
  39.     try {
  40.         $validator = new Validator();
  41.  
  42.         $validation = $validator->validate(!empty($request->getParams()) ? $request->getParams() : [], [
  43.             'id'        => 'required',
  44.             'jalan'     => 'required',
  45.             'kota'      => 'required',
  46.             'propinsi'  => 'required',
  47.             'kodepos'   => 'required',
  48.             'telepon'   => 'required',
  49.             'tipe'      => 'required'
  50.         ]);
  51.  
  52.         if ($validation->fails()) {
  53.  
  54.             $result = array(
  55.                 'status' => 'failed' ,
  56.                 'message'=> $validation->errors()->firstOfAll()
  57.             );
  58.  
  59.         }else{
  60.             $model  = new Model\AlamatDev();
  61.             $result = $model->updateAlamat($request, $this->jwt->user);
  62.         }
  63.  
  64.         return $response->withStatus(200)
  65.         ->withHeader("Content-Type", "application/json")
  66.         ->write(json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  67.  
  68.     } catch (PDOException $e) {    
  69.         $app->response()->status(400);
  70.         $app->response()->header('X-Status-Reason', $e->getMessage());
  71.     }
  72.    
  73. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement