Skip to content

Commit 016bc46

Browse files
wip2
1 parent a3932bb commit 016bc46

File tree

2 files changed

+36
-55
lines changed

2 files changed

+36
-55
lines changed

application/controllers/ApiV1ContactgroupsController.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public function indexAction(): void
4646
$responseCode = 200;
4747
$db = Database::get();
4848
$identifier = $request->getParam('identifier');
49-
// TODO: Remove rawurldecode(). Only added to test, bcz phpstorm's http client encodes the params
50-
$queryString = rawurldecode(Url::fromRequest()->getQueryString());
49+
5150
$filter = FilterProcessor::assembleFilter(
52-
QueryString::fromString($queryString)
51+
QueryString::fromString(Url::fromRequest()->getQueryString())
5352
->on(
5453
QueryString::ON_CONDITION,
5554
function (Filter\Condition $condition) {
@@ -140,34 +139,27 @@ function (Filter\Condition $condition) {
140139

141140
$this->assertValidData($data);
142141

142+
if ($this->getContactgroupId($data['id']) !== null) {
143+
throw new HttpException('422', 'Contactgroup already exists');
144+
}
145+
143146
$db->beginTransaction();
144147

145148
if ($identifier === null) {
146-
if ($this->getContactgroupId($data['id']) !== null) {
147-
throw new HttpException('422', 'Contactgroup already exists');
148-
}
149-
150149
$this->addContactgroup($data);
151-
152-
$identifier = $data['id'];
153150
} else {
154151
$contactgroupId = $this->getContactgroupId($identifier);
155152
if ($contactgroupId === null) {
156153
$this->httpNotFound('Contactgroup not found');
157154
}
158155

159-
if ($identifier === $data['id']) {
160-
throw new HttpException('422', 'Contactgroup already exists');
161-
}
162-
163-
$identifier = $data['id'];
164156
$this->removeContactgroup($contactgroupId);
165157
$this->addContactgroup($data);
166158
}
167159

168160
$db->commitTransaction();
169161

170-
$this->getResponse()->setHeader('Location', self::ENDPOINT . '/' . $identifier);
162+
$this->getResponse()->setHeader('Location', self::ENDPOINT . '/' . $data['id']);
171163
$responseCode = 201;
172164

173165
break;
@@ -188,11 +180,9 @@ function (Filter\Condition $condition) {
188180

189181
$contactgroupId = $this->getContactgroupId($identifier);
190182
if ($contactgroupId !== null) {
191-
$db->update('contactgroup', [
192-
'name' => $data['name'],
193-
], ['id = ?' => $contactgroupId]);
183+
$db->update('contactgroup', ['name' => $data['name']], ['id = ?' => $contactgroupId]);
194184

195-
$db->delete('contactgroup_member', ['contactgroup_id = ?' => $identifier]);
185+
$db->delete('contactgroup_member', ['contactgroup_id = ?' => $contactgroupId]);
196186

197187
if (! empty($data['users'])) {
198188
$this->addUsers($contactgroupId, $data['users']);
@@ -309,13 +299,12 @@ private function getContactgroupId(string $identifier): ?int
309299
*/
310300
private function addContactgroup(array $data): void
311301
{
312-
$db = Database::get();
313-
$db->insert('contactgroup', [
302+
Database::get()->insert('contactgroup', [
314303
'name' => $data['name'],
315304
'external_uuid' => $data['id']
316305
]);
317306

318-
$id = $db->lastInsertId();
307+
$id = Database::get()->lastInsertId();
319308

320309
if (! empty($data['users'])) {
321310
$this->addUsers($id, $data['users']);
@@ -349,10 +338,8 @@ private function addUsers(int $contactgroupId, array $users): void
349338
*/
350339
private function removeContactgroup(int $id): void
351340
{
352-
$db = Database::get();
353-
354-
$db->delete('contactgroup_member', ['contactgroup_id = ?' => $id]);
355-
$db->delete('contactgroup', ['id = ?' => $id]);
341+
Database::get()->delete('contactgroup_member', ['contactgroup_id = ?' => $id]);
342+
Database::get()->delete('contactgroup', ['id = ?' => $id]);
356343
}
357344

358345
/**
@@ -365,7 +352,7 @@ private function removeContactgroup(int $id): void
365352
private function assertValidData(array $data): void
366353
{
367354
if (! isset($data['id'], $data['name'])) {
368-
$this->httpBadRequest('missing required fields');
355+
$this->httpBadRequest('fields id and name are required');
369356
}
370357
}
371358
}

application/controllers/ApiV1ContactsController.php

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ public function indexAction(): void
4646
$responseCode = 200;
4747
$db = Database::get();
4848
$identifier = $request->getParam('identifier');
49-
// TODO: Remove rawurldecode(). Only added to test, bcz phpstorm's http client encodes the params
50-
$queryString = rawurldecode(Url::fromRequest()->getQueryString());
49+
5150
$filter = FilterProcessor::assembleFilter(
52-
QueryString::fromString($queryString)
51+
QueryString::fromString(Url::fromRequest()->getQueryString())
5352
->on(
5453
QueryString::ON_CONDITION,
5554
function (Filter\Condition $condition) {
@@ -162,34 +161,27 @@ function (Filter\Condition $condition) {
162161

163162
$this->assertValidData($data);
164163

164+
if ($this->getContactId($data['id']) !== null) {
165+
throw new HttpException('422', 'Contact already exists');
166+
}
167+
165168
$db->beginTransaction();
166169

167170
if ($identifier === null) {
168-
if ($this->getContactId($data['id']) !== null) {
169-
throw new HttpException('422', 'Contact already exists');
170-
}
171-
172171
$this->addContact($data);
173-
$identifier = $data['id'];
174172
} else {
175173
$contactId = $this->getContactId($identifier);
176174
if ($contactId === null) {
177175
$this->httpNotFound('Contact not found');
178176
}
179177

180-
if ($identifier === $data['id']) {
181-
throw new HttpException('422', 'Contact already exists');
182-
}
183-
184178
$this->removeContact($contactId);
185179
$this->addContact($data);
186-
187-
$identifier = $data['id'];
188180
}
189181

190182
$db->commitTransaction();
191183

192-
$this->getResponse()->setHeader('Location', self::ENDPOINT . '/' . $identifier);
184+
$this->getResponse()->setHeader('Location', self::ENDPOINT . '/' . $data['id']);
193185
$responseCode = 201;
194186

195187
break;
@@ -210,6 +202,10 @@ function (Filter\Condition $condition) {
210202

211203
$contactId = $this->getContactId($identifier);
212204
if ($contactId !== null) {
205+
if (isset($data['username'])) {
206+
$this->assertUniqueUsername($data['username']);
207+
}
208+
213209
$db->update('contact', [
214210
'full_name' => $data['full_name'],
215211
'username' => $data['username'] ?? null,
@@ -382,20 +378,18 @@ protected function getContactId(string $identifier): ?int
382378
*/
383379
private function addContact(array $data): void
384380
{
385-
$db = Database::get();
386-
387381
if (isset($data['username'])) {
388382
$this->assertUniqueUsername($data['username']);
389383
}
390384

391-
$db->insert('contact', [
385+
Database::get()->insert('contact', [
392386
'full_name' => $data['full_name'],
393387
'username' => $data['username'] ?? null,
394388
'default_channel_id' => $this->getChannelId($data['default_channel']),
395389
'external_uuid' => $data['id']
396390
]);
397391

398-
$contactId = $db->lastInsertId();
392+
$contactId = Database::get()->lastInsertId();
399393

400394
if (! empty($data['addresses'])) {
401395
$this->addAddresses($contactId, $data['addresses']);
@@ -443,12 +437,15 @@ private function assertAddressTypesExist(array $addressTypes): void
443437
$types = Database::get()->fetchCol(
444438
(new Select())
445439
->from('available_channel_type')
446-
->columns(1)
440+
->columns('type')
447441
->where(['type IN (?)' => $addressTypes])
448442
);
449443

450444
if (count($types) !== count($addressTypes)) {
451-
$this->httpBadRequest('An undefined address type given');
445+
$this->httpBadRequest(sprintf(
446+
'undefined address type %s given',
447+
implode(', ', array_diff($addressTypes, $types))
448+
));
452449
}
453450
}
454451

@@ -485,7 +482,6 @@ private function addAddresses(int $contactId, array $addresses): void
485482
$this->assertAddressTypesExist(array_keys($addresses));
486483

487484
foreach ($addresses as $type => $address) {
488-
//TODO: Check if type exists, db allows any type
489485
Database::get()->insert('contact_address', [
490486
'contact_id' => $contactId,
491487
'type' => $type,
@@ -503,11 +499,9 @@ private function addAddresses(int $contactId, array $addresses): void
503499
*/
504500
private function removeContact(int $id): void
505501
{
506-
$db = Database::get();
507-
508-
$db->delete('contactgroup_member', ['contact_id = ?' => $id]);
509-
$db->delete('contact_address', ['contact_id = ?' =>$id]);
510-
$db->delete('contact', ['id = ?' => $id]);
502+
Database::get()->delete('contactgroup_member', ['contact_id = ?' => $id]);
503+
Database::get()->delete('contact_address', ['contact_id = ?' =>$id]);
504+
Database::get()->delete('contact', ['id = ?' => $id]);
511505
}
512506

513507
/**
@@ -522,7 +516,7 @@ private function removeContact(int $id): void
522516
private function assertValidData(array $data): void
523517
{
524518
if (! isset($data['id'], $data['full_name'], $data['default_channel'])) {
525-
$this->httpBadRequest('missing required fields');
519+
$this->httpBadRequest('fields id, full_name and default_channel are required');
526520
}
527521
}
528522
}

0 commit comments

Comments
 (0)