Skip to content

Instantly share code, notes, and snippets.

@er361
Created September 26, 2019 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save er361/a280cd7b6b8f75abe0bdc819167d1049 to your computer and use it in GitHub Desktop.
Save er361/a280cd7b6b8f75abe0bdc819167d1049 to your computer and use it in GitHub Desktop.
Бэк-енд приложения попутка
class ClientController extends Controller
{
public function getClients(Request $request, $type)
{
$ids = json_decode($request->post('ids'));
$delete = ClientModel::where(['status' => false, 'direction' => $type])
->whereIn('id', $ids)
->pluck('id')->toArray();
$new = ClientModel::where(['status' => true, 'direction' => $type])
->whereNotIn('id', $ids)
->get();
if (count($delete) || count($new))
return ['status' => true, 'delete' => $delete, 'new' => $new];
else
return ['status' => false];
}
public function changeType($type)
{
$models = ClientModel::where(['status' => true, 'direction' => $type])
->get();
if ($models->count() > 0)
return ['status' => true, 'clients' => $models];
return ['status' => false];
}
public function getDrivers()
{
$driverIds = DriverModel::where(['status' => true])
->pluck('id')
->toArray();
if (count($driverIds))
return ['status' => true, 'driver_ids' => $driverIds];
}
public function addCounter(Request $request,$call, $wtsapp)
{
$type = $request->post("type");
$init_id = $request->post("client_phone");
$target_id = $request->post("target_id");
$counterModel = new CounterModel();
$counterModel->type = $type;
$counterModel->driver_phone = $init_id;
$counterModel->target_id = $target_id;
if ($call == 1)
$counterModel->call += 1;
if ($wtsapp == 1)
$counterModel->wtsapp += 1;
$counterModel->save();
}
public function saveDest(Request $request)
{
$modelJson = $request->post("model");
$model = json_decode($modelJson, true);
$clientModel = new ClientModel();
$clientModel->fill($model);
$clientModel->dest = json_encode($model['dest']);
try {
$clientModel->saveOrFail();
} catch (\Throwable $e) {
return ['status' => false, 'message' => $e->getMessage()];
}
$modelDb = ClientModel::whereId($clientModel->id)->first();
return ['status' => true, 'model' => $modelDb];
}
public function hasPoint($id)
{
try {
ClientModel::where(['status' => true])->findOrFail($id);
return ['status' => true];
} catch (ModelNotFoundException $exception) {
return ['status' => false, 'message' => $exception->getMessage()];
}
}
public function cancel($id)
{
try{
$model = ClientModel::whereId($id)->firstOrFail();
$model->status = false;
$model->save();
return ['status' => true];
}catch (ModelNotFoundException $exception){
return ['status' => false, 'message' => $exception->getMessage()];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment