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\DocumentRequest;
|
2017-03-08 17:03:35 +01:00
|
|
|
use App\Http\Requests\CreateDocumentRequest;
|
2017-03-09 11:11:45 +01:00
|
|
|
use App\Http\Requests\UpdateDocumentRequest;
|
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",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="List document",
|
|
|
|
* operationId="listDocuments",
|
2016-12-29 17:17:17 +01:00
|
|
|
* tags={"document"},
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
2017-03-08 17:03:35 +01:00
|
|
|
* description="A list of documents",
|
2016-12-29 17:17:17 +01:00
|
|
|
* @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
|
2017-03-08 17:03:35 +01:00
|
|
|
*
|
|
|
|
* @SWG\Get(
|
|
|
|
* path="/documents/{document_id}",
|
|
|
|
* summary="Download a document",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="getDocument",
|
|
|
|
* tags={"document"},
|
2017-03-08 17:03:35 +01:00
|
|
|
* produces={"application/octet-stream"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="document_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="A file",
|
|
|
|
* @SWG\Schema(type="file")
|
|
|
|
* ),
|
|
|
|
* @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 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",
|
|
|
|
* summary="Create a document",
|
2017-03-09 17:22:37 +01:00
|
|
|
* operationId="createDocument",
|
|
|
|
* tags={"document"},
|
2016-12-29 17:17:17 +01:00
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="body",
|
2017-03-08 17:03:35 +01:00
|
|
|
* name="document",
|
2016-12-29 17:17:17 +01:00
|
|
|
* @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
|
|
|
}
|
2017-03-08 17:03:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @SWG\Delete(
|
|
|
|
* path="/documents/{document_id}",
|
2017-03-09 17:22:37 +01:00
|
|
|
* summary="Delete a document",
|
|
|
|
* operationId="deleteDocument",
|
2017-03-08 17:03:35 +01:00
|
|
|
* tags={"document"},
|
|
|
|
* @SWG\Parameter(
|
|
|
|
* in="path",
|
|
|
|
* name="document_id",
|
|
|
|
* type="integer",
|
2017-03-09 11:11:45 +01:00
|
|
|
* required=true
|
2017-03-08 17:03:35 +01:00
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response=200,
|
|
|
|
* description="Deleted document",
|
|
|
|
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Document"))
|
|
|
|
* ),
|
|
|
|
* @SWG\Response(
|
|
|
|
* response="default",
|
|
|
|
* description="an ""unexpected"" error"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
2017-03-09 11:11:45 +01:00
|
|
|
public function destroy(UpdateDocumentRequest $request)
|
2017-03-08 17:03:35 +01:00
|
|
|
{
|
|
|
|
$entity = $request->entity();
|
|
|
|
|
|
|
|
$this->documentRepo->delete($entity);
|
|
|
|
|
|
|
|
return $this->itemResponse($entity);
|
|
|
|
}
|
2016-05-21 10:44:53 +02:00
|
|
|
}
|