diff --git a/Gemfile b/Gemfile index 99d8e519..d0d971e8 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'sinatra-contrib' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..89504308 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -16,8 +16,11 @@ GEM diff-lcs (1.4.4) docile (1.4.0) mini_mime (1.1.1) + multi_json (1.15.0) mustermann (1.1.1) ruby2_keywords (~> 0.0.1) + nokogiri (1.12.3-arm64-darwin) + racc (~> 1.4) nokogiri (1.12.3-x86_64-darwin) racc (~> 1.4) parallel (1.20.1) @@ -75,6 +78,12 @@ GEM rack (~> 2.2) rack-protection (= 2.1.0) tilt (~> 2.0) + sinatra-contrib (2.1.0) + multi_json + mustermann (~> 1.0) + rack-protection (= 2.1.0) + sinatra (= 2.1.0) + tilt (~> 2.0) terminal-table (3.0.1) unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) @@ -83,6 +92,7 @@ GEM nokogiri (~> 1.8) PLATFORMS + arm64-darwin-21 x86_64-darwin-20 DEPENDENCIES @@ -93,6 +103,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + sinatra-contrib RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb index 2450fb92..31fe9ea0 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,39 @@ require 'sinatra/base' +require './lib/peeps' class Chitter < Sinatra::Base get '/test' do 'Test page' end + get '/' do + 'Welcome to Chitter!' + erb :index + end + + get '/peeps' do + @peeps = Peeps.all + erb :peeps + end + + get '/peeps/new' do + erb :new_peep + end + + post '/peeps' do + Peeps.create(message: params[:message], entry_date: params[:entry_date]) + redirect '/peeps' + end + + get '/peeps/reverse' do + @peeps = Peeps.reverse_chronology + erb :peeps_reverse + end + + get '/peeps/filter' do + @filtered_peeps = Peeps.filter(params[:filter]) + erb :peeps_filter + end + run! if app_file == $0 end diff --git a/chitter_image.jpeg b/chitter_image.jpeg new file mode 100644 index 00000000..f9d08fa6 Binary files /dev/null and b/chitter_image.jpeg differ diff --git a/db/migrations/01_add_date_column.sql b/db/migrations/01_add_date_column.sql new file mode 100644 index 00000000..8c0031f1 --- /dev/null +++ b/db/migrations/01_add_date_column.sql @@ -0,0 +1 @@ +ALTER TABLE peeps ADD entry_date date; \ No newline at end of file diff --git a/lib/peeps.rb b/lib/peeps.rb new file mode 100644 index 00000000..e86de18d --- /dev/null +++ b/lib/peeps.rb @@ -0,0 +1,57 @@ +require 'pg' + +class Peeps + + attr_reader :id, :message, :entry_date, :filter + + def initialize(id:, message:, entry_date:) + @id = id + @message = message + @entry_date = entry_date + end + + def self.open_connection + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + end + + def self.row_to_peep(row) + Peeps.new(id: row['id'], message: row['message'], entry_date: row['entry_date']) + end + + def self.all + connection = open_connection + + result = connection.exec("SELECT * FROM peeps;") + result.map do |peep| + row_to_peep(peep) + end + end + + def self.create(message:, entry_date:) + connection = open_connection + + result = connection.exec_params("INSERT INTO peeps (message, entry_date) VALUES($1, $2) RETURNING id, message, entry_date;", [message, entry_date]) + row_to_peep(result[0]) + end + + def self.reverse_chronology + connection = open_connection + + result = connection.exec("SELECT * FROM peeps ORDER BY entry_date DESC;") + result.map do |peep| + row_to_peep(peep) + end + end + + def self.filter(filter) + connection = open_connection + result = connection.exec("SELECT * FROM peeps WHERE lower(message) LIKE '%#{filter.downcase}%';") + result.map do |peep| + row_to_peep(peep) + end + end +end \ No newline at end of file diff --git a/spec/database_helpers.rb b/spec/database_helpers.rb new file mode 100644 index 00000000..2bb1ec76 --- /dev/null +++ b/spec/database_helpers.rb @@ -0,0 +1,7 @@ +require 'pg' + +def persisted_data(id:) + connection = PG.connect(dbname: 'chitter_test') + result = connection.query("SELECT * FROM peeps WHERE id = #{id};") + result.first +end \ No newline at end of file diff --git a/spec/features/creating_peeps_spec.rb b/spec/features/creating_peeps_spec.rb new file mode 100644 index 00000000..a4b2b76d --- /dev/null +++ b/spec/features/creating_peeps_spec.rb @@ -0,0 +1,15 @@ +feature 'Creating peeps' do + scenario 'user can add peeps and dates' do + setup_test_database + + Peeps.create(message: 'Test peep!', entry_date: "2021-03-18") + Peeps.create(message: 'Second test peep!', entry_date: "2021-02-15") + + visit ('/peeps') + + expect(page).to have_content('Test peep!') + expect(page).to have_content('Second test peep!') + expect(page).to have_content("2021-03-18") + expect(page).to have_content("2021-02-15") + end +end \ No newline at end of file diff --git a/spec/features/filter_peeps_spec.rb b/spec/features/filter_peeps_spec.rb new file mode 100644 index 00000000..7135c2d8 --- /dev/null +++ b/spec/features/filter_peeps_spec.rb @@ -0,0 +1,15 @@ +feature 'Filtering peeps' do + scenario 'user can filter peeps by keyword' do + setup_test_database + + Peeps.create(message: 'Test peep!', entry_date: "2021-03-18") + Peeps.create(message: 'Second test peep!', entry_date: "2021-02-15") + + visit ('/peeps') + fill_in('filter', with: 'peep') + click_button "Filter" + + expect(page).to have_content("Test peep!") + expect(page).to have_content("Second test peep!") + end +end \ No newline at end of file diff --git a/spec/features/reverse_chronological_order_spec.rb b/spec/features/reverse_chronological_order_spec.rb new file mode 100644 index 00000000..b5a8a850 --- /dev/null +++ b/spec/features/reverse_chronological_order_spec.rb @@ -0,0 +1,14 @@ +feature 'Reverse chronological peeps' do + scenario 'user can see most recent peeps first' do + setup_test_database + + Peeps.create(message: 'Test peep!', entry_date: "2021-03-18") + Peeps.create(message: 'Second test peep!', entry_date: "2021-02-15") + + visit ('/peeps') + + click_button "View Newest First" + + page.body.index("2021-03-18").should < page.body.index("2021-02-15") + end +end \ No newline at end of file diff --git a/spec/features/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb new file mode 100644 index 00000000..8a200986 --- /dev/null +++ b/spec/features/viewing_peeps_spec.rb @@ -0,0 +1,13 @@ +feature 'Viewing peeps' do + scenario 'user can view all peeps' do + setup_test_database + + Peeps.create(message: 'Test peep!', entry_date: "2021-03-18") + Peeps.create(message: 'Second test peep!', entry_date: "2021-02-15") + + visit ('/peeps') + + expect(page).to have_content('Test peep!') + expect(page).to have_content('Second test peep!') + end +end \ No newline at end of file diff --git a/spec/peeps_spec.rb b/spec/peeps_spec.rb new file mode 100644 index 00000000..1938c936 --- /dev/null +++ b/spec/peeps_spec.rb @@ -0,0 +1,44 @@ +require 'peeps' + +describe '.all' do + it 'returns a list of peeps' do + setup_test_database + + chitter = Peeps.create(message: "Test peep!", entry_date: "2021-02-15") + Peeps.create(message: "Second test peep!", entry_date: "2021-02-13") + Peeps.create(message: "Third test peep!", entry_date: "2021-10-16") + + chitter = Peeps.all + + expect(chitter.length).to eq 3 + expect(chitter.first).to be_a Peeps + expect(chitter.first.message).to eq "Test peep!" + expect(chitter.first.entry_date).to eq "2021-02-15" + end +end + +require 'database_helpers' + +describe '.create' do + it 'creates a new peep' do + chitter = Peeps.create(message: "Test peep!", entry_date: "2021-02-15") + persisted_data = persisted_data(id: chitter.id) + + expect(chitter).to be_a Peeps + expect(chitter.id).to eq persisted_data['id'] + expect(chitter.message).to eq "Test peep!" + expect(chitter.entry_date).to eq "2021-02-15" + end +end + +describe '.filter' do + it 'filters peeps by keyword' do + chitter = Peeps.create(message: "Test peep!", entry_date: "2021-02-15") + Peeps.create(message: "Second test peep!", entry_date: "2021-02-13") + Peeps.create(message: "Third say hello!", entry_date: "2021-10-16") + + filtered_results = Peeps.filter("Peep") + expect(filtered_results[0].message).to eq "Test peep!" + expect(filtered_results[1].message).to eq "Second test peep!" + end +end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..e40811df --- /dev/null +++ b/views/index.erb @@ -0,0 +1,2 @@ +
<%= peep.message %>+ <%= peep.entry_date %> +
<%= peep.message %>+ <%= peep.entry_date %> +
<%= peep.message %>+ <%= peep.entry_date %> +