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..99447580 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,29 @@ 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 + + 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 new file mode 100644 index 00000000..5ab473a1 --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,35 @@ +require 'pg' + +class Peep + + attr_reader :id, :message + + def initialize(id:, message:) + @id = id + @message = message + end + + def self.all + 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/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/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 new file mode 100644 index 00000000..66b676c0 --- /dev/null +++ b/spec/peep_spec.rb @@ -0,0 +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.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 new file mode 100644 index 00000000..69bf46db --- /dev/null +++ b/views/peeps/index.erb @@ -0,0 +1,7 @@ + \ 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