bakaphp /
phalcon-api
| 1 | <?php |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 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 Exception; |
||
| 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', 'created_at', 'updated_at']; |
||
| 23 | |||
| 24 | /* |
||
| 25 | * fields we accept to create |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $updateFields = ['name', 'profile_image', 'website', 'users_id', 'created_at', 'updated_at']; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * set objects |
||
| 33 | * |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public function onConstruct() |
||
| 37 | {
|
||
| 38 | $this->model = new Companies(); |
||
| 39 | |||
| 40 | $this->additionalSearchFields = [ |
||
| 41 | ['users_id', ':', $this->userData->getId()], |
||
| 42 | ]; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get Uer |
||
| 47 | * |
||
| 48 | * @param mixed $id |
||
| 49 | * |
||
| 50 | * @method GET |
||
| 51 | * @url /v1/company/{id}
|
||
| 52 | * |
||
| 53 | * @return Phalcon\Http\Response |
||
| 54 | */ |
||
| 55 | public function getById($id) : Response |
||
| 56 | {
|
||
| 57 | //find the info |
||
| 58 | $company = $this->model->findFirst([ |
||
| 59 | 'id = ?0 AND is_deleted = 0 and users_id = ?1', |
||
| 60 | 'bind' => [$id, $this->userData->getId()], |
||
| 61 | ]); |
||
| 62 | |||
| 63 | //get relationship |
||
| 64 | if ($this->request->hasQuery('relationships')) {
|
||
| 65 | $relationships = $this->request->getQuery('relationships', 'string');
|
||
| 66 | |||
| 67 | $company = QueryParser::parseRelationShips($relationships, $company); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ($company) {
|
||
| 71 | return $this->response($company); |
||
| 72 | } else {
|
||
| 73 | throw new Exception('Record not found');
|
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Update a User Info |
||
| 79 | * |
||
| 80 | * @method PUT |
||
| 81 | * @url /v1/company/{id}
|
||
| 82 | * |
||
| 83 | * @return Phalcon\Http\Response |
||
| 84 | */ |
||
| 85 | public function edit($id) : Response |
||
| 86 | {
|
||
| 87 | $company = $this->model->findFirst([ |
||
| 88 | 'id = ?0 AND is_deleted = 0 and users_id = ?1', |
||
| 89 | 'bind' => [$id, $this->userData->getId()], |
||
| 90 | ]); |
||
| 91 | |||
| 92 | if ($company) {
|
||
| 93 | $request = $this->request->getPut(); |
||
| 94 | |||
| 95 | if (empty($request)) {
|
||
| 96 | $request = $this->request->getJsonRawBody(true); |
||
| 97 | } |
||
| 98 | |||
| 99 | //update |
||
| 100 | if ($company->update($request, $this->updateFields)) {
|
||
| 101 | return $this->response($company); |
||
| 102 | } else {
|
||
| 103 | //didnt work |
||
| 104 | throw new Exception(current($company->getMessages())); |
||
| 105 | } |
||
| 106 | } else {
|
||
| 107 | throw new Exception('Record not found');
|
||
| 108 | } |
||
| 109 | } |
||
| 110 | } |
||
| 111 |