diff --git a/db/migrate/20250321234644_create_users.rb b/db/migrate/20250321234644_create_users.rb
new file mode 100644
index 0000000..7367989
--- /dev/null
+++ b/db/migrate/20250321234644_create_users.rb
@@ -0,0 +1,9 @@
+class CreateUsers < ActiveRecord::Migration[7.2]
+ def change
+ create_table :users do |t|
+ t.string :email
+ t.string :password
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index ca192cc..c61f9f7 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.2].define(version: 2025_03_21_224552) do
+ActiveRecord::Schema[7.2].define(version: 2025_03_21_234644) do
create_table "candidates", force: :cascade do |t|
t.integer "vote_id"
t.string "name"
@@ -20,6 +20,13 @@ ActiveRecord::Schema[7.2].define(version: 2025_03_21_224552) do
t.index ["vote_id"], name: "index_candidates_on_vote_id"
end
+ create_table "users", force: :cascade do |t|
+ t.string "email"
+ t.string "password"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
create_table "votes", force: :cascade do |t|
t.string "secure_id"
t.string "title"
diff --git a/views/login.erb b/views/login.erb
index dc175d7..ebd8a03 100644
--- a/views/login.erb
+++ b/views/login.erb
@@ -3,7 +3,14 @@
<%= @error %>
<% end %>
+Create account
diff --git a/views/signup.erb b/views/signup.erb
new file mode 100644
index 0000000..72e4551
--- /dev/null
+++ b/views/signup.erb
@@ -0,0 +1,13 @@
+Create account
+
+
diff --git a/views/votes_show.erb b/views/votes_show.erb
index 56cbde3..3d3263f 100644
--- a/views/votes_show.erb
+++ b/views/votes_show.erb
@@ -2,6 +2,8 @@
<%= @vote.description %>
+Hello, <%= current_user.email %>.
+
Candidates
diff --git a/vote.rb b/vote.rb
index 7322445..b67954f 100644
--- a/vote.rb
+++ b/vote.rb
@@ -15,6 +15,9 @@ class Candidate < ActiveRecord::Base
belongs_to :vote
end
+class User < ActiveRecord::Base
+end
+
def hash_password(password)
BCrypt::Password.create(password).to_s
end
@@ -23,13 +26,6 @@ def verify_password(password, hash)
BCrypt::Password.new(hash) == password
end
-User = Struct.new(:id, :email, :password_hash)
-USERS = [
- User.new(1, 'P1', hash_password('P1')),
- User.new(2, 'P2', hash_password('P2')),
- User.new(3, 'P3', hash_password('P3')),
-]
-
enable :sessions
get '/' do
@@ -38,13 +34,23 @@ get '/' do
erb :home
end
+get '/signup' do
+ erb :signup
+end
+
+post '/signup' do
+ @user = User.create(email: params[:email],
+ password: hash_password(params[:password]))
+ redirect '/'
+end
+
get '/login' do
erb :login
end
post '/login' do
- user = USERS.find { |u| u.email == params[:email] }
- if user && verify_password(params[:password], user.password_hash)
+ user = User.find_by(email: params[:email])
+ if user && verify_password(params[:password], user.password)
session.clear
session[:user_id] = user.id
redirect '/'
@@ -64,6 +70,7 @@ get '/votes/new' do
end
get '/votes/:id' do
+ redirect '/login' unless current_user
@vote = Vote.find(params[:id])
erb :votes_show
end
@@ -84,10 +91,23 @@ post '/votes/:id/candidates' do
redirect '/votes/' + @vote.secure_id
end
+post '/votes/:id/ratings' do
+ redirect '/login' unless current_user
+ vote = Vote.find(params[:id])
+ vote.candidates.each do |candidate|
+ rating = Rating.find_by(user: current_user)
+ rating = Rating.find_by(candidate: candidate)
+ rating = Rating.find_or_initialize_by(user: current_user, candidate: candidate)
+ rating.value = params[candidate.id.to_s]
+ rating.save
+ end
+ redirect '/votes/' + vote.secure_id
+end
+
helpers do
def current_user
if session[:user_id]
- USERS.find { |u| u.id == session[:user_id] }
+ User.find(session[:user_id])
else
nil
end