diff --git a/Gemfile b/Gemfile index 99d8e519..99615d01 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,7 @@ ruby '3.0.2' gem 'pg' gem 'sinatra' +gem 'webrick' group :test do gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index 7d4eb449..2cba3902 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,11 +79,13 @@ GEM unicode-display_width (>= 1.1.1, < 3) tilt (2.0.10) unicode-display_width (2.0.0) + webrick (1.7.0) xpath (3.2.0) nokogiri (~> 1.8) PLATFORMS x86_64-darwin-20 + x86_64-darwin-21 DEPENDENCIES capybara @@ -93,6 +95,7 @@ DEPENDENCIES simplecov simplecov-console sinatra + webrick RUBY VERSION ruby 3.0.2p107 diff --git a/README.md b/README.md index f9638b66..bc1947ca 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ You should see 1 passing test. ## User stories + ``` As a Maker So that I can see what people are doing @@ -41,11 +42,17 @@ I want to see all the messages (peeps) in a browser ``` + +![alt text](story1diagram.png "See all messages diagram") + ``` As a Maker So that I can let people know what I am doing I want to post a message (peep) to chitter ``` +# Second Story diagram + +![alt text](story2diagram.png "Post a message (peep) diagram") ``` As a Maker @@ -54,6 +61,13 @@ I want to see the date the message was posted ``` (Hint the database table will need to change to store the date too) +# To do - Date of message + +* to faciltate time of peep add column to the databases(dev and test) +* Update Readme with how to set up database tables to include date and time column +* add date and time to tests +* working through the tests, amend the app.rb and new peep files to capture the date and time of peeps + ``` As a Maker So that I can easily see the latest peeps @@ -64,3 +78,6 @@ As a Maker So that I can find relevant peeps I want to filter on a specific keyword ``` + + + diff --git a/app.rb b/app.rb index 2450fb92..acd8e804 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,34 @@ require 'sinatra/base' +require './lib/peeps' +require 'time' class Chitter < Sinatra::Base get '/test' do 'Test page' end + get '/' do + @peeps = Peeps.all + p @peeps + erb :'index' + end + + get '/new' do + erb :'new' + end + + post '/' do + message = params['message'] + p params['message'] + time = Time.now.httpdate + connection = PG.connect(dbname: 'chitter') + connection.exec("INSERT INTO peeps (message, posted) VALUES('#{message}', '#{time}')") + redirect '/' + end + + + + + run! if app_file == $0 end diff --git a/db/migrations/01_create_chitter_table.sql b/db/migrations/01_create_chitter_table.sql index 6e077248..ba599894 100644 --- a/db/migrations/01_create_chitter_table.sql +++ b/db/migrations/01_create_chitter_table.sql @@ -1 +1 @@ -CREATE TABLE peeps(id SERIAL PRIMARY KEY, message VARCHAR(60)); +CREATE TABLE peeps(id SERIAL PRIMARY KEY, message VARCHAR(60), posted VARCHAR(60)); diff --git a/diagram.png b/diagram.png new file mode 100644 index 00000000..89d7b3fc Binary files /dev/null and b/diagram.png differ diff --git a/lib/peeps.rb b/lib/peeps.rb new file mode 100644 index 00000000..c97364b0 --- /dev/null +++ b/lib/peeps.rb @@ -0,0 +1,18 @@ + +require 'pg' +require 'time' + +class Peeps + def self.all + if ENV['ENVIRONMENT'] == 'test' + connection = PG.connect(dbname: 'chitter_test') + else + connection = PG.connect(dbname: 'chitter') + end + + result = connection.exec('SELECT message, posted FROM peeps') + # binding.irb + result.map { |peep| peep } + + end +end \ No newline at end of file diff --git a/public/chitter.png b/public/chitter.png new file mode 100644 index 00000000..456d7ae7 Binary files /dev/null and b/public/chitter.png differ diff --git a/spec/features/new_peep_spec.rb b/spec/features/new_peep_spec.rb new file mode 100644 index 00000000..6dafa720 --- /dev/null +++ b/spec/features/new_peep_spec.rb @@ -0,0 +1,11 @@ +# in spec/features/creating_bookmarks_spec.rb + +feature 'Post a new peep' do + scenario 'A user can post a new peep to chitter' do + visit('/new') + fill_in('message', with: 'hey peeps. just a peep to say I am posting a new peep. so peep this!') + click_button('post your peep') + + expect(page).to have_content 'hey peeps. just a peep to say I am posting a new peep. so peep this!' + end +end \ No newline at end of file diff --git a/spec/features/view_peeps_spec.rb b/spec/features/view_peeps_spec.rb new file mode 100644 index 00000000..f269c2f8 --- /dev/null +++ b/spec/features/view_peeps_spec.rb @@ -0,0 +1,15 @@ +feature 'Viewing peeps' do + scenario 'A user can see peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps VALUES(1, 'Just completed SQL Zoo. It was fun to do!');") + connection.exec("INSERT INTO peeps VALUES(2, 'Testing adding chitter messages');") + connection.exec("INSERT INTO peeps VALUES(3, 'testing chitter.');") + + visit('/') + expect(page).to have_content "Just completed SQL Zoo. It was fun to do!" + expect(page).to have_content "Testing adding chitter messages" + expect(page).to have_content "testing chitter." + 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..7b2e23cd --- /dev/null +++ b/spec/peeps_spec.rb @@ -0,0 +1,28 @@ +# in spec/peeps_spec.rb + +require 'peeps' + +describe Peeps do + describe '.all' do + it 'returns all peeps' do + connection = PG.connect(dbname: 'chitter_test') + + # Add the test data + connection.exec("INSERT INTO peeps VALUES(1, 'Just completed SQL Zoo. It was fun to do!');") + connection.exec("INSERT INTO peeps VALUES(2, 'Testing adding chitter messages');") + connection.exec("INSERT INTO peeps VALUES(3, 'testing chitter.');") + + + peeps = Peeps.all + + expect(peeps).to include "Just completed SQL Zoo. It was fun to do!" + expect(peeps).to include "Testing adding chitter messages" + expect(peeps).to include "testing chitter." + end + end + end + + + + + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 74046cad..3a8fdf6a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,7 +29,7 @@ ]) SimpleCov.start -ENV['RACK_ENV'] = 'test' +# ENV['RACK_ENV'] = 'test' ENV['ENVIRONMENT'] = 'test' # Bring in the contents of the `app.rb` file. The below is equivalent to: require_relative '../app.rb' diff --git a/story1diagram.png b/story1diagram.png new file mode 100644 index 00000000..9242aeed Binary files /dev/null and b/story1diagram.png differ diff --git a/story2diagram.png b/story2diagram.png new file mode 100644 index 00000000..eb008151 Binary files /dev/null and b/story2diagram.png differ diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 00000000..222f20b8 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/views/new.erb b/views/new.erb new file mode 100644 index 00000000..2eacb1be --- /dev/null +++ b/views/new.erb @@ -0,0 +1,8 @@ + + +
+ + +
+ + \ No newline at end of file