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