<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
class FaceBookLinkController extends Controller
{
protected $database;
public function __construct()
{
$this->database = app('firebase.database');
}
public function index(){
return view('admin.facebook_links.all');
} public function add(){
return view('admin.facebook_links.add');
}
public function create(){
}
public function store(Request $request)
{
$validateArray = [
'link' => 'required|url|regex:/http(?:s):\/\/(?:www\.)facebook\.com\/.+/i',
];
//validate
$validator = Validator::make($request->all(), $validateArray, []);
if ($validator->fails()) {
return error_api_processor('Some fields seems not correct!', 200, validation_error_processor($validator));
}
try {
$reference = $this->database->getReference('facebook_link');
$key = $reference->set([
$request->link
])->getKey();
return success_api_processor($key, 'Link created successfully.');
} catch (Exception $e) {
return error_api_processor('Something went wrong! Please try again later.', 200, $e);
}
}
/**
* @param $key
* @return JsonResponse
*/
public function remove($key): JsonResponse
{
$reference = $this->database->getReference('facebook_link');
$reference = $reference->getChild($key);
$reference->remove();
return success_api_processor($reference, 'Success');
}
}