Exemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, $mastori_id)
 {
     $mastori = Mastori::findOrFail($mastori_id);
     $data = $request->all();
     $validator = $this->validator($data);
     if ($validator->fails()) {
         return response(['errors' => $validator->messages()], 400);
     }
     // store
     $data['end_user_id'] = Auth::user()->userable->id;
     $data['editing_expires_at'] = date('Y-m-d H:i:s', strtotime('+5 minutes'));
     $rating = $mastori->ratings()->create($data);
     // Update mastori average rating
     $mastori['avg_rating'] = $mastori->ratings()->avg('rating');
     $mastori->save();
     // Reward user for rating
     $points = Config::get('services.points.review');
     Auth::user()->userable->addPoints($points, 'Review for mastori ' . $mastori_id);
     $rating['points_rewarded'] = $points;
     return response($rating, 201);
 }
Exemplo n.º 2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $mastori = Mastori::findOrFail($id);
     if ($mastori->id !== Auth::user()->userable->id) {
         return response('Unauthorised', 401);
     }
     $data = $request->all();
     $validator = $this->validator($data, $mastori->user->id);
     if ($validator->fails()) {
         return response(['errors' => $validator->messages()], 400);
     }
     $addresses = $data['addresses'];
     $professions = array_pluck($data['professions'], 'id');
     $areas = isset($data['areas']) ? array_pluck($data['areas'], 'id') : [];
     $data = array_except($data, ['password_repeat', 'addresses', 'professions']);
     $userdata = array_only($data, ['email', 'username', 'password']);
     $mastoridata = array_except($data, ['email', 'username', 'password']);
     $data = array_except($data, ['password_repeat', 'addresses', 'professsions']);
     // Encrypt password
     if (isset($userdata['password'])) {
         $userdata['password'] = bcrypt($userdata['password']);
     }
     // store
     $mastori->update($mastoridata);
     $mastori->user()->update($userdata);
     // Insert related addresses
     $mastori->user->addresses()->delete();
     foreach ($addresses as $address) {
         $mastori->user->addresses()->create($address);
     }
     // Sync professions
     $mastori->professions()->sync($professions);
     //update serving areas
     $mastori->areas()->sync($areas);
     return $mastori->load('professions');
 }