Skip to content
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

Laura's Chitter Challenge #217

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ ruby '3.0.2'

gem 'pg'
gem 'sinatra'
gem 'webrick'


group :test do
gem 'capybara'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ 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)

Expand All @@ -93,6 +94,7 @@ DEPENDENCIES
simplecov
simplecov-console
sinatra
webrick

RUBY VERSION
ruby 3.0.2p107
Expand Down
51 changes: 25 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
## Chitter Challenge

* Challenge time: until the end of the day
* Feel free to use google, your notes, books etc but please work on your own
* Please raise a pull request when you start this challenge, and keep pushing updates as and when you make commits throughout the day
* There is _no expectation_ to finish all or any of the user stories, please use this time to reflect on where you feel you are with the skill and what may support your learning.
* If you get blocked, please reflect on what blocked you and any strategies you adopted that helped you make progress.
## CHITTER CHALLENGE

We are going to write a small Twitter clone that will allow the users to post messages to a public stream.

## Set up
## SETTING UP THE DATABASE

To setup the database:
1. Connect to `psql` (you may need to install postgresql first by running `brew install postgres`)
2. Create the database using the psql command `CREATE DATABASE chitter`
3. Connect to the database using the pqsl command `\c chitter`
4. Run the query we have saved in the file **01_create_chitter_table.sql**
5. Run the query we have saved in the file **02_create_author_table.sql**
6. Run the query we have saved in the file **03_add_timestamp_to_peeps.sql**
7. Populate your table by running the queries saved in the file **04_insert_data_to_peeps.sql** and **05_insert_data_to_author.sql**

* Connect to psql
* Create the database using the psql command `CREATE DATABASE chitter;`
* Connect to the database using the psql command `\c chitter`;
* Run the query we have saved in the file 01_create_chitter_table.sql
* Populate your table with a row by running `INSERT INTO peeps (message) values ('This is a peep!');`
To check you have everything set up ok, please take a look at the peeps table and the author inside the chitter database.

To check you have everything set up ok, please take a look at the peeps table inside the chitter database. You should see one row in there.
## SETTING UP THE TEST DATABASE

To setup the test database:
* Connect to psql
* Create the database using the psql
command `CREATE DATABASE chitter_test;`;
* Connect to the database using the psql command `\c chitter_test`
* Run the query we have saved in the file 01_create_chitter_table.sql
1. Connect to `psql`
2. Create the database using the psql command `CREATE DATABASE chitter_test`
3. Connect to the database using the pqsl command `\c chitter_test`
4. Run the query we have saved in the file **01_create_chitter_table.sql**
5. Run the query we have saved in the file **02_create_author_table.sql**
6. Run the query we have saved in the file **03_add_timestamp_to_peeps.sql**

* `bundle install`
* `rspec`
## START THE APP

You should see 1 passing test.
- Run `bundle install` to add predefined package dependencies
- Run `rackup` and run the app on localhost 9292
- Run `rspec` to test code

## User stories
## USER STORIES

```
As a Maker
Expand All @@ -43,7 +40,7 @@ in a browser

```
As a Maker
So that I can let people know what I am doing
So that I can let people know what I am doing
I want to post a message (peep) to chitter
```

Expand All @@ -52,13 +49,15 @@ As a Maker
So that I can see when people are doing things
I want to see the date the message was posted
```

(Hint the database table will need to change to store the date too)

```
As a Maker
So that I can easily see the latest peeps
I want to see a list of peeps in reverse chronological order
```

```
As a Maker
So that I can find relevant peeps
Expand Down
32 changes: 30 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
require 'sinatra/base'
require './lib/peep'


class Chitter < Sinatra::Base
get '/test' do
'Test page'

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]
@peeps_filtered = Peep.search(keyword: params[:filter])
erb :filtered_results
end

run! if app_file == $0
Expand Down
6 changes: 5 additions & 1 deletion db/migrations/01_create_chitter_table.sql
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
);
1 change: 1 addition & 0 deletions db/migrations/02_create_author_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE author(id SERIAL PRIMARY KEY, name VARCHAR(60));
2 changes: 2 additions & 0 deletions db/migrations/03_add_timestamp_to_peeps.sql
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');
12 changes: 12 additions & 0 deletions db/migrations/04_insert_data_to_peeps.sql
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);
6 changes: 6 additions & 0 deletions db/migrations/05_insert_data_to_author.sql
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');
14 changes: 14 additions & 0 deletions lib/database.rb
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
67 changes: 67 additions & 0 deletions lib/peep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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
connection = select_db
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
connection = select_db
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:)
connection = select_db
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:)
connection = select_db
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

def self.select_db
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end
end
end
7 changes: 7 additions & 0 deletions spec/database_helpers.rb
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
11 changes: 11 additions & 0 deletions spec/features/post_peeps_spec.rb
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
18 changes: 18 additions & 0 deletions spec/features/search_peeps_spec.rb
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
58 changes: 58 additions & 0 deletions spec/features/view_peeps_spec.rb
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
Loading