diff --git a/lib/NGCP/Panel/Controller/API/SoundFiles.pm b/lib/NGCP/Panel/Controller/API/SoundFiles.pm index 41827c07d5..1a93b01f90 100644 --- a/lib/NGCP/Panel/Controller/API/SoundFiles.pm +++ b/lib/NGCP/Panel/Controller/API/SoundFiles.pm @@ -152,17 +152,14 @@ sub POST :Allow { my $form = $self->get_form($c); my $item; - try { - my $tmp_item = $c->model('DB')->resultset('voip_sound_files')->search_rs({ - set_id => $resource->{set_id}, - handle_id => $resource->{handle_id}, - })->first; - $item = $self->update_item($c, $tmp_item, undef, $resource, $form); - } catch($e) { - $c->log->error("failed to create soundfile: $e"); # TODO: user, message, trace, ... - $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create soundfile."); - last; - } + my $tmp_item = $self->item_rs($c)->search_rs({ + set_id => $resource->{set_id}, + 'handle.name' => $resource->{handle}, + },{ + join => 'handle', + })->first; + $item = $self->update_item($c, $tmp_item, undef, $resource, $form); + last unless $item; $guard->commit; diff --git a/lib/NGCP/Panel/Role/API/SoundFiles.pm b/lib/NGCP/Panel/Role/API/SoundFiles.pm index 4dc1ac3178..81514d1de1 100644 --- a/lib/NGCP/Panel/Role/API/SoundFiles.pm +++ b/lib/NGCP/Panel/Role/API/SoundFiles.pm @@ -158,7 +158,7 @@ sub update_item { unless($handle) { $c->log->error("invalid handle '$$resource{handle}', must be in group pbx, music_on_hold or digits for a customer sound set"); $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Handle must be in group pbx, music_on_hold or digits for a customer sound set"); - last; + return; } } else { $handle_rs = $handle_rs->search({ @@ -170,7 +170,7 @@ sub update_item { unless($handle) { $c->log->error("invalid handle '$$resource{handle}', must not be in group pbx for a system sound set"); $self->error($c, HTTP_UNPROCESSABLE_ENTITY, "Handle must not be in group pbx for a system sound set"); - last; + return; } } $resource->{handle_id} = $handle->id; @@ -196,10 +196,16 @@ sub update_item { last unless($resource); delete $resource->{handle}; - if ($item) { - $item->update($resource); - } else { - $item = $c->model('DB')->resultset('voip_sound_files')->create($resource); + try { + if ($item) { + $item->update($resource); + } else { + $item = $c->model('DB')->resultset('voip_sound_files')->create($resource); + } + } catch($e) { + $c->log->error("failed to create soundfile: $e"); # TODO: user, message, trace, ... + $self->error($c, HTTP_INTERNAL_SERVER_ERROR, "Failed to create soundfile."); + return; } return $item;