@@ -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