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