2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
2016-05-21 10:44:53 +02:00
|
|
|
|
2017-01-30 20:40:43 +01:00
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Http\Requests\CreateDocumentRequest;
|
|
|
|
use App\Http\Requests\DocumentRequest;
|
2016-05-21 10:44:53 +02:00
|
|
|
use App\Models\Document;
|
2016-05-30 12:44:49 +02:00
|
|
|
use App\Ninja\Repositories\DocumentRepository;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class DocumentAPIController.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-05-21 10:44:53 +02:00
|
|
|
class DocumentAPIController extends BaseAPIController
|
|
|
|
{
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var DocumentRepository
|
|
|
|
*/
|
2016-05-30 12:44:49 +02:00
|
|
|
protected $documentRepo;
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-05-30 12:44:49 +02:00
|
|
|
protected $entityType = ENTITY_DOCUMENT;
|
2016-05-21 10:44:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* DocumentAPIController constructor.
|
|
|
|
*
|
|
|
|
* @param DocumentRepository $documentRepo
|
|
|
|
*/
|
2016-05-30 12:44:49 +02:00
|
|
|
public function __construct(DocumentRepository $documentRepo)
|
2016-05-21 10:44:53 +02:00
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
2016-05-30 12:44:49 +02:00
|
|
|
$this->documentRepo = $documentRepo;
|
2016-05-21 10:44:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2016-12-29 17:17:17 +01:00
|
|
|
* @SWG\Get(
|
|
|
|
* path="/documents",
|
|
|
|
* summary="List of document",
|
|
|
|
* tags={"document"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A list with documents",
|
|
|
|
* @SWG\Schema(type="array", @SWG\Items(ref="#/definitions/Document"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-05-21 10:44:53 +02:00
|
|
|
public function index()
|
|
|
|
{
|
2016-06-05 12:15:49 +02:00
|
|
|
$documents = Document::scope();
|
2016-06-05 12:00:21 +02:00
|
|
|
|
2016-06-05 12:13:38 +02:00
|
|
|
return $this->listResponse($documents);
|
2016-05-21 10:44:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param DocumentRequest $request
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response|\Redirect|\Symfony\Component\HttpFoundation\StreamedResponse
|
|
|
|
*/
|
2016-05-30 12:44:49 +02:00
|
|
|
public function show(DocumentRequest $request)
|
2016-05-21 10:44:53 +02:00
|
|
|
{
|
2016-05-30 12:44:49 +02:00
|
|
|
$document = $request->entity();
|
2016-05-21 10:44:53 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (array_key_exists($document->type, Document::$types)) {
|
2016-08-03 15:02:42 +02:00
|
|
|
return DocumentController::getDownloadResponse($document);
|
2017-01-30 17:05:31 +01:00
|
|
|
} else {
|
2017-01-30 20:40:43 +01:00
|
|
|
return $this->errorResponse(['error' => 'Invalid mime type'], 400);
|
2017-01-30 17:05:31 +01:00
|
|
|
}
|
2016-08-03 15:03:14 +02:00
|
|
|
}
|
2016-05-21 10:44:53 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2016-12-29 17:17:17 +01:00
|
|
|
* @SWG\Post(
|
|
|
|
* path="/documents",
|
|
|
|
* tags={"document"},
|
|
|
|
* summary="Create a document",
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
|
|
|
* name="body",
|
|
|
|
* @SWG\Schema(ref="#/definitions/Document")
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="New document",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Document"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-05-30 12:44:49 +02:00
|
|
|
public function store(CreateDocumentRequest $request)
|
2016-05-21 10:44:53 +02:00
|
|
|
{
|
2016-06-01 11:39:42 +02:00
|
|
|
$document = $this->documentRepo->upload($request->all());
|
2016-05-30 12:44:49 +02:00
|
|
|
|
|
|
|
return $this->itemResponse($document);
|
2016-05-21 10:44:53 +02:00
|
|
|
}
|
|
|
|
}
|