1
|
|
|
<?php
|
|
|
|
|
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'];
|
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 Exception('Record not found');
|
75
|
|
|
}
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* Update a User Info
|
80
|
|
|
*
|
81
|
|
|
* @method PUT
|
82
|
|
|
* @url /v1/company/{id}
|
83
|
|
|
*
|
84
|
|
|
* @return Phalcon\Http\Response
|
85
|
|
|
*/
|
86
|
|
|
public function edit($id) : Response
|
87
|
|
|
{
|
88
|
|
|
$company = $this->model->findFirst([
|
89
|
|
|
'id = ?0 AND is_deleted = 0 and users_id = ?1',
|
90
|
|
|
'bind' => [$id, $this->userData->getId()],
|
|
|
|
|
91
|
|
|
]);
|
92
|
|
|
|
93
|
|
|
if ($company) {
|
94
|
|
|
$request = $this->request->getPut();
|
|
|
|
|
95
|
|
|
|
96
|
|
|
if (empty($request)) {
|
97
|
|
|
$request = $this->request->getJsonRawBody(true);
|
98
|
|
|
}
|
99
|
|
|
|
100
|
|
|
//update
|
101
|
|
|
if ($company->update($request, $this->updateFields)) {
|
102
|
|
|
return $this->response($company);
|
|
|
|
|
103
|
|
|
} else {
|
104
|
|
|
//didnt work
|
105
|
|
|
throw new Exception(current($company->getMessages()));
|
106
|
|
|
}
|
107
|
|
|
} else {
|
108
|
|
|
throw new Exception('Record not found');
|
109
|
|
|
}
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
|