mirror of https://github.com/realodix/urlhub.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.1 KiB
44 lines
1.1 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreUrl;
|
|
use App\Models\Url;
|
|
use Illuminate\Http\Response;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class UrlController extends Controller
|
|
{
|
|
/**
|
|
* UrlController constructor.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('urlhublinkchecker')->only('create');
|
|
}
|
|
|
|
/**
|
|
* Store the data the user sent to create the Short URL.
|
|
*
|
|
* @param StoreUrl $request \App\Http\Requests\StoreUrl
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
|
|
*/
|
|
public function store(StoreUrl $request)
|
|
{
|
|
$v = Validator::make($request->all(), (new StoreUrl)->rules());
|
|
if ($v->fails()) {
|
|
return response()->json(['errors' => $v->errors()->all()]);
|
|
}
|
|
|
|
$url = (new Url)->shortenUrl($request, Auth::id());
|
|
|
|
return response([
|
|
'id' => $url->id,
|
|
'long_url' => $url->long_url,
|
|
'short_url' => url($url->keyword),
|
|
], Response::HTTP_CREATED);
|
|
}
|
|
}
|