Skip to content

Commit

Permalink
Generate Query model
Browse files Browse the repository at this point in the history
  • Loading branch information
Haritha-Kotte committed Sep 26, 2024
1 parent 8f7fbfb commit d90065f
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
51 changes: 51 additions & 0 deletions app/controllers/queries_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class QueriesController < ApplicationController
before_action :set_query, only: %i[ show update destroy ]

# GET /queries
def index
@queries = Query.all

render json: @queries
end

# GET /queries/1
def show
render json: @query
end

# POST /queries
def create
@query = Query.new(query_params)

if @query.save
render json: @query, status: :created, location: @query
else
render json: @query.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /queries/1
def update
if @query.update(query_params)
render json: @query
else
render json: @query.errors, status: :unprocessable_entity
end
end

# DELETE /queries/1
def destroy
@query.destroy!
end

private
# Use callbacks to share common setup or constraints between actions.
def set_query
@query = Query.find(params[:id])
end

# Only allow a list of trusted parameters through.
def query_params
params.require(:query).permit(:description)
end
end
2 changes: 2 additions & 0 deletions app/models/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Query < ApplicationRecord
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :queries
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20240926053302_create_queries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateQueries < ActiveRecord::Migration[7.2]
def change
create_table :queries do |t|
t.text :description

t.timestamps
end
end
end
19 changes: 19 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions test/controllers/queries_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require "test_helper"

class QueriesControllerTest < ActionDispatch::IntegrationTest
setup do
@query = queries(:one)
end

test "should get index" do
get queries_url, as: :json
assert_response :success
end

test "should create query" do
assert_difference("Query.count") do
post queries_url, params: { query: { description: @query.description } }, as: :json
end

assert_response :created
end

test "should show query" do
get query_url(@query), as: :json
assert_response :success
end

test "should update query" do
patch query_url(@query), params: { query: { description: @query.description } }, as: :json
assert_response :success
end

test "should destroy query" do
assert_difference("Query.count", -1) do
delete query_url(@query), as: :json
end

assert_response :no_content
end
end
7 changes: 7 additions & 0 deletions test/fixtures/queries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
description: MyText

two:
description: MyText
7 changes: 7 additions & 0 deletions test/models/query_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class QueryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit d90065f

Please sign in to comment.