Ejemplo n.º 1
0
 /**
  * POST /api/create-room
  */
 public function createRoom()
 {
     // Get all data send in post
     $subscribeFormData = Request::all();
     // Create rules for the validator
     $validator = Validator::make($subscribeFormData, ['user_name' => 'required', 'user_email' => 'required|email|unique:users,email', 'user_password' => 'required', 'partner_name' => 'required', 'partner_email' => 'required|email|unique:users,email', 'partner_password' => 'required']);
     // If validator fails return a 404 response
     if ($validator->fails()) {
         return Response::json(['status_code' => 404, 'errors' => $validator->errors()->all()], 404);
     }
     // If user use same email for user/partner ...
     if ($subscribeFormData['user_email'] == $subscribeFormData['partner_email']) {
         return Response::json(['status_code' => 404, 'errors' => ["Votre email doit être différent de celui de votre partenaire."]], 404);
     }
     // Create user
     $user = User::create(['name' => ucfirst($subscribeFormData['user_name']), 'email' => $subscribeFormData['user_email'], 'password' => Hash::make($subscribeFormData['user_password']), 'activation_token' => uniqid()]);
     // Create partner
     $partner = User::create(['name' => ucfirst($subscribeFormData['partner_name']), 'email' => $subscribeFormData['partner_email'], 'password' => Hash::make($subscribeFormData['partner_password']), 'is_partner' => true, 'activation_token' => uniqid()]);
     // Create room
     $room = Room::create(['token' => Room::generateToken(), 'user_id' => $user->id, 'partner_id' => $partner->id]);
     if ($user && $partner && $room) {
         // Add room ID
         User::where('id', '=', $user->id)->update(['room_id' => $room->id]);
         User::where('id', '=', $partner->id)->update(['room_id' => $room->id]);
         // Send email registration
         event(new UserHasRegistered(User::find($user->id), false));
         event(new UserHasRegistered(User::find($partner->id), true));
         // Return response
         return Response::json(['status_code' => 200, 'success' => "Merci de valider vos adresses email pour activer vos comptes."]);
     }
 }