-
Notifications
You must be signed in to change notification settings - Fork 1
/
CountriesController.php
129 lines (119 loc) · 4.32 KB
/
CountriesController.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
<?php
declare(strict_types=1);
namespace App\Controller;
use SwaggerBake\Lib\Attribute\OpenApiPaginator;
use SwaggerBake\Lib\Attribute\OpenApiRequestBody;
use SwaggerBake\Lib\Attribute\OpenApiResponse;
/**
* Countries Controller
*
* @property \App\Model\Table\CountriesTable $Countries
* @method \App\Model\Entity\Country[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
*/
class CountriesController extends AppController
{
/**
* Index method
*
* Use a custom OpenApi schema from swagger.yaml
*
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Http\Exception\MethodNotAllowedException When invalid method
*/
#[OpenApiPaginator]
#[OpenApiResponse(schemaType: "", ref: "#/components/schemas/Places")]
public function index()
{
$this->request->allowMethod('get');
$countries = $this->paginate($this->Countries);
$this->set(compact('countries'));
$this->viewBuilder()->setOption('serialize', 'countries');
}
/**
* View method
*
* Use a custom OpenApi schema from swagger.yaml
*
* @param string|null $id Country id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException When invalid method
*/
#[OpenApiResponse(ref: "#/components/schemas/Place")]
public function view($id = null)
{
$this->request->allowMethod('get');
$country = $this->Countries->get($id, [
'contain' => ['Cities'],
]);
$this->set('country', $country);
$this->viewBuilder()->setOption('serialize', 'country');
}
/**
* Add method
*
* Use a custom OpenApi schema from swagger.yaml
*
* @return \Cake\Http\Response|null|void HTTP 200 on successful add
* @throws \Cake\Http\Exception\MethodNotAllowedException When invalid method
* @throws \Exception
*/
#[OpenApiRequestBody(ref: "#/components/schemas/Place")]
#[OpenApiResponse(ref: "#/components/schemas/Place")]
public function add()
{
$this->request->allowMethod('post');
$country = $this->Countries->newEmptyEntity();
$country = $this->Countries->patchEntity($country, $this->request->getData());
if ($this->Countries->save($country)) {
$this->viewBuilder()->setOption('serialize', 'country');
return;
}
throw new \Exception("Record not created");
}
/**
* Edit method
*
* Use a custom OpenApi schema from swagger.yaml
*
* @param string|null $id Country id.
* @return \Cake\Http\Response|null|void HTTP 200 on successful edit
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException When invalid method
*/
#[OpenApiRequestBody(ref: "#/components/schemas/Place")]
#[OpenApiResponse(ref: "#/components/schemas/Place")]
public function edit($id = null)
{
$this->request->allowMethod(['patch', 'post', 'put']);
$country = $this->Countries->get($id, [
'contain' => [],
]);
$country = $this->Countries->patchEntity($country, $this->request->getData());
if ($this->Countries->save($country)) {
$this->viewBuilder()->setOption('serialize', 'country');
return;
}
throw new \Exception("Record not saved");
}
/**
* Delete method
*
* @param string|null $id Country id.
* @return \Cake\Http\Response|null|void HTTP 204 on success
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
* @throws \Cake\Http\Exception\MethodNotAllowedException When invalid method
* @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);
/* $country = $this->Countries->get($id);
if ($this->Countries->delete($country)) {
return $this->response->withStatus(204);
}
throw new \Exception("Record not deleted");*/
}
}