Phoenix JSON API: Generating code for CRUD actions

This lesson is part of a series on building JSON APIs with Phoenix. Previous lessons:

  • Getting started, where we set up a very simple API endpoint to return a random number from 1 to 6.
  • Rendering Many, where we made an endpoint that returns a collection of items in a JSON response

This time, we'll add support to our API for famous quotes. Each quotation will have two fields—one for the text of the quotation itself and one for its author. We'll allow quotations to be shown and listed via JSON requests just as we previously set up for dice. This time, we'll store them in the database and also allow them to be created, updated or deleted via JSON requests.

Generating code

One of the most powerful features in back-end frameworks such as Pheonix is pragmatic code generation. Code generation can certainly be abused, but when used properly it can dramatically accelerate development and lead to a higher level uniformity between applications.

The generator we'll be using this time is the Phoenix JSON generator. It will create a new schema, a database migration for it, a controller, and a context file to encapsulate DB interactions. Enter this into your terminal:

mix phx.gen.json Content Quotation quotations text:text author:string

And you should see the following output:

* creating lib/firehose_web/controllers/quotation_controller.ex
* creating lib/firehose_web/views/quotation_view.ex
* creating test/firehose_web/controllers/quotation_controller_test.exs
* creating lib/firehose_web/views/changeset_view.ex
* creating lib/firehose_web/controllers/fallback_controller.ex
* creating lib/firehose/content/quotation.ex
* creating priv/repo/migrations/20220604053917_create_quotations.exs
* creating lib/firehose/content.ex
* injecting lib/firehose/content.ex
* creating test/firehose/content_test.exs
* injecting test/firehose/content_test.exs
* creating test/support/fixtures/content_fixtures.ex
* injecting test/support/fixtures/content_fixtures.ex

Add the resource to your :api scope in lib/firehose_web/router.ex:

    resources "/quotations", QuotationController, except: [:new, :edit]


Remember to update your repository by running migrations:

    $ mix ecto.migrate

Add the new route to the bottom of the api scope in your router, as directed:

  scope "/api", FirehoseWeb do
    pipe_through :api

    get "/roll", RollController, :index
    get "/roll/:num_dice", RollController, :show
    resources "/quotations", QuotationController, except: [:new, :edit]
  end

And run mix ecto.migrate to migrate your database.

Thats it?

That's it! The server now has a database-backed schema for quotations.

Start the server and set a GET request to localhost:4000/api/quotations and you'll get back a response with an empty collection. Open up your favorite REST client (or Ma Huachao's REST Client plugin for VS Code). Now you can POST a new quotation to the server like so:

POST http://localhost:4000/api/quotations HTTP/1.1
content-type: application/json

{ 
  "quotation": {

    "author": "Dennis Ritchie",
    "text": "The only way to learn a new programming language is by writing programs in it"
  }
}

Or update it:

PATCH http://localhost:4000/api/quotations/1 HTTP/1.1
content-type: application/json

{ 
  "quotation": {
    "author": "Gandalf"
  }
}
Back to index

No Comments