-
Notifications
You must be signed in to change notification settings - Fork 1
/
ActorsController.php
164 lines (154 loc) · 5.64 KB
/
ActorsController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace App\Controller;
use Exception;
use SwaggerBake\Lib\Attribute\OpenApiOperation;
use SwaggerBake\Lib\Attribute\OpenApiPaginator;
use SwaggerBake\Lib\Attribute\OpenApiResponse;
/**
* Actors Controller
*
* @property \App\Model\Table\ActorsTable $Actors
* @method \App\Model\Entity\Actor[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class ActorsController extends AppController
{
/**
* List Actors
*
* This example uses the `OpenApiPaginator` attribute to automatically add in page, limit, sort, and direction
*
* @return \Cake\Http\Response|null|void Renders view
* @throws \App\Exception\MyCustomException MyCustomException - This is a custom exception
*/
#[OpenApiPaginator]
#[OpenApiOperation(
externalDocs: [
'url' => 'https://github.com/cnizzardini/cakephp-swagger-bake',
'description' => 'Check out the documentation'
]
)]
public function index()
{
$this->request->allowMethod('get');
$actors = $this->paginate($this->Actors);
$this->set(compact('actors'));
$this->viewBuilder()->setOption('serialize', 'actors');
}
/**
* View Actor
*
* Barebones example, this was all built direct from your cake models and routes. Additional information is
* parsed from docblocks using "@see" for links and "@throws" for HTTP 40x and 50x responses. The path parameter
* was automatically added and these comments come direct from your DocBlock comments.
*
* @see https://github.com/cnizzardini/cakephp-swagger-bake#doc-blocks SwaggerBake parses your DocBlocks!
* @param string|null $id Actor id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$this->request->allowMethod('get');
$actor = $this->Actors->get($id, [
'contain' => [],
]);
$this->set('actor', $actor);
$this->viewBuilder()->setOption('serialize', 'actor');
}
/**
* Add Actor
*
* Your OpenApi request bodies are built automatically straight from your Cake Models and Routes.
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
* @throws \Cake\Http\Exception\MethodNotAllowedException Method Not Allowed, use HTTP POST
* @throws \Exception
*/
public function add()
{
$this->request->allowMethod('post');
$actor = $this->Actors->patchEntity(
$this->Actors->newEmptyEntity(),
$this->request->getData()
);
if ($this->Actors->save($actor)) {
$this->set(compact('actor'));
$this->viewBuilder()->setOption('serialize', 'actor');
return;
}
throw new Exception('Unable to save');
}
/**
* Edit Actor
*
* The path parameter was automatically added and these comments come direct from your DocBlock comments.
*
* @param string|null $id Actor id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException
* @throws \Exception
*/
public function edit($id = null)
{
$this->request->allowMethod(['patch', 'post', 'put']);
$actor = $this->Actors->get($id, [
'contain' => [],
]);
$actor = $this->Actors->patchEntity($actor, $this->request->getData());
if ($this->Actors->save($actor)) {
$this->set(compact('actor'));
$this->viewBuilder()->setOption('serialize', 'actor');
return;
}
throw new Exception('Unable to save');
}
/**
* Delete Actor
*
* Deletes are disabled in this example
*
* @param string|null $id Actor id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException
* @throws \Exception
*/
public function delete($id = null)
{
$this->request->allowMethod(['delete']);
$response = $this->response->withHeader('_demo_mode_', 'DELETES ARE DISABLED IN DEMO MODE');
return $response->withStatus(422);
/*
$actor = $this->Actors->get($id);
if ($this->Actors->delete($actor)) {
return $this->response->withStatus(202);
}
new Exception('Unable to delete');
*/
}
/**
* Actor Films
*
* Example of using OpenApiResponse to show sample of associations.
*
* @see https://github.com/cnizzardini/cakephp-swagger-bake/blob/master/docs/attributes.md#OpenApiResponse Read
* more about OpenApiResponse associations.
* @param string|null $id Actor id.
* @return \Cake\Http\Response|null|void Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException
* @throws \Exception
*/
#[OpenApiResponse(associations: ['whiteList' => ['Films.Languages']])]
public function films($id)
{
$this->request->allowMethod('get');
$actor = $this->Actors->get($id, [
'contain' => ['Films' => ['Languages']],
]);
$this->set('films', $actor->films);
$this->viewBuilder()->setOption('serialize', 'films');
}
}