Lumen API 07: Creating URL To Delete a Resource

Yanuar Arifin
4 min readDec 30, 2019
Photo by Gary Chan on Unsplash

This is what jsonapi.org says about delete(we will be covering when the resource is not exist, and another state when the field exists and delete success)

  • if resource does not exists, returns 404 and empty body
  • if resource exists, delete and return 200 and empty body

The Test Cases

From those specs above, there are few tests that we need to do:

  • test when we request delete to non existent record, it will return 404
  • when we request delete to record that exists, it will delete it from database. But since we are using soft delete feature from Laravel, we still has the record in database, to differentiate between record that is currently deleted and the one that still available, we could see from deleted_at field, if it has value of NULL , then it is exists, if it has value of datetime, then currently it is deleted
  • when we request delete to record that exists, we will receive 200 as the HTTP code
  • when we request deletion to record that exists, we will receive empty body

Let’s create the test:

    /**
* deleting using bogus id return s40
*
* @return void
*/
public function testDeleteNonexistentRecordReturns404()
{
$item = factory(App\Item::class)->raw();
$this->delete('/items/300'); $this->assertResponseStatus(404);
}
/**
* deleting will return 200
*
* @return void
*/
public function testDeleteExistingRecordReturns200()
{
factory(App\Item::class, 45)->create();
$this->delete('/items/3'); $this->assertResponseStatus(200);
}
/**
* deleting the resource will update the value of
* deleted_at field from NULL to datetime it was deleted
*
* @return void
*/
public function testDeletExistingRecordUpdateDeletedAtToNotNull()
{
factory(App\Item::class, 45)->create();
$deleted_id= 3;
$this->delete('/item/' . $deleted_id); $deleted_item = Item::where(['id' => $deleted_id])->where('deleted_at', '<>', null)->first(); $this->assertNull($deleted_item);
}

/**
* response of successfull deletion is blank page
*
* @return void
*/
public function testDeleteExistingRecordReturnsEmptyPage()
{
factory(App\Item::class, 45)->create();
$this->delete('/items/3'); $response = $this->response->getOriginalContent(); $this->assertEmpty($response);
}

If you run it, of course it will fail.

Let’s fix that

Write The Code

Create Route

First thing first as we have covered in previous articles, we need to make route so your clients can access your delete function. Open the routes/web.php file, and add this code below PUT route

$router->delete('/items/{id}', ["as" => "item.delete", 'uses' => "ItemController@delete"]);

What this code do? it will accept DELETE HTTP verb, then pass it to a function called delete() in your ItemController .

Handling Request

Next part is writing the logic to handle DELETE request. create a function called delete() in your app/Http/Controllers/ItemController.php . Write this code to that function

    /**
* delete a resource, return 404 if not found,
* 200 if record exist in DB and update
* deletd_at field no datetime of deletion
*
* @return void
*/
public function delete(Request $request, $id)
{
$item = Item::findOrFail($id);
$item->delete(); return response()->json('', 200);
}

This code will accept the id parameter in your URL, for example https://item.local/items/5 means the id is 5 . Then your app will search for items with that id, if not found it will return 404, if found it will delete that item (remember we use soft delete, it will not be erased from your database but only given value to deleted_at field) then returns empty response with 200 HTTP code which means your app successfully delete the record.

Run test again, it will succeed

Alright, now your test has all passed.

Conclusion

This is the last part of our basic Lumen app creation, it covers the basic API functionality that you will find in most of public API. To remind you, there are:

  • GET without id will show a list of resources (in our case list of items)
  • GET with id will give you the information about an item with the requested id
  • POST will create a new item
  • PUT will update an existing item
  • and finally DELETE which will remove an item

There are other variation to what you could serve in your API response, for example adding other resource that is connected to your requested resource. For example you request an item, maybe you could give its category in your response. Or other custom action, like adding a discount to an item if in a shop website.

This is only the beginning of making API, look out for more at the internet, there are so many things you could do with an API.

See you in the next article

--

--