Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend Challenge - Encyclopedia CRUD Implementation [Damanjit Gill] #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile.lock
Expand Up @@ -147,6 +147,8 @@ GEM
racc (~> 1.4)
nokogiri (1.15.5-aarch64-linux)
racc (~> 1.4)
nokogiri (1.15.5-x64-mingw32)
racc (~> 1.4)
nokogiri (1.15.5-x86_64-linux)
racc (~> 1.4)
psych (5.1.1.1)
Expand Down Expand Up @@ -215,6 +217,7 @@ GEM
sqlite3 (1.6.9)
mini_portile2 (~> 2.8.0)
sqlite3 (1.6.9-aarch64-linux)
sqlite3 (1.6.9-x64-mingw32)
sqlite3 (1.6.9-x86_64-linux)
stimulus-rails (1.3.0)
railties (>= 6.0.0)
Expand All @@ -227,6 +230,8 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
tzinfo-data (1.2023.4)
tzinfo (>= 1.0.0)
web-console (4.2.1)
actionview (>= 6.0.0)
activemodel (>= 6.0.0)
Expand All @@ -248,6 +253,7 @@ GEM
PLATFORMS
aarch64-linux
ruby
x64-mingw32
x86_64-linux

DEPENDENCIES
Expand Down
52 changes: 52 additions & 0 deletions app/controllers/articles_controller.rb
@@ -0,0 +1,52 @@
class ArticlesController < ApplicationController
def index
@articles = Article.all

if params[:query]
@articles = Article.search params[:query]
@query = params[:query]
end
end

def show
@article = Article.find(params[:id])
end

def new
@article = Article.new
end

def create
@article = Article.new(article_params)

if @article.save
redirect_to @article, notice: "Successfully created new article!"
else
render :new, status: :unprocessable_entity
end
end

def edit
@article = Article.find(params[:id])
end

def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article, notice: "Successfully updated article!"
else
render :edit, status: :unprocessable_entity
end
end

def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to root_path, notice: "Successfully deleted article!"
end

private
def article_params
params.require(:article).permit(:title, :content, :author, :date)
end
end
2 changes: 2 additions & 0 deletions app/helpers/articles_helper.rb
@@ -0,0 +1,2 @@
module ArticlesHelper
end
12 changes: 12 additions & 0 deletions app/models/article.rb
@@ -0,0 +1,12 @@
class Article < ApplicationRecord
def self.search(query)
# search by checking both title and content of articles
return Article.where("title LIKE ? OR content LIKE ?", "%#{query}%", "%#{query}%")
end

# title must be present and has a maximum length
validates :title, presence: true, length: {minimum: 1, maximum: 200}
# content must be present
validates :content, presence: true, length: { minimum: 10 }

end
37 changes: 37 additions & 0 deletions app/views/articles/_form.html.erb
@@ -0,0 +1,37 @@
<%= form_with model: @article do |form| %>
<div>
<%= form.label :title, "Article Title" %><br>
<%= form.text_field :title %>
<% @article.errors.full_messages_for(:title).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :content, "Article Content" %><br>
<%= form.text_area :content %><br>
<% @article.errors.full_messages_for(:content).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :author "Author" %><br>
<%= form.text_area :author %><br>
<% @article.errors.full_messages_for(:author).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :date "Date Authored"%><br>
<%= form.date_select :date, include_blank: true, start_year:1990, end_year: 2024 %><br>
<% @article.errors.full_messages_for(:date).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/articles/edit.html.erb
@@ -0,0 +1,2 @@
<h1>Edit Article</h1>
<%= render 'form', article: @article %>
15 changes: 15 additions & 0 deletions app/views/articles/index.html.erb
@@ -0,0 +1,15 @@
<%= flash[:notice] %>
<h1>Encyclopedia</h1>
<%= form_tag "/articles", method: "GET" do %>
<%= label_tag "Filter articles" %>
<%= text_field_tag :query, params[:query] %>
<%= submit_tag "Search" %>
<% end %>
List of articles:
<ul>
<% @articles.each do |article| %>
<li>
<%= link_to article.title, article %>
</li>
<% end %>
</ul>
2 changes: 2 additions & 0 deletions app/views/articles/new.html.erb
@@ -0,0 +1,2 @@
<h1>New Article</h1>
<%= render 'form', article: @article %>
14 changes: 14 additions & 0 deletions app/views/articles/show.html.erb
@@ -0,0 +1,14 @@
<%= flash[:notice] %>
<h1><%= @article.title %></h1>
<h3><%= @article.author %></h3>
<h4><%= @article.date %></h4>
<p><%= @article.content %></p>

<ul>
<li><%= link_to "Edit this article", edit_article_path(@article) %></li>
<li><%= link_to "Delete this article", article_path(@article), data: {
turbo_method: :delete,
turbo_confirm: "Are you sure? Once deleted, this article cannot be recovered."
} %> </li>
</ul>

6 changes: 3 additions & 3 deletions config/routes.rb
Expand Up @@ -4,7 +4,7 @@
# 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

# Defines the root path route ("/")
# root "posts#index"
root "articles#index"
resources :articles
end
10 changes: 10 additions & 0 deletions db/migrate/20240130012421_create_articles.rb
@@ -0,0 +1,10 @@
class CreateArticles < ActiveRecord::Migration[7.1]
def change
create_table :articles do |t|
t.string :title
t.text :content
t.string :author
t.datetime :date
end
end
end
21 changes: 21 additions & 0 deletions db/schema.rb

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

5 changes: 5 additions & 0 deletions test/controllers/articles_controller_test.rb
@@ -0,0 +1,5 @@
require "test_helper"

class ArticlesControllerTest < ActionDispatch::IntegrationTest

end