diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb new file mode 100644 index 00000000..6b81fadf --- /dev/null +++ b/app/controllers/articles_controller.rb @@ -0,0 +1,28 @@ +class ArticlesController < ApplicationController + @@query_string = "" + + def index + @articles = Article.all + end + def submit_query + @@query_string = params["query_string"] + print @@query_string + redirect_to "/search", query_string: params["query_string"] + end + def search + @query_string = @@query_string + print "SEARCH: " + @@query_string + @articles = Article.search(@@query_string) + end + def show + @article = Article.find(params[:id]) + end + def update + @article = Article.find(params[:id]) + @article.update( + title: params["new_article_title"], + content: params["new_article_contents"] + ) + redirect_to "/articles/" + params[:id] + end +end diff --git a/app/helpers/articles_helper.rb b/app/helpers/articles_helper.rb new file mode 100644 index 00000000..29682775 --- /dev/null +++ b/app/helpers/articles_helper.rb @@ -0,0 +1,2 @@ +module ArticlesHelper +end diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 00000000..611abf01 --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,12 @@ +class Article < ApplicationRecord + def self.search( query_string ) + # perform SQL query to find articles where + # either its title OR contents contains + # the query_string as a substring + Article.where([ + "title LIKE ? OR content LIKE ?", + "%#{query_string}%", + "%#{query_string}%" + ]) + end +end diff --git a/app/views/articles/index.html.erb b/app/views/articles/index.html.erb new file mode 100644 index 00000000..777d436f --- /dev/null +++ b/app/views/articles/index.html.erb @@ -0,0 +1,15 @@ +

Shopify Internship assessment - Encyclopedia!!

+ +

Search for articles:

+
+ + +
+ + diff --git a/app/views/articles/search.html.erb b/app/views/articles/search.html.erb new file mode 100644 index 00000000..b2ed94e5 --- /dev/null +++ b/app/views/articles/search.html.erb @@ -0,0 +1,9 @@ +

Some results for: "<%= @query_string %>"

+ \ No newline at end of file diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb new file mode 100644 index 00000000..7a292c4c --- /dev/null +++ b/app/views/articles/show.html.erb @@ -0,0 +1,10 @@ +

<%= @article.title %>

+ +

<%= @article.content %>

+ +

Submit this form to update article:

+
+ + + +
diff --git a/config/routes.rb b/config/routes.rb index a125ef08..77f5312d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,12 @@ # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. get "up" => "rails/health#show", as: :rails_health_check + get "/articles", to: "articles#index" + root "articles#index" + get "/search", to: "articles#search" + post "/submit_query", to: "articles#submit_query" + get "/articles/:id", to: "articles#show" + post "/articles/:id/update", to: "articles#update" # Defines the root path route ("/") # root "posts#index" diff --git a/db/migrate/20240130025832_create_articles.rb b/db/migrate/20240130025832_create_articles.rb new file mode 100644 index 00000000..4df19bef --- /dev/null +++ b/db/migrate/20240130025832_create_articles.rb @@ -0,0 +1,12 @@ +class CreateArticles < ActiveRecord::Migration[7.1] + def change + create_table :articles do |t| + t.string :title + t.string :content + t.string :author + t.datetime :date + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..673f8a4b --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# 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.1].define(version: 2024_01_30_025832) do + create_table "articles", force: :cascade do |t| + t.string "title" + t.string "content" + t.string "author" + t.datetime "date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/articles_controller_test.rb b/test/controllers/articles_controller_test.rb new file mode 100644 index 00000000..0a82cbb1 --- /dev/null +++ b/test/controllers/articles_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class ArticlesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/fixtures/articles.yml b/test/fixtures/articles.yml new file mode 100644 index 00000000..a9050390 --- /dev/null +++ b/test/fixtures/articles.yml @@ -0,0 +1 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html