[RUBY] Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation

Building a bulletin board API with certification authorization with Rails 6 # 4 post validation, test implementation

Make a controller

Since I made a model last time, I will implement a controller this time.

$ rails g controller v1/posts

When executed, a controller and request spec file will be generated.

For the time being, implement the controller as follows.

app/controllers/v1/posts_controller.rb


# frozen_string_literal: true

module V1
  #
  #  post controller
  #
  class PostsController < ApplicationController
    before_action :set_post, only: %i[show update destroy]

    def index
      # TODO
    end

    def show
      # TODO
    end

    def create
      # TODO
    end

    def update
      # TODO
    end

    def destroy
      # TODO
    end

    private

    def set_post
      @post = Post.find(params[:id])
    end

    def post_params
      params.permit(:subject, :body)
    end
  end
end

Create a controller according to CRUD once without logic. In addition, cutting the namespace called V1 is a method often used in API development. This makes it easier to separate and develop when making version 2 that is not backward compatible.

Then set the routes.

config/routes.rb


 # frozen_string_literal: true

 Rails.application.routes.draw do
+   namespace "v1" do
+     resources :posts
+   end
 end

This will set the CRUD routes. Let's check.

$ rails routes
...
                               Prefix Verb   URI Pattern                                                                              Controller#Action
                             v1_posts GET    /v1/posts(.:format)                                                                      v1/posts#index
                                      POST   /v1/posts(.:format)                                                                      v1/posts#create
                              v1_post GET    /v1/posts/:id(.:format)                                                                  v1/posts#show
                                      PATCH  /v1/posts/:id(.:format)                                                                  v1/posts#update
                                      PUT    /v1/posts/:id(.:format)                                                                  v1/posts#update
                                      DELETE /v1/posts/:id(.:format)                                                                  v1/posts#destroy
...

Implementation of index test

As usual, implement the test first. As a behavior

I will go. I won't include a pager for a tutorial on a simple test application, but maybe I'll write an article in the future.

spec/requests/v1/posts_controller.rb


# frozen_string_literal: true

require "rails_helper"

RSpec.describe "V1::Posts", type: :request do
  describe "GET /v1/posts#index" do
    before do
      create_list(:post, 3)
    end
    it "Normal response code is returned" do
      get v1_posts_url
      expect(response.status).to eq 200
    end
    it "The number is returned correctly" do
      get v1_posts_url
      json = JSON.parse(response.body)
      expect(json["posts"].length).to eq(3)
    end
    it "Responses are returned in descending order of id" do
      get v1_posts_url
      json = JSON.parse(response.body)
      first_id = json["posts"][0]["id"]
      expect(json["posts"][1]["id"]).to eq(first_id - 1)
      expect(json["posts"][2]["id"]).to eq(first_id - 2)
    end
  end
end

In other words, to summarize the behavior, LINE 10: it "Normal response code is returned" Block start LINE 8: 3 posts are saved LINE 11: Make a get request to v1_posts_url (v1 / posts / index) LINE 12: Response code is: ok (200 normal) LINE 13: it "Normal response code is returned" Block end. Rolled back to 0 post records LINE 14: it "The number is returned correctly" Block start LINE 8: 3 posts are saved LINE 15: Make a get request to v1_posts_url (v1 / posts / index) LINE 16: Convert response.body to JSON.parse and convert to Ruby array LINE 17: Response post is 3 records LINE 18: it "The number is returned correctly" Block end. Rolled back to 0 post records ↓ ...

It will be.

At this point, the controller is not implemented, so of course the test is moss.

Strictly speaking, the final test requires a comparison of created_at, but a simple comparison is made by id. Originally, you should also test limit, but omit it. If you're curious, try implementing a test that makes 21 create_lists and confirms that only 20 are returned.

Tips.

By the way, I will introduce the factoryBot method that I often use.

Add example.com to hosts

In addition, the requests test requires the following measures.

config/application.rb


...
     config.hosts << ".amazonaws.com"
+    config.hosts << "www.example.com"
...

This is because rspec tests are recognized as requests from www.example.com.

Implementation of index

app/controllers/v1/posts_controller.rb


...
     def index
-      # TODO
+      posts = Post.order(created_at: :desc).limit(20)
+      render json: { posts: posts }
     end
...

Now you can get the list. I will try to hit the API with curl.

$ curl localhost:8080/v1/posts
{"posts":[{"id":2,"subject":"","body":"hoge","created_at":"2020-09-06T01:07:52.628Z","updated_at":"2020-09-06T01:07:52.628Z"},{"id":1,"subject":"hoge","body":"fuga","created_at":"2020-09-05T13:50:01.797Z","updated_at":"2020-09-05T13:50:01.797Z"}]}

If you get empty data, try creating a post record from rails c.

Once you've done that, don't forget to run rubocop or rspec, then git commit.

Continued

Building a bulletin board API with authentication authorization in Rails 6 # 6 show, create implementation

[To the serial table of contents]

Recommended Posts

Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 13 Add authentication header
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
Introduced # 10 devise_token_auth to build a bulletin board API with authentication authorization in Rails 6
Introducing # 15 pundit to build a bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Build a bulletin board API with authentication authorization with Rails 6 # 2 Introducing git and rubocop
Build a bulletin board API with authentication authorization in Rails 6 # 11 User model test and validation added
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
[Implementation procedure] Create a user authentication function using sorcery in Rails
I implemented Rails API with TDD by RSpec. part3-Action implementation with authentication-
I tried to make a group function (bulletin board) with Rails
I implemented Rails API with TDD by RSpec. part1-Action implementation without authentication-
Create a SPA with authentication function with Rails API mode + devise_token_auth + Vue.js 3 (Rails edition)
Create a simple bulletin board with Java + MySQL
Try to create a bulletin board in Java
How to build API with GraphQL and Rails
[Note] Build a Python3 environment with Docker in EC2
[How to insert a video in haml with Rails]
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
Build Rails (API) x MySQL x Nuxt.js environment with Docker
Create a SlackBot with AWS lambda & API Gateway in Java
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
I came across a guy with two dots in Rails
How to set up a proxy with authentication in Feign