[RUBY] # 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6

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

Update test implementation

Implement the remaining 2 actions update and destroy.

update is an update process. If you can create it, it's similar, so it shouldn't get stuck.

spec/requests/v1/posts_controller.rb


...

+  describe "PUT /v1/posts#update" do
+    let(:update_param) do
+      post = create(:post)
+      update_param = attributes_for(:post, subject: "update_subject test", body: "update_body test")
+      update_param[:id] = post.id
+      update_param
+    end
+    it "Normal response code is returned" do
+      put v1_post_url({ id: update_param[:id] }), params: update_param
+      expect(response.status).to eq 200
+    end
+    it "subject,body returns correctly" do
+      put v1_post_url({ id: update_param[:id] }), params: update_param
+      json = JSON.parse(response.body)
+      expect(json["post"]["subject"]).to eq("update_subject test")
+      expect(json["post"]["body"]).to eq("update_body test")
+    end
+    it "Errors are returned when the parameter is invalid" do
+      put v1_post_url({ id: update_param[:id] }), params: { subject: "" }
+      json = JSON.parse(response.body)
+      expect(json.key?("errors")).to be true
+    end
+    it "404 response is returned when id does not exist" do
+      put v1_post_url({ id: update_param[:id] + 1 }), params: update_param
+      expect(response.status).to eq 404
+    end
+  end
...

As usual, the controller is not implemented, so the test is moss.

implementation of update

app/controllers/v1/posts_controller.rb


...

     def update
-      # TODO
+      if @post.update(post_params)
+        render json: { post: @post }
+      else
+        render json: { errors: @post.errors }
+      end
     end
...

There is no need to talk about it here either. This should pass the test.

Implementation of destroy test

spec/requests/v1/posts_controller.rb


...

+  describe "DELETE /v1/posts#destroy" do
+    let(:delete_post) do
+      create(:post)
+    end
+    it "Normal response code is returned" do
+      delete v1_post_url({ id: delete_post.id })
+      expect(response.status).to eq 200
+    end
+    it "One less and returns" do
+      delete_post
+      expect do
+        delete v1_post_url({ id: delete_post.id })
+      end.to change { Post.count }.by(-1)
+    end
+    it "404 response is returned when id does not exist" do
+      delete v1_post_url({ id: delete_post.id + 1 })
+      expect(response.status).to eq 404
+    end
+  end
...

In the opposite of post, confirm that the number of records is reduced by 1 at Jitsugyo-ji. As a point, It means calling delete_post before expect.

As mentioned earlier, let is lazy evaluated, so if there is no delete_post before expect, a record will be created with delete_post.id in expect and one record will be deleted with delete v1_post_url. In other words, + 1-1 = 0 in the expect block, and there is no change in the number of records.

Therefore, a record is generated before expect, and when delete is executed in expect, it becomes -1 record.

implementation of destroy

app/controllers/v1/posts_controller.rb


...

     def destroy
+      @post.destroy
+      render json: { post: @post }
     end
...

This is also very simple. This time it's short, but if you include the seed to be done next, it will be too long, so that's it.

Continued

Building a bulletin board API with certification authorization in Rails 6 # 8 seed implementation

[To the serial table of contents]

Recommended Posts

# 7 update, destroy implementation to build bulletin board API with authentication authorization in Rails 6
# 8 seed implementation to build bulletin board API with authentication authorization in Rails 6
# 6 show, create implementation to build bulletin board API with authentication authorization in Rails 6
# 16 policy setting to build bulletin board API with authentication authorization in Rails 6
Introduced # 9 serializer to build bulletin board API with authentication authorization in Rails 6
Build a bulletin board API with authentication authorization in Rails 6 # 5 controller, routes implementation
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 # 13 Add authentication header
Build a bulletin board API with authentication authorization in Rails # 17 Add administrator privileges
Build a bulletin board API with authentication authorization in Rails 6 # 14 seed Execution time display
Build a bulletin board API with authentication and authorization with Rails # 18 ・ Implementation of final user controller
Build a bulletin board API with authentication authorization in Rails # 12 Association of user and post
Build a bulletin board API with authentication and authorization with Rails 6 # 1 Environment construction
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 # 2 Introducing git and rubocop
Building a bulletin board API with authentication authorization with Rails 6 Validation and test implementation of # 4 post
Build a bulletin board API with authentication authorization with Rails 6 # 3 RSpec, FactoryBot introduced and post model
How to build API with GraphQL and 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-
How to build Rails 6 environment with Docker
Try to create a bulletin board in Java
One way to redirect_to with parameters in rails
[How to insert a video in haml with Rails]
Using PAY.JP API with Rails ~ Implementation Preparation ~ (payjp.js v2)
How to query Array in jsonb with Rails + postgres
Implementation policy to dynamically save and display Timezone in Rails
[Rails] Create API to download files with Active Storage [S3]
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
How to build Rails, Postgres, ElasticSearch development environment with Docker
How to set up a proxy with authentication in Feign
Things to keep in mind when using Sidekiq with Rails