From c024fa2a5d6757add94f7a25c4943c53259ca533 Mon Sep 17 00:00:00 2001 From: shannon white Date: Fri, 1 Apr 2022 11:54:28 +0100 Subject: [PATCH 1/3] User Story 1 --- .github/pull_request_template.md | 8 ++------ Gemfile | 1 + Gemfile.lock | 1 + app.rb | 11 +++++++++++ lib/peep.rb | 9 +++++++++ spec/peep_spec.rb | 12 ++++++++++++ views/peeps/index.erb | 5 +++++ 7 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 lib/peep.rb create mode 100644 spec/peep_spec.rb create mode 100644 views/peeps/index.erb diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 36884560..eb22012f 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,16 +1,12 @@ # Your name -Please write your full name here to make it easier to find your pull request. +Shannon White # User stories Please list which user stories you've implemented (delete the ones that don't apply). -- [ ] User story 1: "I want to see all the messages (peeps) in a browser" -- [ ] User story 2: "I want to post a message (peep) to chitter" -- [ ] User story 3: "I want to see the date the message was posted" -- [ ] User story 4: "I want to see a list of peeps in reverse chronological order" -- [ ] User story 5: "I want to filter on a specific keyword" +- [x] User story 1: "I want to see all the messages (peeps) in a browser" # README checklist 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..2c1f52ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -84,6 +84,7 @@ GEM PLATFORMS x86_64-darwin-20 + x86_64-darwin-21 DEPENDENCIES capybara diff --git a/app.rb b/app.rb index 2450fb92..b43cb6d6 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,20 @@ require 'sinatra/base' +require 'sinatra/reloader' +require './lib/peep' class Chitter < Sinatra::Base + configure :development do + register Sinatra::Reloader + end + get '/test' do 'Test page' end + get '/peeps' do + @peeps = Peep.all + erb :"peeps/index" + end + run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 00000000..66cdf8ca --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,9 @@ +class Peep + + def self.all + [ + "This is a peep", + "This is another peep", + ] + end +end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb new file mode 100644 index 00000000..58b3b531 --- /dev/null +++ b/spec/peep_spec.rb @@ -0,0 +1,12 @@ +require 'peep' + +describe Peep do + describe '.all' do + it "returns a list of peeps" do + peeps = Peep.all + + expect(peeps).to include("This is a peep") + expect(peeps).to include("This is another peep") + end + end +end diff --git a/views/peeps/index.erb b/views/peeps/index.erb new file mode 100644 index 00000000..15399e45 --- /dev/null +++ b/views/peeps/index.erb @@ -0,0 +1,5 @@ + \ No newline at end of file From 994dde7f67869d98716ac4a479e159a7b25325eb Mon Sep 17 00:00:00 2001 From: shannon white Date: Fri, 1 Apr 2022 12:38:35 +0100 Subject: [PATCH 2/3] refactored with database --- lib/peep.rb | 34 +++++++++++++++++++++++++---- spec/database_helpers.rb | 7 ++++++ spec/features/viewing_peeps_spec.rb | 11 ++++++++++ spec/peep_spec.rb | 22 +++++++++++++++++-- views/peeps/index.erb | 4 +++- 5 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 spec/database_helpers.rb create mode 100644 spec/features/viewing_peeps_spec.rb diff --git a/lib/peep.rb b/lib/peep.rb index 66cdf8ca..fbc42c04 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,9 +1,35 @@ +require 'pg' + class Peep + attr_reader :id, :message + + def initialize(id:, message:) + @id = id + @message = message + end + def self.all - [ - "This is a peep", - "This is another peep", - ] + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + result = connection.exec("SELECT * FROM peeps;") + result.map do |peep| + Peep.new(id: peep['id'], message: peep['message']) + end + end + + def self.create(message:) + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + result = connection.exec("INSERT INTO peeps (message) VALUES('#{message}') RETURNING id, message") + Peep.new(id: result[0]['id'], message: result[0]['message']) 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/viewing_peeps_spec.rb b/spec/features/viewing_peeps_spec.rb new file mode 100644 index 00000000..0bbd8a3f --- /dev/null +++ b/spec/features/viewing_peeps_spec.rb @@ -0,0 +1,11 @@ +require 'pg' + +feature 'Viewing peeps' do + scenario 'A user can see all posted peeps' do + Peep.create(message: 'This is a peep!') + + visit('/peeps') + + expect(page).to have_content 'This is a peep!' + end +end \ No newline at end of file diff --git a/spec/peep_spec.rb b/spec/peep_spec.rb index 58b3b531..66b676c0 100644 --- a/spec/peep_spec.rb +++ b/spec/peep_spec.rb @@ -1,12 +1,30 @@ require 'peep' +require 'database_helpers' describe Peep do + describe '.all' do it "returns a list of peeps" do + peep = Peep.create(message: 'This is a peep!') + Peep.create(message: 'This is another peep') + peeps = Peep.all - expect(peeps).to include("This is a peep") - expect(peeps).to include("This is another peep") + expect(peeps.length).to eq 2 + expect(peeps.first).to be_a Peep + expect(peeps.first.id).to eq peep.id + expect(peeps.first.message).to eq 'This is a peep!' + end + end + + describe '.create' do + it 'creates a new peep' do + peep = Peep.create(message: 'This is a peep!') + persisted = persisted_data(id: peep.id) + + expect(peep).to be_a Peep + expect(peep.id).to eq persisted['id'] + expect(peep.message).to eq 'This is a peep!' end end end diff --git a/views/peeps/index.erb b/views/peeps/index.erb index 15399e45..69bf46db 100644 --- a/views/peeps/index.erb +++ b/views/peeps/index.erb @@ -1,5 +1,7 @@ \ No newline at end of file From 87cc480fbd4816c7d404e71bb0177353cf3fba31 Mon Sep 17 00:00:00 2001 From: shannon white Date: Fri, 1 Apr 2022 13:52:29 +0100 Subject: [PATCH 3/3] user story 2 --- app.rb | 9 +++++++++ lib/peep.rb | 4 ++-- spec/features/creating_peeps_spec.rb | 9 +++++++++ views/peeps/new.erb | 4 ++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 spec/features/creating_peeps_spec.rb create mode 100644 views/peeps/new.erb diff --git a/app.rb b/app.rb index b43cb6d6..99447580 100644 --- a/app.rb +++ b/app.rb @@ -16,5 +16,14 @@ class Chitter < Sinatra::Base erb :"peeps/index" end + get '/peeps/new' do + erb :"peeps/new" + end + + post '/peeps' do + Peep.create(message: params[:message]) + redirect '/peeps' + end + run! if app_file == $0 end diff --git a/lib/peep.rb b/lib/peep.rb index fbc42c04..5ab473a1 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -18,7 +18,7 @@ def self.all result = connection.exec("SELECT * FROM peeps;") result.map do |peep| - Peep.new(id: peep['id'], message: peep['message']) + Peep.new(id: peep['id'], message: peep['message']) end end @@ -28,7 +28,7 @@ def self.create(message:) else connection = PG.connect(dbname: 'chitter') end - + result = connection.exec("INSERT INTO peeps (message) VALUES('#{message}') RETURNING id, message") Peep.new(id: result[0]['id'], message: result[0]['message']) end diff --git a/spec/features/creating_peeps_spec.rb b/spec/features/creating_peeps_spec.rb new file mode 100644 index 00000000..17a47428 --- /dev/null +++ b/spec/features/creating_peeps_spec.rb @@ -0,0 +1,9 @@ +feature 'adding a new peep' do + scenario 'a user can add a new peep' do + visit('/peeps/new') + fill_in('message', with: 'This is another peep') + click_button('Submit') + + expect(page).to have_content 'This is another peep' + end +end \ No newline at end of file diff --git a/views/peeps/new.erb b/views/peeps/new.erb new file mode 100644 index 00000000..f6a3a845 --- /dev/null +++ b/views/peeps/new.erb @@ -0,0 +1,4 @@ +
+ + +
\ No newline at end of file