|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Api\Controllers; |
|
6
|
|
|
|
|
7
|
|
|
use Gewaer\Models\Companies; |
|
8
|
|
|
use Phalcon\Http\Response; |
|
9
|
|
|
use Gewaer\Exception\UnprocessableEntityHttpException; |
|
10
|
|
|
use Baka\Http\QueryParser; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CompaniesController |
|
14
|
|
|
* |
|
15
|
|
|
* @package Gewaer\Api\Controllers |
|
16
|
|
|
* |
|
17
|
|
|
* @property Users $userData |
|
18
|
|
|
* @property Request $request |
|
19
|
|
|
*/ |
|
20
|
|
|
class CompaniesController extends BaseController |
|
21
|
|
|
{ |
|
22
|
|
|
/* |
|
23
|
|
|
* fields we accept to create |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $createFields = ['name', 'profile_image', 'website', 'users_id']; |
|
28
|
|
|
|
|
29
|
|
|
/* |
|
30
|
|
|
* fields we accept to create |
|
31
|
|
|
* |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $updateFields = ['name', 'profile_image', 'website']; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* set objects |
|
38
|
|
|
* |
|
39
|
|
|
* @return void |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function onConstruct() |
|
42
|
|
|
{ |
|
43
|
5 |
|
$this->model = new Companies(); |
|
44
|
5 |
|
$this->model->users_id = $this->userData->getId(); |
|
45
|
|
|
|
|
46
|
5 |
|
$this->additionalSearchFields = [ |
|
47
|
5 |
|
['users_id', ':', $this->userData->getId()], |
|
48
|
|
|
]; |
|
49
|
5 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get Uer |
|
53
|
|
|
* |
|
54
|
|
|
* @param mixed $id |
|
55
|
|
|
* |
|
56
|
|
|
* @method GET |
|
57
|
|
|
* @url /v1/company/{id} |
|
58
|
|
|
* |
|
59
|
|
|
* @return Response |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getById($id) : Response |
|
62
|
|
|
{ |
|
63
|
|
|
//find the info |
|
64
|
1 |
|
$company = $this->model->findFirst([ |
|
65
|
1 |
|
'id = ?0 AND is_deleted = 0 and users_id = ?1', |
|
66
|
1 |
|
'bind' => [$id, $this->userData->getId()], |
|
67
|
|
|
]); |
|
68
|
|
|
|
|
69
|
|
|
//get relationship |
|
70
|
1 |
|
if ($this->request->hasQuery('relationships')) { |
|
71
|
|
|
$relationships = $this->request->getQuery('relationships', 'string'); |
|
72
|
|
|
|
|
73
|
|
|
$company = QueryParser::parseRelationShips($relationships, $company); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
if ($company) { |
|
77
|
1 |
|
return $this->response($company); |
|
78
|
|
|
} else { |
|
79
|
|
|
throw new UnprocessableEntityHttpException('Record not found'); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Add a new item |
|
85
|
|
|
* |
|
86
|
|
|
* @method POST |
|
87
|
|
|
* @url /v1/company |
|
88
|
|
|
* |
|
89
|
|
|
* @return Response |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function create() : Response |
|
92
|
|
|
{ |
|
93
|
1 |
|
$request = $this->request->getPost(); |
|
94
|
|
|
|
|
95
|
1 |
|
if (empty($request)) { |
|
96
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
//transaction |
|
100
|
1 |
|
$this->db->begin(); |
|
101
|
|
|
|
|
102
|
|
|
//alwasy overwrite userid |
|
103
|
1 |
|
$request['users_id'] = $this->userData->getId(); |
|
104
|
|
|
|
|
105
|
|
|
//try to save all the fields we allow |
|
106
|
1 |
|
if ($this->model->save($request, $this->createFields)) { |
|
107
|
1 |
|
$this->db->commit(); |
|
108
|
1 |
|
return $this->response($this->model->toArray()); |
|
109
|
|
|
} else { |
|
110
|
|
|
$this->db->rollback(); |
|
111
|
|
|
throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Update a User Info |
|
117
|
|
|
* |
|
118
|
|
|
* @method PUT |
|
119
|
|
|
* @url /v1/company/{id} |
|
120
|
|
|
* |
|
121
|
|
|
* @return Response |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function edit($id) : Response |
|
124
|
|
|
{ |
|
125
|
1 |
|
$company = $this->model->findFirst([ |
|
126
|
1 |
|
'id = ?0 AND is_deleted = 0 and users_id = ?1', |
|
127
|
1 |
|
'bind' => [$id, $this->userData->getId()], |
|
128
|
|
|
]); |
|
129
|
|
|
|
|
130
|
1 |
|
if ($company) { |
|
131
|
1 |
|
$request = $this->request->getPut(); |
|
132
|
|
|
|
|
133
|
1 |
|
if (empty($request)) { |
|
134
|
|
|
$request = $this->request->getJsonRawBody(true); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
//update |
|
138
|
1 |
|
if ($company->update($request, $this->updateFields)) { |
|
139
|
1 |
|
return $this->response($company); |
|
140
|
|
|
} else { |
|
141
|
|
|
//didnt work |
|
142
|
|
|
throw new UnprocessableEntityHttpException((string) current($company->getMessages())); |
|
143
|
|
|
} |
|
144
|
|
|
} else { |
|
145
|
|
|
throw new UnprocessableEntityHttpException('Record not found'); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|