diff --git a/Gemfile b/Gemfile index a963765..91a092f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,9 @@ source "http://rubygems.org" -gem "sinatra" -gem "bcrypt" +gem 'sinatra' +gem 'sinatra-activerecord' +gem 'rake' +gem 'rackup' +gem 'puma' +gem 'sqlite3' +gem 'bcrypt' diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..653dd46 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require './vote' +require 'sinatra/activerecord/rake' diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000..11a2f6a --- /dev/null +++ b/config/database.yml @@ -0,0 +1,3 @@ +development: + adapter: sqlite3 + database: db/vote.sqlite3 diff --git a/db/migrate/20250321213011_create_votes.rb b/db/migrate/20250321213011_create_votes.rb new file mode 100644 index 0000000..321e58f --- /dev/null +++ b/db/migrate/20250321213011_create_votes.rb @@ -0,0 +1,11 @@ +class CreateVotes < ActiveRecord::Migration[7.2] + def change + create_table :votes do |t| + t.string :secure_id + t.string :title + t.text :description + t.datetime :expire_on + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..8245e87 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,22 @@ +# 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.2].define(version: 2025_03_21_213011) do + create_table "votes", force: :cascade do |t| + t.string "secure_id" + t.string "title" + t.text "description" + t.datetime "expire_on" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end +end diff --git a/views/home.erb b/views/home.erb index e0fb120..962ec6b 100644 --- a/views/home.erb +++ b/views/home.erb @@ -1,5 +1,19 @@

Home

+

Hello, <%= current_user.email %>.

-
- + +

Votes

+ +<% @votes.each do |vote| %> +
  • + <%= vote.title %> +
  • +<% end %> + + + +
    + +
    +
    diff --git a/views/votes_new.erb b/views/votes_new.erb new file mode 100644 index 0000000..3a6a07e --- /dev/null +++ b/views/votes_new.erb @@ -0,0 +1,13 @@ +

    New vote

    + +
    +

    + + +

    +

    + + +

    + +
    diff --git a/views/votes_show.erb b/views/votes_show.erb new file mode 100644 index 0000000..480b3f7 --- /dev/null +++ b/views/votes_show.erb @@ -0,0 +1,3 @@ +

    <%= @vote.title %>

    + +

    <%= @vote.description %>

    diff --git a/vote.rb b/vote.rb index 690eb40..ead780e 100644 --- a/vote.rb +++ b/vote.rb @@ -1,5 +1,14 @@ +require 'bundler/setup' require 'sinatra' +require 'sinatra/activerecord' require 'bcrypt' +require 'securerandom' + +#set :database, 'sqlite3:db/vote.sqlite3' + +class Vote < ActiveRecord::Base + self.primary_key = "secure_id" +end def hash_password(password) BCrypt::Password.create(password).to_s @@ -19,11 +28,9 @@ USERS = [ enable :sessions get '/' do - if current_user - erb :home - else - redirect '/login' - end + redirect '/login' unless current_user + @votes = Vote.all + erb :home end get '/login' do @@ -31,7 +38,6 @@ get '/login' do end post '/login' do - puts params user = USERS.find { |u| u.email == params[:email] } if user && verify_password(params[:password], user.password_hash) session.clear @@ -48,6 +54,23 @@ post '/logout' do redirect '/login' end +get '/votes/new' do + erb :votes_new +end + +get '/votes/:id' do + @vote = Vote.find(params[:id]) + erb :votes_show +end + +post '/votes' do + secure_id = SecureRandom.hex(8) + @vote = Vote.create(secure_id: secure_id, + title: params[:title], + description: params[:description]) + redirect '/votes/' + secure_id +end + helpers do def current_user if session[:user_id]