-
Notifications
You must be signed in to change notification settings - Fork 241
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
Chitter solo challenge #231
base: main
Are you sure you want to change the base?
Changes from all commits
5531edd
9b5d95c
3778506
249c22f
e26b9aa
79b7258
b11aa38
33c6b62
a731fd5
8333709
3f40e0f
53068bf
484224d
07af151
888c6e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,42 @@ | ||
## Chitter Challenge | ||
# Chitter | ||
|
||
* 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 is a databased backed web application that allows a user to view and post messages (peeps). | ||
|
||
We are going to write a small Twitter clone that will allow the users to post messages to a public stream. | ||
## Getting started | ||
|
||
## Set up | ||
``` | ||
git clone https://github.com/olliefreeman94/chitter-challenge-apprenticeships.git | ||
``` | ||
|
||
To setup the database: | ||
|
||
* 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 inside the chitter database. You should see one row in there. | ||
* Connect to the database using the psql command `\c chitter` | ||
* Run the query saved in 01_create_chitter_table.sql | ||
* Run the query saved in 02_add_date_to_peeps.sql | ||
|
||
To setup the test database: | ||
* Connect to psql | ||
* Create the database using the psql | ||
command `CREATE DATABASE chitter_test;`; | ||
* 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 | ||
|
||
* `bundle install` | ||
* `rspec` | ||
|
||
You should see 1 passing test. | ||
|
||
## User stories | ||
* Run the query saved in 01_create_chitter_table.sql | ||
* Run the query saved in 02_add_date_to_peeps.sql | ||
|
||
``` | ||
As a Maker | ||
So that I can see what people are doing | ||
I want to see all the messages (peeps) | ||
in a browser | ||
bundle install | ||
``` | ||
|
||
``` | ||
As a Maker | ||
So that I can let people know what I am doing | ||
I want to post a message (peep) to chitter | ||
``` | ||
## Usage | ||
|
||
``` | ||
As a Maker | ||
So that I can see when people are doing things | ||
I want to see the date the message was posted | ||
rackup | ||
``` | ||
(Hint the database table will need to change to store the date too) | ||
|
||
Navigate to `http://localhost:9292/` | ||
|
||
## Running tests | ||
|
||
``` | ||
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 | ||
I want to filter on a specific keyword | ||
rspec | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,38 @@ | ||
require 'sinatra/base' | ||
require_relative './lib/peep' | ||
|
||
class Chitter < Sinatra::Base | ||
get '/test' do | ||
'Test page' | ||
end | ||
Comment on lines
5
to
7
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always a good idea to remove test code from code you submit for code review (anywhere, not just at Makers) |
||
|
||
get '/' do | ||
erb :index | ||
end | ||
|
||
get '/peeps' do | ||
@peeps = Peep.all | ||
erb :'peeps/index' | ||
end | ||
|
||
post '/peeps' do | ||
Peep.add(message: params[:message], date: params[:date]) | ||
redirect '/peeps' | ||
end | ||
|
||
get '/peeps/new' do | ||
erb :'peeps/new' | ||
end | ||
|
||
get '/peeps/search' do | ||
erb :'peeps/search' | ||
end | ||
|
||
get '/peeps/filtered' do | ||
@peeps = Peep.filter(params[:keyword]) | ||
@view_all_button = 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I saw further down that you're using this as a kind of boolean flag, in which case it would be better to make your intention explicit here and set this to true rather than one. |
||
erb :'peeps/index' | ||
end | ||
Comment on lines
+23
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You've shown good use of RESTful routes 👍🏽 |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clean and concise. I can follow the workflow from how the methods are structured. |
||
run! if app_file == $0 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## 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. | ||
|
||
We are going to write a small Twitter clone that will allow the users to post messages to a public stream. | ||
|
||
## Set up | ||
|
||
To setup the database: | ||
|
||
* 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 inside the chitter database. You should see one row in there. | ||
|
||
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 | ||
|
||
* `bundle install` | ||
* `rspec` | ||
|
||
You should see 1 passing test. | ||
|
||
## User stories | ||
|
||
``` | ||
As a Maker | ||
So that I can see what people are doing | ||
I want to see all the messages (peeps) | ||
in a browser | ||
``` | ||
|
||
``` | ||
As a Maker | ||
So that I can let people know what I am doing | ||
I want to post a message (peep) to chitter | ||
``` | ||
|
||
``` | ||
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 | ||
I want to filter on a specific keyword | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE peeps ADD COLUMN date DATE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Separate migration file -- great. And DATE is an appropriate datatype to use. Another option would have been to use TIMESTAMP. That would give you flexibility in the future if it ever becomes necessary to show time information too. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'pg' | ||
|
||
class Peep | ||
|
||
attr_reader :id, :message, :date | ||
|
||
def initialize(id:, message:, date:) | ||
@id = id | ||
@message = message | ||
@date = date | ||
end | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
Comment on lines
+14
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're using this same piece of code in more than one place in this file. How might you get rid of this duplication? |
||
|
||
result = connection.exec("SELECT * FROM peeps ORDER BY date DESC") | ||
result.map { |peep| Peep.new(id: peep['id'], message: peep['message'], date: peep['date']) } | ||
end | ||
|
||
def self.add(message:, date:) | ||
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, date) VALUES($1, $2) RETURNING id, message, date;", | ||
[message, date] | ||
).first | ||
Peep.new(id: result['id'], message: result['message'], date: result['date']) | ||
end | ||
|
||
def self.filter(keyword) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
|
||
result = connection.exec( | ||
"SELECT * FROM peeps WHERE message ILIKE '%#{keyword}%' ORDER BY date DESC" | ||
) | ||
Comment on lines
+45
to
+47
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice use of ILIKE! |
||
result.map { |peep| Peep.new(id: peep['id'], message: peep['message'], date: peep['date']) } | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Next steps | ||
- review format in which date is displayed to user | ||
- add validation to forms | ||
- consolidate helper classes into one file? | ||
- merge or link '/' and 'peeps' routes? | ||
- pathing between routes | ||
|
||
Additional features | ||
- record time as well as date | ||
- message character limit validation | ||
- cf. Bookmark Manager extensions, | ||
e.g. update (inc. logging?), delete, user authentication | ||
- equivilent of Twitter # | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good use of notes and to do to plan next steps |
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};") | ||
return result.first | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'timecop' | ||
|
||
feature 'adding a new message' do | ||
scenario 'a user can add a message to Chitter' do | ||
Timecop.freeze(Time.local(2022, 7, 15)) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! Timecop is useful. Another option that doesn't require a gem would have been to stub the return value of
|
||
visit('/peeps/new') | ||
fill_in('message', with: 'Example peep') | ||
click_button('Submit') | ||
end | ||
|
||
expect(page).to have_content 'Example peep (2022-07-15)' | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
feature 'filtering messages' do | ||
scenario 'a user can filter messages by keyword' do | ||
Peep.add(message: 'Banana peep', date: '2022-07-15') | ||
Peep.add(message: 'Apple', date: '2022-07-10') | ||
Peep.add(message: 'Another banana peep', date: '2022-07-05') | ||
|
||
visit('/peeps/search') | ||
fill_in('keyword', with: 'banana') | ||
click_button('Search') | ||
|
||
expect(page).to have_content "Banana peep (2022-07-15)" | ||
expect(page).to have_content "Another banana peep (2022-07-05)" | ||
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is good but doesn't exclude the possibility that the other non-banana peeps are also on the page! How could you test for that? |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
feature 'pathing between routes' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very thorough, nice! |
||
scenario 'from homepage user can view all messages' do | ||
add_row_to_test_database | ||
visit '/' | ||
click_button 'View all peeps' | ||
|
||
expect(page).to have_content "This is a peep!" | ||
end | ||
|
||
scenario 'from homepage user can add a new message' do | ||
visit '/' | ||
click_button 'Add a new peep' | ||
|
||
expect(page).to have_content "New peep" | ||
end | ||
|
||
scenario "from '/peeps' user can add a new message" do | ||
visit '/peeps' | ||
click_button 'Add a new peep' | ||
|
||
expect(page).to have_content "New peep" | ||
end | ||
|
||
scenario "from '/peeps' user can filter messages by keyword" do | ||
visit '/peeps' | ||
click_button 'Filter peeps by keyword' | ||
|
||
expect(page).to have_content "Search" | ||
end | ||
|
||
scenario 'from filtered search results, user can view all messages' do | ||
Peep.add(message: 'Banana peep', date: '2022-07-15') | ||
Peep.add(message: 'Apple peep', date: '2022-07-10') | ||
visit '/peeps/search' | ||
fill_in('keyword', with: 'banana') | ||
click_button 'Search' | ||
|
||
click_button 'View all peeps' | ||
|
||
expect(page).to have_content "Apple peep" | ||
end | ||
|
||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice README, it's very clear.