Advertisement
Guest User

TraderApiController.php

a guest
Feb 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use App\Shop;
  6. use App\Trader;
  7. use App\Booking;
  8. use App\Item;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Validation\Validator;
  11.  
  12. class TraderApiController extends Controller
  13. {
  14.     //
  15.     //gKNo3a07Y4DqrRWwjZueqr2VdwoRnVJNQpqfDe13IdZmkRGT8VPDvEhiXLMpq5yg
  16.     public $token;
  17.     private $tader;
  18.     private $r;
  19.     public function __construct(Request $r){
  20.         $this->middleware('auth.api.trader');
  21.         $this->token = $r->token;
  22.         $this->trader = Trader::where('token',$this->token)->first();
  23.         $this->r = $r;
  24.     }
  25.  
  26.     public function getTrader(){
  27.         return $this->trader;
  28.     }
  29.  
  30.     public function getListOrder(){
  31.         return response()->json(['message' => 'OKE', 'status' => '200 OKE!', 'data' => Booking::where('shop_id', Trader::find($this->token)->id)->get()]);
  32.     }
  33.  
  34.     public function addItem(){
  35.         $validator = Validator::make($this->r->all(),[
  36.             'name' => 'required|max:100',
  37.         ]);
  38.  
  39.         if($validator->fails()){
  40.             $errors = $validator->errors();
  41.             return response()->json(['message' => 'OKE', 'status' => '200 OKE!', 'errors' => $errors], 200);
  42.         }
  43.  
  44.         $shop = Shop::where('trader_id', $this->trader->id)->first();
  45.         $item = new Item;
  46.         $item->shop_id      = $shop->id;
  47.         $item->name         = $this->r->name;
  48.         $item->photo        = strtolower(substr($this->r->name, 0,1)).".png";
  49.         $item->description  = $this->r->description;
  50.         $item->stock        = $this->r->stock;
  51.  
  52.         if ($item->save()){
  53.             $data = [
  54.                 'message' => 'item has been added'
  55.             ];
  56.             return response()->json(['message' => 'OKE', 'status' => '200 OKE!', 'data' => $data], 200);
  57.         }
  58.  
  59.         return response()->json(['message' => 'oppss error!! something went wrong', 'status' => '200 OKE!'], 200);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement