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)) { |
|
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
107 | |||
108 | $this->db->commit(); |
||
109 | return $this->response($this->model->toArray()); |
||
110 | } else { |
||
111 | $this->db->rollback(); |
||
112 | throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Update a User Info |
||
118 | * |
||
119 | * @method PUT |
||
120 | * @url /v1/company/{id} |
||
121 | * |
||
122 | * @return Response |
||
123 | */ |
||
124 | 1 | public function edit($id) : Response |
|
125 | { |
||
126 | 1 | $company = $this->model->findFirst([ |
|
127 | 1 | 'id = ?0 AND is_deleted = 0 and users_id = ?1', |
|
128 | 1 | 'bind' => [$id, $this->userData->getId()], |
|
129 | ]); |
||
130 | |||
131 | 1 | if ($company) { |
|
132 | 1 | $request = $this->request->getPut(); |
|
133 | |||
134 | 1 | if (empty($request)) { |
|
135 | $request = $this->request->getJsonRawBody(true); |
||
136 | } |
||
137 | |||
138 | //update |
||
139 | 1 | if ($company->update($request, $this->updateFields)) { |
|
140 | 1 | return $this->response($company); |
|
141 | } else { |
||
142 | //didnt work |
||
143 | throw new UnprocessableEntityHttpException((string) current($company->getMessages())); |
||
144 | } |
||
145 | } else { |
||
146 | throw new UnprocessableEntityHttpException('Record not found'); |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 |