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