81 lines
2.1 KiB
PHP
81 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\UrlCollection;
|
|
use App\Http\Resources\UrlHistoryCollection;
|
|
use App\Http\Resources\UrlResource;
|
|
use App\Http\Resources\UrlStatsCollection;
|
|
use App\Models\Url;
|
|
use DateTime;
|
|
use Illuminate\Http\Request;
|
|
|
|
class UrlController extends Controller
|
|
{
|
|
/**
|
|
* Returns a listing of the urls.
|
|
*
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function index()
|
|
{
|
|
return new UrlCollection(Url::paginate(25));
|
|
}
|
|
|
|
/**
|
|
* Returns the specified url.
|
|
*
|
|
* @param Url $url
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function show(Url $url)
|
|
{
|
|
return new UrlResource($url);
|
|
}
|
|
|
|
/**
|
|
* Returns the history for a given url.
|
|
*
|
|
* @param Url $url
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function getHistory(Url $url, Request $request)
|
|
{
|
|
// Set start date with request or now - 1 year.
|
|
if ($request->input('start')) {
|
|
$start = $request->input('start');
|
|
$start = DateTime::createFromFormat('dmY', $start);
|
|
} else {
|
|
$start = now()->modify('-1 year');
|
|
}
|
|
// Set end date with request or now.
|
|
if ($request->input('end')) {
|
|
$end = $request->input('end');
|
|
$end = DateTime::createFromFormat('dmY', $end)->modify('+1 day');
|
|
} else {
|
|
$end = now();
|
|
}
|
|
// Check if the difference bewteen end and start is < than 1 year.
|
|
if ($start->diff($end)->days > 366) {
|
|
return response('Date interval too long', 403);
|
|
};
|
|
$history = $url->history()
|
|
->where('start_date', '>', $start)
|
|
->where('end_date', '<', $end)
|
|
->get();
|
|
return new UrlHistoryCollection($history);
|
|
}
|
|
|
|
/**
|
|
* Returns the stats for the given url.
|
|
*
|
|
* @param Url $url
|
|
* @return \Illuminate\Http\Response
|
|
*/
|
|
public function getstats(Url $url)
|
|
{
|
|
return new UrlStatsCollection($url->stats);
|
|
}
|
|
}
|