{"title":"JSON API Full CRUD","description":"","section":"examples","version":"v1.5","path":"examples/json-api-full-crud","canonical_url":"https://amberframework.org/docs/v1.5/examples/json-api-full-crud","markdown_url":"https://amberframework.org/docs/v1.5/examples/json-api-full-crud.md","inherited":true,"content_markdown":"# JSON API full CRUD\n\nIf you want to configure a full crud JSON API, you will need to first generate a model with the following command (for this example we will use the model `Post` with `title` and `body` attributes):\n\n```\namber generate model Post title body\n```\n\nThis will generate a database migration and a `Post` model file in `models/post.cr`.\n\n{% code-tabs %}\n{% code-tabs-item title=\"src/models/post.cr\" %}\n```crystal\nclass Post < Granite::Base\n  connection pg\n  table posts\n\n  column id : Int64, primary: true\n  column title : String?\n  column body : String?\nend\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\nThen in your controller:\n\n{% code-tabs %}\n{% code-tabs-item title=\"src/controllers/post_controller.cr\" %}\n```crystal\nclass PostController < ApplicationController\n  def index\n    posts = Post.all\n    respond_with 200 do\n      json posts.to_json\n    end\n  end\n\n  def show\n    if post = Post.find params[\"id\"]\n      respond_with 200 do\n        json post.to_json\n      end\n    else\n      results = {status: \"not found\"}\n      respond_with 404 do\n        json results.to_json\n      end\n    end\n  end\n\n  def create\n    post = Post.new(post_params.validate!)\n\n    if post.valid? && post.save\n      respond_with 201 do\n        json post.to_json\n      end\n    else\n      results = {status: \"invalid\"}\n      respond_with 422 do\n        json results.to_json\n      end\n    end\n  end\n\n  def update\n    if post = Post.find(params[\"id\"])\n      post.set_attributes(post_params.validate!)\n      if post.valid? && post.save\n        respond_with 200 do\n          json post.to_json\n        end\n      else\n        results = {status: \"invalid\"}\n        respond_with 422 do\n          json results.to_json\n        end\n      end\n    else\n      results = {status: \"not found\"}\n      respond_with 404 do\n        json results.to_json\n      end\n    end\n  end\n\n  def destroy\n    if post = Post.find params[\"id\"]\n      post.destroy\n      respond_with 204 do\n        json \"\"\n      end\n    else\n      results = {status: \"not found\"}\n      respond_with 404 do\n        json results.to_json\n      end\n    end\n  end\n\n  def post_params\n    params.validation do\n      required(:title, msg: nil, allow_blank: true)\n      required(:body, msg: nil, allow_blank: true)\n    end\n  end\nend\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}\n\nAnd your routes file:\n\n{% code-tabs %}\n{% code-tabs-item title=\"config/routes.cr\" %}\n```crystal\nAmber::Server.configure do |app|\n  pipeline :api do\n    # pipelines...\n  end\n\n  routes :api do\n    # other routes,,,\n    resources \"/posts\", PostController, except: [:edit, :new]\n  end\nend\n```\n{% endcode-tabs-item %}\n{% endcode-tabs %}"}