Last active
April 4, 2017 18:51
-
-
Save ronsuez/eaeeeff54d78b297d4233e01b7eda626 to your computer and use it in GitHub Desktop.
php-code-refactoring-challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function post_confirm(Request $request) { | |
//validate the requrest params | |
$this->validate($request, [ | |
'service_id' => 'required|integer', | |
'driver_id' => 'required|integer', | |
]); | |
//take only the specified values from the request | |
$input = $request->only('service_id', 'driver_id'); | |
$service_id = $input['service_id']; | |
$driver_id = $input['driver_id']; | |
//try to find a driver by a given driver_id or fails if the driver does not exists | |
try { | |
$driver = Driver::findOrFail($driver_id); | |
} catch (ModelNotFoundException $e) { | |
return Response::json(['err' => '4']); | |
} | |
//try to find a service by a given service_id or fails if the service does not exists | |
try { | |
$servicio = Service::findOrFail($service_id); | |
} catch(ModelNotFoundException $e) { | |
return Response::json(['err' => '3']); | |
} | |
//check if a service has a driver assigned | |
if($servicio->driver_id && $servicio->status_id != '1') { | |
return Response::json(['err' => '1']); | |
} | |
if($servicio->user->uuid == ' ') { | |
return Response::json(['err' => '0']); | |
} | |
if($servicio->status_id == '6') { | |
return Response::json(['err' => '2']); | |
} | |
//assign driver to the given service | |
$servicio->driver_id = $driver_id; | |
$servicio->car_id = $driver->car_id; | |
$servicio->status_id = '2'; | |
$servicio->save(); | |
//update driver availabilty | |
$driver->available = 0; | |
$driver->save(); | |
//send notification to user based on type | |
$push = Push::make(); | |
$pushMessage = 'Tu servicio ha sido confirmado!'; | |
if($servicio->user->type == '1') { | |
$result = $push->ios($servicio->user->uuid, $pushMessage, '1', 'honk.wav', 'Open', ['serviceId' => $servicio->id]); | |
} else { | |
$result = $push->android2($servicio->user->uuid, $pushMessage, '1', 'default', 'Open', ['serviceId' => $servicio->id]); | |
} | |
return Response::json(['err' => '0']); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function post_confirm() { | |
$id = Input::get('service_id'); | |
$servicio = Service::find($id); | |
if($servicio != NULL) { | |
if ($servicio->status_id == '6') { | |
return Response::json(array('error' => '2')); | |
} | |
if ($servicio->driver_id == NULL && $servicio->status_id == '1') { | |
$servicio = Service::update($id, array( | |
'driver_id' => Input::get('driver_id'), | |
'status_id' => '2', | |
)); | |
Driver::update(Input::get('driver_id'), array('available' => '0')); | |
$driverTmp = Driver::find(Input::get('driver_id')); | |
Service::update($id, array( | |
'car_id' => $driverTmp->car_id | |
)); | |
$pushMessage = 'Tu servicio ha sido confirmado'; | |
$servicio = Service::find($id); | |
$push = Push::make(); | |
if($servicio->user->uuid == '1') { | |
return Response::json(array('error' => '0')); | |
} | |
if($servicio->user->type == '1') { | |
$result = $push->ios($servicio->user->uuid, $pushMessage, '1', 'honk.wav', 'Open', ['serviceId' => $servicio->id]); | |
} else { | |
$result = $push->android2($servicio->user->uuid, $pushMessage, '1', 'default', 'Open', ['serviceId' => $servicio->id]); | |
} | |
return Response::json(array('error' => '0')); | |
} else { | |
return Response::json(array('error' => '1')); | |
} | |
} else { | |
return Response::json(array('error' => '3')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment