diff --git a/.tool-versions b/.tool-versions index 6da6866f..64e71382 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1,2 +1,2 @@ ruby 3.1.2 -postgres 14.4 +postgres 16.3 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1c07694e..243be795 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,8 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :authenticate_user! + + + private + end diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb new file mode 100644 index 00000000..d81d8371 --- /dev/null +++ b/app/controllers/login_controller.rb @@ -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 diff --git a/app/controllers/news_controller.rb b/app/controllers/news_controller.rb new file mode 100644 index 00000000..56840cf2 --- /dev/null +++ b/app/controllers/news_controller.rb @@ -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 diff --git a/app/helpers/login_helper.rb b/app/helpers/login_helper.rb new file mode 100644 index 00000000..a0418e32 --- /dev/null +++ b/app/helpers/login_helper.rb @@ -0,0 +1,2 @@ +module LoginHelper +end diff --git a/app/helpers/news_helper.rb b/app/helpers/news_helper.rb new file mode 100644 index 00000000..9877c335 --- /dev/null +++ b/app/helpers/news_helper.rb @@ -0,0 +1,2 @@ +module NewsHelper +end diff --git a/app/models/article.rb b/app/models/article.rb new file mode 100644 index 00000000..19f1c84f --- /dev/null +++ b/app/models/article.rb @@ -0,0 +1,3 @@ +class Article < ApplicationRecord + has_and_belongs_to_many :users +end diff --git a/app/models/liked_article.rb b/app/models/liked_article.rb new file mode 100644 index 00000000..06575abe --- /dev/null +++ b/app/models/liked_article.rb @@ -0,0 +1,2 @@ +class LikedArticle < ApplicationRecord +end diff --git a/app/models/user.rb b/app/models/user.rb index b2091f9a..e2a6cb37 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 331a7ed0..3c77dd58 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,25 @@
<%= alert %>
++ <% 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 %> + + + <% end %> +
<%= yield %>