Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ruby 3.1.2
postgres 14.4
postgres 16.3
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :authenticate_user!


private

end
18 changes: 18 additions & 0 deletions app/controllers/login_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class LoginController < ApplicationController

def create
Rails.logger.info("zzz")
if user = User.authenticate(params[:username], params[:password])
Rails.logger.info(params)
# Save the user ID in the session so it can be used in
# subsequent requests
session[:current_user_id] = user.id
redirect_to root_url
end
end

def delete
Rails.logger.info("zzz1")
session[:current_user_id] = nil
end
end
37 changes: 37 additions & 0 deletions app/controllers/news_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
class NewsController < ApplicationController
# before_action :require_login
helper_method :current_user

def index
all_articles = Article.where(hn_time: 7.days.ago..).limit(100)
@articles = []
@articles, @team_articles = all_articles.partition { |article| article.users.empty? }
@my_articles, @team_articles = @team_articles.partition { |article| article.users.pluck(:id).include?(current_user.id) }
# Cap this
@articles = @articles.slice(0, 50)
end

def star
id = params["id"]
Rails.logger.info(id)
return unless id
return unless current_user
a = Article.find(id)
a.users << current_user
# a.save!
end

def unstar
id = params["id"]
Rails.logger.info(id)
return unless id
return unless current_user
a = Article.find(id)
a.users.delete(current_user)
end

#def current_user
# @current_user = User.first
# @current_user ||= User.find(session[:current_user_id]) if session[:current_user_id]
#end
end
2 changes: 2 additions & 0 deletions app/helpers/login_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LoginHelper
end
2 changes: 2 additions & 0 deletions app/helpers/news_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NewsHelper
end
3 changes: 3 additions & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Article < ApplicationRecord
has_and_belongs_to_many :users
end
2 changes: 2 additions & 0 deletions app/models/liked_article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class LikedArticle < ApplicationRecord
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_and_belongs_to_many :articles

# def authenticate(email)
# User.find(email: email)
# end

end
19 changes: 19 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
<p class="alert">
<%= alert %>
</p>
<p class="login">
<% if current_user %>
<%= "Welcome " + current_user.email + " !" %>
<%= link_to 'Sign out', destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to 'Sign in', new_user_session_path %>
<!-- <%= form_with model:@login do |form| %>
text_field("Sign in")
<%= form.label :login, "Sign in:" %>
<%= form.label :email, "Email" %>
<%= form.text_field :email %>
<%= form.label :password, "Password" %>
<%= form.text_field :password %>
<%= form.submit "Sign in" %>
<% end %>
-->
<!-- <%= link_to 'Sign in', new_login_path, method: :create %> -->
<% end %>
</p>
<%= yield %>
</body>
</html>
34 changes: 34 additions & 0 deletions app/views/news/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<h1>Welcome to Top News</h1>

<h2>Team Articles</h2>
<ul>
<% @team_articles.each do |article| %>
<!-- This is ugly - should be CSS styling instead, here & elsewhere -->
<div><%= button_to "Star", {action: "star", id: article.id, method: :post } %></div>
<div><%= link_to article.title, article.hn_url %></div>
<div><%= article.author.to_s + " " + article.hn_time.to_s %></div>
<div><%= article.users.pluck(:email) %></div>
</li>
<% end %>
</ul>

<h2>Trending Articles</h2>
<ul>
<% @articles.each do |article| %>
<li>
<div><%= button_to "Star", {action: "star", id: article.id, method: :post } %></div>
<div><%= link_to article.title, article.hn_url %></div>
<div><%= article.author.to_s + " " + article.hn_time.to_s %></div>
</li>
<% end %>
</ul>

<h2>My Articles</h2>
<ul>
<% @my_articles.each do |article| %>
<div><%= button_to "Unstar", {action: "unstar", id: article.id, method: :post } %></div>
<div><%= link_to article.title, article.hn_url %></div>
<div><%= article.author.to_s + " " + article.hn_time.to_s %></div>
</li>
<% end %>
</ul>
9 changes: 8 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
Rails.application.routes.draw do
devise_for :users
root to: 'pages#home'
#root to: 'pages#home'

resources :logins, only: [:new, :create, :destroy]

resources :news, only: [:get, :post]
post '/news/star', to: 'news#star'
post '/news/unstar', to: 'news#unstar'
root to: 'news#index'
end
25 changes: 25 additions & 0 deletions db/migrate/20240718202257_create_articles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class CreateArticles < ActiveRecord::Migration[7.0]
def change
create_table :articles do |t|
t.integer :hn_id
t.string :title
t.string :author
t.datetime :hn_time
t.string :hn_url
t.string :hn_type

t.timestamps
end

create_table :articles_users, id: false do |t|
t.bigint :article_id
t.bigint :user_id

t.timestamps
end

add_index :articles_users, :article_id
add_index :articles_users, :user_id
end

end
6 changes: 6 additions & 0 deletions db/migrate/20240718233906_add_article_indices.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddArticleIndices < ActiveRecord::Migration[7.0]
def change
add_index :articles, :hn_id, unique: true
add_index :articles, :hn_time
end
end
24 changes: 23 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,32 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2018_02_28_212101) do
ActiveRecord::Schema[7.0].define(version: 2024_07_18_233906) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "articles", force: :cascade do |t|
t.integer "hn_id"
t.string "title"
t.string "author"
t.datetime "hn_time"
t.string "hn_url"
t.string "hn_type"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["hn_id"], name: "index_articles_on_hn_id", unique: true
t.index ["hn_time"], name: "index_articles_on_hn_time"
end

create_table "articles_users", id: false, force: :cascade do |t|
t.bigint "article_id"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["article_id"], name: "index_articles_users_on_article_id"
t.index ["user_id"], name: "index_articles_users_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
Expand Down
31 changes: 31 additions & 0 deletions lib/tasks/refresh_articles.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace :refresh_articles do

HN_TOP_STORIES_URL = "https://hacker-news.firebaseio.com/v0/topstories.json"

desc "Populate"
task fetch: :environment do
# TODO: Use something fancier like Faraday
resp = Net::HTTP.get_response(URI.parse(HN_TOP_STORIES_URL))
body = resp.body
stories = JSON.parse(body)
stories.each { |id|
next if Article.where(hn_id: id).any?
story_url = "https://hacker-news.firebaseio.com/v0/item/#{id}.json"
resp = Net::HTTP.get_response(URI.parse(story_url))
body = resp.body
details = JSON.parse(body)
a = Article.new(hn_id: id, title: details["title"], author: details["author"],
hn_time: Time.at(details["time"]), hn_url: details["url"], hn_type: details["story"])
a.save!
}

end

desc "Delete old articles"
task erase: :environment do
# TODO: Make this configurable
Article.where(hn_time: ..7.days.ago).delete_all
# TODO: Save starred articles?
end

end
15 changes: 15 additions & 0 deletions spec/helpers/login_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the LoginHelper. For example:
#
# describe LoginHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe LoginHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
15 changes: 15 additions & 0 deletions spec/helpers/news_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the NewsHelper. For example:
#
# describe NewsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe NewsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/article_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Article, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/liked_article_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe LikedArticle, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
7 changes: 7 additions & 0 deletions spec/requests/login_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "Logins", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end
7 changes: 7 additions & 0 deletions spec/requests/news_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rails_helper'

RSpec.describe "News", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end