From d90065feb49043086cb308a0010a0626441187e1 Mon Sep 17 00:00:00 2001 From: Haritha Kotte Date: Thu, 26 Sep 2024 11:03:50 +0530 Subject: [PATCH] Generate Query model --- app/controllers/queries_controller.rb | 51 +++++++++++++++++++++ app/models/query.rb | 2 + config/routes.rb | 1 + db/migrate/20240926053302_create_queries.rb | 9 ++++ db/schema.rb | 19 ++++++++ test/controllers/queries_controller_test.rb | 38 +++++++++++++++ test/fixtures/queries.yml | 7 +++ test/models/query_test.rb | 7 +++ 8 files changed, 134 insertions(+) create mode 100644 app/controllers/queries_controller.rb create mode 100644 app/models/query.rb create mode 100644 db/migrate/20240926053302_create_queries.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/queries_controller_test.rb create mode 100644 test/fixtures/queries.yml create mode 100644 test/models/query_test.rb diff --git a/app/controllers/queries_controller.rb b/app/controllers/queries_controller.rb new file mode 100644 index 0000000..151bd48 --- /dev/null +++ b/app/controllers/queries_controller.rb @@ -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 diff --git a/app/models/query.rb b/app/models/query.rb new file mode 100644 index 0000000..2a0e5b8 --- /dev/null +++ b/app/models/query.rb @@ -0,0 +1,2 @@ +class Query < ApplicationRecord +end diff --git a/config/routes.rb b/config/routes.rb index a125ef0..7d679ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/db/migrate/20240926053302_create_queries.rb b/db/migrate/20240926053302_create_queries.rb new file mode 100644 index 0000000..26cb98a --- /dev/null +++ b/db/migrate/20240926053302_create_queries.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..1a64b2b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,19 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2024_09_26_053302) do + create_table "queries", force: :cascade do |t| + t.text "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end +end diff --git a/test/controllers/queries_controller_test.rb b/test/controllers/queries_controller_test.rb new file mode 100644 index 0000000..011446b --- /dev/null +++ b/test/controllers/queries_controller_test.rb @@ -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 diff --git a/test/fixtures/queries.yml b/test/fixtures/queries.yml new file mode 100644 index 0000000..dd72caa --- /dev/null +++ b/test/fixtures/queries.yml @@ -0,0 +1,7 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + description: MyText + +two: + description: MyText diff --git a/test/models/query_test.rb b/test/models/query_test.rb new file mode 100644 index 0000000..77aee87 --- /dev/null +++ b/test/models/query_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class QueryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end