Skip to content

Commit

Permalink
Feature/group controller (#7)
Browse files Browse the repository at this point in the history
* first groups api test

* code style fixes

* started writing controller and fixed test

* finished controller and fixed tests

* postman collection for backend

* code style fixes

* code style fixes 2
  • Loading branch information
23vbq authored Sep 13, 2024
1 parent e9dc584 commit 82ac6b2
Show file tree
Hide file tree
Showing 10 changed files with 292 additions and 0 deletions.
2 changes: 2 additions & 0 deletions todo_backend/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class ApplicationController < ActionController::API
include Response
include ExceptionHandler
end
13 changes: 13 additions & 0 deletions todo_backend/app/controllers/concerns/exception_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ExceptionHandler
extend ActiveSupport::Concern

included do
rescue_from ActiveRecord::RecordNotFound do |e|
json_response({ message: e.message }, :not_found)
end

rescue_from ActiveRecord::RecordInvalid do |e|
json_response({ message: e.message }, :unprocessable_entity)
end
end
end
5 changes: 5 additions & 0 deletions todo_backend/app/controllers/concerns/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Response
def json_response(object, status = :ok)
render json: object, status: status
end
end
38 changes: 38 additions & 0 deletions todo_backend/app/controllers/group_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class GroupController < ApplicationController
# skip_before_action :verify_authenticity_token
before_action :set_group, only: [ :update, :destroy ]

# GET /group
def index
@groups = Group.all
json_response(@groups)
end

# POST /group
def create
@group = Group.create!(group_params)
json_response(@group, :created)
end

# PUT /group/:id
def update
@group.update(group_params)
head :no_content
end

# DELETE /group/:id
def destroy
@group.destroy
head :no_content
end

private

def group_params
params.permit(:name)
end

def set_group
@group = Group.find(params[:id])
end
end
1 change: 1 addition & 0 deletions todo_backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

# Defines the root path route ("/")
# root "posts#index"
resources :group
end
5 changes: 5 additions & 0 deletions todo_backend/spec/factories/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :group do
name { Faker::Lorem.unique.word }
end
end
7 changes: 7 additions & 0 deletions todo_backend/spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
end
end

Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# RSpec.configuration do |config|
# config.include RequestSpecHelper, type :request
# end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_paths = [
Expand Down Expand Up @@ -74,6 +80,7 @@
# config.filter_gems_from_backtrace("gem name")

config.include FactoryBot::Syntax::Methods
config.include RequestSpecHelper

config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
Expand Down
80 changes: 80 additions & 0 deletions todo_backend/spec/requests/group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rails_helper'

RSpec.describe "Groups", type: :request do
# describe "GET /index" do
# pending "add some examples (or delete) #{__FILE__}"
# end
let!(:groups) { create_list(:group, 10) }
let(:group_id) { groups.first.id.to_s }

# Get list of groups
describe "GET /group" do
before { get '/group' }

it 'returns groups' do
expect(json).not_to be_empty
expect(json.size).to eq(10)
end

it 'returns status code 200' do
expect(response).to have_http_status(200)
end
end

# Create new group
describe "POST /group" do
let(:valid_attributes) { { name: 'Group' } }

context "request is valid" do
before { post '/group', params: valid_attributes }

it "creates a group" do
expect(json['name']).to eq('Group')
end

it "returns status code 201" do
expect(response).to have_http_status(201)
end
end

context "request is invalid" do
before { post '/group', params: {} }

it "returns status code 422" do
expect(response).to have_http_status(422)
end

it "returns validation presence failure" do
expect(response.body)
.to match("{\"message\":\"Validation failed: Name can't be blank\"}")
end
end
# TODO other validations
end

# Update existing group
describe "PUT /group/:id" do
let(:valid_attributes) { { name: 'Tasks' } }

context "record exists" do
before { put '/group/' + group_id, params: valid_attributes }

it "updates record" do
expect(response.body).to be_empty
end

it "returns status code 204" do
expect(response).to have_http_status(204)
end
end
end

# Delete group
describe "DELETE /group/:id" do
before { delete '/group/' + group_id }

it "returns status code 204" do
expect(response).to have_http_status(204)
end
end
end
5 changes: 5 additions & 0 deletions todo_backend/spec/support/group_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module RequestSpecHelper
def json
JSON.parse(response.body)
end
end
136 changes: 136 additions & 0 deletions todo_vue_rails.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
{
"info": {
"_postman_id": "e45012ad-df75-41a5-83cd-f8ad1ddb265e",
"name": "todo_vue_rails",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "29255098"
},
"item": [
{
"name": "group",
"item": [
{
"name": "List groups",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{ADDR}}/group",
"host": [
"{{ADDR}}"
],
"path": [
"group"
]
}
},
"response": []
},
{
"name": "Create group",
"request": {
"method": "POST",
"header": [],
"url": {
"raw": "{{ADDR}}/group?name",
"host": [
"{{ADDR}}"
],
"path": [
"group"
],
"query": [
{
"key": "name",
"value": null
}
]
}
},
"response": []
},
{
"name": "Update group",
"request": {
"method": "PUT",
"header": [],
"url": {
"raw": "{{ADDR}}/group/:id?name",
"host": [
"{{ADDR}}"
],
"path": [
"group",
":id"
],
"query": [
{
"key": "name",
"value": null
}
],
"variable": [
{
"key": "id",
"value": ""
}
]
}
},
"response": []
},
{
"name": "Delete group",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "{{ADDR}}/group/:id",
"host": [
"{{ADDR}}"
],
"path": [
"group",
":id"
],
"variable": [
{
"key": "id",
"value": ""
}
]
}
},
"response": []
}
]
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "ADDR",
"value": "http://localhost:3000",
"type": "string"
}
]
}

0 comments on commit 82ac6b2

Please sign in to comment.