1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Http/Controllers/DocumentAPIController.php

162 lines
4.0 KiB
PHP
Raw Normal View History

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;
use App\Http\Requests\CreateDocumentRequest;
use App\Http\Requests\UpdateDocumentRequest;
2016-05-21 10:44:53 +02:00
use App\Models\Document;
use App\Ninja\Repositories\DocumentRepository;
/**
2017-01-30 20:40:43 +01:00
* Class DocumentAPIController.
*/
2016-05-21 10:44:53 +02:00
class DocumentAPIController extends BaseAPIController
{
/**
* @var DocumentRepository
*/
protected $documentRepo;
/**
* @var string
*/
protected $entityType = ENTITY_DOCUMENT;
2016-05-21 10:44:53 +02:00
/**
* DocumentAPIController constructor.
*
* @param DocumentRepository $documentRepo
*/
public function __construct(DocumentRepository $documentRepo)
2016-05-21 10:44:53 +02:00
{
parent::__construct();
$this->documentRepo = $documentRepo;
2016-05-21 10:44:53 +02:00
}
/**
2016-12-29 17:17:17 +01:00
* @SWG\Get(
* path="/documents",
* summary="List document",
* operationId="listDocuments",
2016-12-29 17:17:17 +01:00
* tags={"document"},
* @SWG\Response(
* response=200,
* 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-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
}
/**
* @param DocumentRequest $request
*
* @return \Illuminate\Http\Response|\Redirect|\Symfony\Component\HttpFoundation\StreamedResponse
*
* @SWG\Get(
* path="/documents/{document_id}",
* summary="Download a document",
* operationId="getDocument",
* tags={"document"},
* produces={"application/octet-stream"},
* @SWG\Parameter(
* in="path",
* name="document_id",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=200,
* description="A file",
* @SWG\Schema(type="file")
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function show(DocumentRequest $request)
2016-05-21 10:44:53 +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)) {
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-05-21 10:44:53 +02:00
/**
2016-12-29 17:17:17 +01:00
* @SWG\Post(
* path="/documents",
* summary="Create a document",
* operationId="createDocument",
* tags={"document"},
2016-12-29 17:17:17 +01:00
* @SWG\Parameter(
* in="body",
* 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"
* )
* )
*/
public function store(CreateDocumentRequest $request)
2016-05-21 10:44:53 +02:00
{
$document = $this->documentRepo->upload($request->all());
return $this->itemResponse($document);
2016-05-21 10:44:53 +02:00
}
/**
* @SWG\Delete(
* path="/documents/{document_id}",
* summary="Delete a document",
* operationId="deleteDocument",
* tags={"document"},
* @SWG\Parameter(
* in="path",
* name="document_id",
* type="integer",
* required=true
* ),
* @SWG\Response(
* response=200,
* description="Deleted document",
* @SWG\Schema(type="object", @SWG\Items(ref="#/definitions/Document"))
* ),
* @SWG\Response(
* response="default",
* description="an ""unexpected"" error"
* )
* )
*/
public function destroy(UpdateDocumentRequest $request)
{
$entity = $request->entity();
$this->documentRepo->delete($entity);
return $this->itemResponse($entity);
}
2016-05-21 10:44:53 +02:00
}