| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Webhook; |
| 6 |
use FluentBoards\Framework\Http\Request\Request; |
| 7 |
|
| 8 |
|
| 9 |
class WebhookController extends Controller |
| 10 |
{ |
| 11 |
public function index(Request $request, Webhook $webhook) |
| 12 |
{ |
| 13 |
$fields = $webhook->getFields(); |
| 14 |
$search = $request->getSafe('search', ''); |
| 15 |
|
| 16 |
$webhooks = $webhook->latest()->get()->toArray(); |
| 17 |
|
| 18 |
if ( ! empty($search)) { |
| 19 |
$search = strtolower($search); |
| 20 |
$webhooks = array_map(function ($row) use ($search) { |
| 21 |
$name = strtolower($row['value']['name']); |
| 22 |
if ($row['value'] && str_contains($name, $search)) { |
| 23 |
return $row; |
| 24 |
} |
| 25 |
|
| 26 |
return null; |
| 27 |
}, $webhooks); |
| 28 |
} |
| 29 |
|
| 30 |
$rows = []; |
| 31 |
foreach ($webhooks as $row) { |
| 32 |
if ($row) { |
| 33 |
$rows[] = $row; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
$response = [ |
| 39 |
'webhooks' => $rows, |
| 40 |
'fields' => $fields['fields'] |
| 41 |
]; |
| 42 |
|
| 43 |
return $response; |
| 44 |
} |
| 45 |
|
| 46 |
public function create(Request $request, Webhook $webhook) |
| 47 |
{ |
| 48 |
$webhook = $webhook->store( |
| 49 |
$this->validate( |
| 50 |
$request->all(), |
| 51 |
['name' => 'required'] |
| 52 |
) |
| 53 |
); |
| 54 |
|
| 55 |
return [ |
| 56 |
'id' => $webhook->id, |
| 57 |
'webhook' => $webhook->value, |
| 58 |
'webhooks' => $webhook->latest()->get(), |
| 59 |
'message' => __('Successfully Created the WebHook', 'fluent-boards'), |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
public function update(Request $request, Webhook $webhook, $id) |
| 64 |
{ |
| 65 |
$webhook->find($id)->saveChanges($request->all()); |
| 66 |
|
| 67 |
return [ |
| 68 |
'webhooks' => $webhook->latest()->get(), |
| 69 |
'message' => __('Successfully updated the webhook', 'fluent-boards'), |
| 70 |
]; |
| 71 |
} |
| 72 |
|
| 73 |
public function delete(Webhook $webhook, $id) |
| 74 |
{ |
| 75 |
$webhook->where('id', $id)->delete(); |
| 76 |
|
| 77 |
return [ |
| 78 |
'webhooks' => $webhook->latest()->get(), |
| 79 |
'message' => __('Successfully deleted the webhook', 'fluent-boards'), |
| 80 |
]; |
| 81 |
} |
| 82 |
} |
| 83 |
|