-
Notifications
You must be signed in to change notification settings - Fork 228
Laura's Chitter Challenge #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lauracabtay
wants to merge
11
commits into
makersacademy:main
Choose a base branch
from
lauracabtay:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
49ed70e
Show all messages
lauracabtay f0dc210
Connect to chitter db
lauracabtay 7948ca8
Set up testing environment
lauracabtay 586a240
Show user own peeps and allow user to post
lauracabtay d079e50
Add timestamp to messages
lauracabtay 5e3156c
Display peeps in reverse chronological order
lauracabtay 333f4b3
Attempt to filter peeps by keyword
lauracabtay 416f8b4
Style the keyword search box and button
lauracabtay 780a3c5
Update Readme.md
lauracabtay 4b81357
Correct filter feature
lauracabtay c5b84d3
Tidy up code as per review
lauracabtay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ ruby '3.0.2' | |
|
|
||
| gem 'pg' | ||
| gem 'sinatra' | ||
| gem 'webrick' | ||
|
|
||
|
|
||
| group :test do | ||
| gem 'capybara' | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,42 @@ | ||
| require 'sinatra/base' | ||
| require './lib/peep' | ||
|
|
||
|
|
||
| class Chitter < Sinatra::Base | ||
|
|
||
| get '/test' do | ||
| 'Test page' | ||
| end | ||
|
|
||
| get '/' do | ||
| 'Chitter' | ||
| end | ||
|
|
||
| get '/chitter' do | ||
| @peeps = Peep.all | ||
| erb :chitter | ||
| end | ||
|
|
||
| get '/my_peeps' do | ||
| @peeps = Peep.own_peeps | ||
| erb :my_peeps | ||
| end | ||
|
|
||
| get '/my_peeps/new_peep' do | ||
| erb :post_peep | ||
| end | ||
|
|
||
| post '/my_peeps/create_peep' do | ||
| @peeps_posted = Peep.post(message: params[:message], author_id: params[:author_id]) | ||
| redirect ('/my_peeps') | ||
| end | ||
|
|
||
| post '/chitter/search' do | ||
| @keyword = params[:filter] | ||
| p params #Just to check if keyword comes through | ||
|
lauracabtay marked this conversation as resolved.
Outdated
|
||
| @peeps_filtered = Peep.search(keyword: params[:filter]) | ||
| erb :filtered_results | ||
| end | ||
|
|
||
| run! if app_file == $0 | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| CREATE TABLE peeps(id SERIAL PRIMARY KEY, message VARCHAR(60)); | ||
| CREATE TABLE peeps( | ||
| id SERIAL PRIMARY KEY, | ||
| message VARCHAR(60), | ||
| author_id | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| CREATE TABLE author(id SERIAL PRIMARY KEY, name VARCHAR(60)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ALTER TABLE peeps | ||
| ADD created_at VARCHAR(60) DEFAULT to_char(NOW(), 'On dd-MM-yyyy at HH24:MI'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('This is a peep', 2); | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('Hello world!', 1); | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('What a beautiful day today!', 1); | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('This Chitter thing is great you know!', 2); | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('This is a peep', 3); | ||
| INSERT INTO peeps (message, author_id) | ||
| VALUES ('This is another peep', 2); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| INSERT INTO author (id, name) | ||
| VALUES (1, 'Me'); | ||
| INSERT INTO author (id, name) | ||
| VALUES (2, 'The Real Donald Trump'); | ||
| INSERT INTO author (id, name) | ||
| VALUES (3, 'Joe Blogg'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Database | ||
|
|
||
| def self.setup(db_name) | ||
| @connection = PG.connect :dbname => db_name | ||
| end | ||
|
|
||
| def self.current_connection | ||
| @connection | ||
| end | ||
|
|
||
| def self.query(sql) | ||
| @connection.exec(sql) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| require 'pg' | ||
| require './lib/database' | ||
|
|
||
| class Peep | ||
|
|
||
| attr_reader :id, :message, :name, :created_at | ||
|
|
||
| def initialize(id:, message:, name:, created_at:) | ||
| @id = id | ||
| @message = message | ||
| @name = name | ||
| @created_at = created_at | ||
| end | ||
|
|
||
| def self.all | ||
| if ENV['ENVIRONMENT'] == 'test' | ||
|
lauracabtay marked this conversation as resolved.
Outdated
|
||
| connection = PG.connect(dbname: 'chitter_test') | ||
| else | ||
| connection = PG.connect(dbname: 'chitter') | ||
| end | ||
| result = connection.exec( | ||
| "SELECT peeps.id, peeps.message, author.name, peeps.created_at | ||
| FROM peeps JOIN author ON peeps.author_id = author.id | ||
| WHERE author.id != 1 | ||
| ORDER BY peeps.created_at DESC;") | ||
| result.map { |peep| | ||
| Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) | ||
| } | ||
| end | ||
|
|
||
| def self.own_peeps | ||
| if ENV['ENVIRONMENT'] == 'test' | ||
| connection = PG.connect(dbname: 'chitter_test') | ||
| else | ||
| connection = PG.connect(dbname: 'chitter') | ||
| end | ||
| result = connection.exec(" | ||
| SELECT peeps.id, peeps.message, author.name, peeps.created_at | ||
| FROM peeps JOIN author ON peeps.author_id = author.id | ||
| WHERE author_id = 1 | ||
| ORDER BY peeps.created_at DESC;") | ||
| result.map { |peep| | ||
| Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) | ||
| } | ||
| end | ||
|
|
||
| def self.post(message:, author_id:) | ||
| if ENV['ENVIRONMENT'] == 'test' | ||
| connection = PG.connect(dbname: 'chitter_test') | ||
| else | ||
| connection = PG.connect(dbname: 'chitter') | ||
| end | ||
| result = connection.exec_params( | ||
| "INSERT INTO peeps (message, author_id) | ||
| VALUES($1, $2) RETURNING id, message, author_id;", [message, 1]) | ||
| Peep.new(id: result[0]['id'], message: result[0]['message'], name: result[0]['name'], created_at: result[0]['created_at']) | ||
| end | ||
|
|
||
|
|
||
| def self.search(keyword:) | ||
| if ENV['ENVIRONMENT'] == 'test' | ||
| connection = PG.connect(dbname: 'chitter_test') | ||
| else | ||
| connection = PG.connect(dbname: 'chitter') | ||
| end | ||
| result = connection.exec( | ||
| "SELECT peeps.id, peeps.message, author.name, peeps.created_at | ||
| FROM peeps JOIN author ON peeps.author_id = author.id | ||
| WHERE author.id != 1 AND peeps.message LIKE '%#{keyword}%' | ||
| ORDER BY peeps.created_at DESC;") | ||
| result.map { |peep| | ||
| Peep.new(id: peep['id'], message: peep['message'], name: peep['name'], created_at: peep['created_at']) | ||
| } | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| feature 'Posting a peep' do | ||
| scenario 'A user can post a peep' do | ||
| visit('/chitter') | ||
| click_button('My Peeps') | ||
| click_button('New Peep') | ||
| fill_in('message', with: 'I am posting for the first time!') | ||
| click_button('Post') | ||
|
|
||
| expect(page).to have_content 'I am posting for the first time!' | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| feature "filter peeps" do | ||
| scenario "A user can search peeps by keywords" do | ||
| connection = PG.connect(dbname: 'chitter_test') | ||
|
|
||
| # Add the test data | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") | ||
|
|
||
| visit('/chitter') | ||
| fill_in('filter', with: 'peep') | ||
| click_button('Filter') | ||
|
|
||
| expect(page).not_to have_content("Hello world!") | ||
| expect(page).to have_content("Hi Chitter! This is my first peep!") | ||
| expect(page).to have_content("This is a peep!") | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| require './app' | ||
| require 'pg' | ||
|
|
||
| RSpec.describe "Peeps" do | ||
| feature "view peeps" do | ||
| scenario "user can view all peeps in their browser" do | ||
|
|
||
| connection = PG.connect(dbname: 'chitter_test') | ||
|
|
||
| # Add the test data | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") | ||
|
|
||
| visit('/chitter') | ||
|
|
||
| expect(page).to have_content("Hello world!") | ||
| expect(page).to have_content("Hi Chitter! This is my first peep!") | ||
| expect(page).to have_content("This is a peep!") | ||
| expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") | ||
| end | ||
| end | ||
|
|
||
| feature "view my own peeps" do | ||
| scenario "user can view their own peeps in their browser" do | ||
| Peep.post(message: "Cannot believe it is snowing in April", author_id: 1) | ||
| Peep.post(message: "Happy Friday everyone!", author_id: 1) | ||
| Peep.post(message: "Looking forward to the weekend!", author_id: 1) | ||
|
|
||
| visit('/chitter') | ||
| click_button('My Peeps') | ||
|
|
||
| expect(page).to have_content("Cannot believe it is snowing in April") | ||
| expect(page).to have_content("Looking forward to the weekend!") | ||
| expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") | ||
| end | ||
| end | ||
|
|
||
| feature "back to all peeps from my peeps" do | ||
| scenario "user can go back to all peeps from their peeps" do | ||
| connection = PG.connect(dbname: 'chitter_test') | ||
|
|
||
| # Add the test data | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES ('Hello world!', 3);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('Hi Chitter! This is my first peep!', 2);") | ||
| connection.exec("INSERT INTO peeps (message, author_id) VALUES('This is a peep!', 2);") | ||
|
|
||
| visit('/chitter') | ||
| click_button('My Peeps') | ||
| click_button('All Peeps') | ||
|
|
||
| expect(page).to have_content("Hello world!") | ||
| expect(page).to have_content("Hi Chitter! This is my first peep!") | ||
| expect(page).to have_content("This is a peep!") | ||
| expect(page).to have_content Time.now.strftime("On %d-%m-%Y at %H:%M") | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.