bakaphp /
phalcon-api
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Gewaer\Api\Controllers; |
||
| 6 | |||
| 7 | use Gewaer\Models\Companies; |
||
| 8 | use Gewaer\Models\CompaniesCustomFields; |
||
| 9 | use Phalcon\Http\Response; |
||
| 10 | use Gewaer\Exception\UnprocessableEntityHttpException; |
||
| 11 | use Baka\Http\QueryParser; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class CompaniesController |
||
| 15 | * |
||
| 16 | * @package Gewaer\Api\Controllers |
||
| 17 | * |
||
| 18 | * @property Users $userData |
||
| 19 | * @property Request $request |
||
| 20 | */ |
||
| 21 | class CompaniesController extends \Baka\Http\Rest\CrudCustomFieldsController |
||
| 22 | { |
||
| 23 | /* |
||
| 24 | * fields we accept to create |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | protected $createFields = ['name', 'profile_image', 'website', 'users_id', 'address', 'zip', 'email', 'language', 'timezone']; |
||
| 29 | |||
| 30 | /* |
||
| 31 | * fields we accept to create |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $updateFields = ['name', 'profile_image', 'website', 'address', 'zip', 'email', 'language', 'timezone']; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * set objects |
||
| 39 | * |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | 5 | public function onConstruct() |
|
| 43 | { |
||
| 44 | 5 | $this->model = new Companies(); |
|
| 45 | 5 | $this->customModel = new CompaniesCustomFields(); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 46 | |||
| 47 | 5 | $this->model->users_id = $this->userData->getId(); |
|
| 48 | |||
| 49 | 5 | $this->additionalSearchFields = [ |
|
| 50 | 5 | ['users_id', ':', $this->userData->getId()], |
|
| 51 | ]; |
||
| 52 | 5 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Get Uer |
||
| 56 | * |
||
| 57 | * @param mixed $id |
||
| 58 | * |
||
| 59 | * @method GET |
||
| 60 | * @url /v1/company/{id} |
||
| 61 | * |
||
| 62 | * @return Response |
||
| 63 | */ |
||
| 64 | 2 | public function getById($id) : Response |
|
| 65 | { |
||
| 66 | //find the info |
||
| 67 | 2 | $company = $this->model->findFirst([ |
|
| 68 | 2 | 'id = ?0 AND is_deleted = 0 and users_id = ?1', |
|
| 69 | 2 | 'bind' => [$id, $this->userData->getId()], |
|
| 70 | ]); |
||
| 71 | |||
| 72 | //get relationship |
||
| 73 | 2 | if ($this->request->hasQuery('relationships')) { |
|
| 74 | $relationships = $this->request->getQuery('relationships', 'string'); |
||
| 75 | |||
| 76 | $company = QueryParser::parseRelationShips($relationships, $company); |
||
| 77 | } |
||
| 78 | |||
| 79 | 2 | if ($company) { |
|
| 80 | 2 | return $this->response($company->toFullArray()); |
|
| 81 | } else { |
||
| 82 | throw new UnprocessableEntityHttpException('Record not found'); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Add a new item |
||
| 88 | * |
||
| 89 | * @method POST |
||
| 90 | * @url /v1/company |
||
| 91 | * |
||
| 92 | * @return Response |
||
| 93 | */ |
||
| 94 | 1 | public function create() : Response |
|
| 95 | { |
||
| 96 | 1 | $request = $this->request->getPost(); |
|
| 97 | |||
| 98 | 1 | if (empty($request)) { |
|
| 99 | $request = $this->request->getJsonRawBody(true); |
||
| 100 | } |
||
| 101 | |||
| 102 | //transaction |
||
| 103 | 1 | $this->db->begin(); |
|
| 104 | |||
| 105 | //alwasy overwrite userid |
||
| 106 | 1 | $request['users_id'] = $this->userData->getId(); |
|
| 107 | |||
| 108 | 1 | $this->model->setCustomFields($request); |
|
| 109 | //try to save all the fields we allow |
||
| 110 | 1 | if ($this->model->save($request, $this->createFields)) { |
|
| 111 | 1 | $this->db->commit(); |
|
| 112 | 1 | return $this->response($this->model->findFirst($this->model->getId())->toFullArray()); |
|
| 113 | } else { |
||
| 114 | $this->db->rollback(); |
||
| 115 | throw new UnprocessableEntityHttpException((string) $this->model->getMessages()[0]); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 |