Merge pull request #181 from jonnybarnes/improving_media_endpoint_querying

Improving media endpoint querying
This commit is contained in:
Jonny Barnes 2020-06-28 16:46:47 +01:00 committed by GitHub
commit 7fcbfec105
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View file

@ -11,10 +11,12 @@ use App\Models\Media;
use App\Services\TokenService; use App\Services\TokenService;
use Exception; use Exception;
use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\File; use Illuminate\Http\File;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Intervention\Image\Exception\NotReadableException; use Intervention\Image\Exception\NotReadableException;
@ -53,7 +55,11 @@ class MicropubMediaController extends Controller
} }
if (request()->input('q') === 'last') { if (request()->input('q') === 'last') {
$media = Media::latest()->firstOrFail(); try {
$media = Media::latest()->whereDate('created_at', '>=', Carbon::now()->subMinutes(30))->firstOrFail();
} catch (ModelNotFoundException $exception) {
return response()->json([], 404);
}
return response()->json(['url' => $media->url]); return response()->json(['url' => $media->url]);
} }

View file

@ -3,6 +3,7 @@
namespace Tests\Feature; namespace Tests\Feature;
use App\Jobs\ProcessMedia; use App\Jobs\ProcessMedia;
use App\Models\Media;
use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Queue; use Illuminate\Support\Facades\Queue;
@ -15,6 +16,21 @@ class MicropubMediaTest extends TestCase
use DatabaseTransactions; use DatabaseTransactions;
use TestToken; use TestToken;
/** @test */
public function emptyResponseForLastUploadWhenNoneFound()
{
// Make sure theres no media
Media::all()->each(function ($media) {
$media->delete();
});
$response = $this->get(
'/api/media?q=last',
['HTTP_Authorization' => 'Bearer ' . $this->getToken()]
);
$response->assertStatus(404);
}
/** @test */ /** @test */
public function clientCanListLastUpload() public function clientCanListLastUpload()
{ {