2

been playing with Apigility a bit and there is something i do not like. My delete method in an entity mapper looks like:

public function delete($id)
{
    $affectedRows = $this->table->delete(
        array('userId' => $id)
    );
    if (0 === $affectedRows) {
        throw new DomainException('ID not found', 500);
    }
    return $affectedRows;
}

And in the matching entity resources i've got:

public function delete($id)
{
    $affectedRows = $this->mapper->delete($id);
    return new ApiProblem(200, 'Affected rows count ' . $affectedRows);
}

but i think it is not appropiate to call ApiProblem for a 200 code. Is there anything that suits for a success operation?

Illiax
  • 1,002
  • 1
  • 8
  • 21

2 Answers2

3

application/problem+json is NOT proper response for successful operation.

You should return response normally with status code 200 or no response with status code 204.

And you can use ApiProblem when resource is not found.

As side note: domain exception is not suitable for runtime conditions, it generally points at bug in software.

Xerkus
  • 2,695
  • 1
  • 19
  • 32
  • I understand, but if i want to send a custom message along with 200, which object should i use? – Illiax Oct 22 '14 at 16:32
  • @Illiax That will be `ZF\ContentNegotiation\ViewModel` iirc. I don't have apigility installed atm to verify though. – Xerkus Oct 22 '14 at 16:57
  • I've tried returning a viewModel instance but i get 422 "Unable to delete entity" error. (delete in the db does work) – Illiax Nov 03 '14 at 18:59
1

ApiProblem should not be used in this case. It should only be returned in case of problems/errors. Read the answer on StackOverflow here for a reference to how to respond to successful delete operations.

Community
  • 1
  • 1
Wilt
  • 41,477
  • 12
  • 152
  • 203