Skip to content
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
16 changes: 8 additions & 8 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Your name

Please write your full name here to make it easier to find your pull request.
William Marshall

# 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"
- [x] User story 1: "I want to see all the messages (peeps) in a browser"
- [x] User story 2: "I want to post a message (peep) to chitter"
- [x] User story 3: "I want to see the date the message was posted"
- [x] 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"

# README checklist

Does your README contains instructions for

- [ ] how to install,
- [ ] how to run,
- [ ] and how to test your code?
- [x] how to install,
- [x] how to run,
- [x] and how to test your code?

[Here is a pill](https://github.com/makersacademy/course/blob/main/pills/readmes.md) that can help you write a great README!
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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
* Run the queries saved in the files 01_create_chitter_table.sql and 02_add_date_to_peeps.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.
Expand All @@ -25,13 +25,19 @@ To setup the test database:
* 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
* Run the queries saved in the files 01_create_chitter_table.sql and 02_add_date_to_peeps.sql

* `bundle install`
* `rspec`

You should see 1 passing test.

To run the app:
* `ruby app.rb`

To run tests:
* `rspec`

## User stories

```
Expand Down
19 changes: 19 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
require 'sinatra/base'
require "./lib/peep"

class Chitter < Sinatra::Base
get '/test' do
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably delete this test route

'Test page'
end

get "/" do
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice restful routes

"Chitter"
end

get "/peeps" do
@peeps = Peep.sort_by_date
erb(:"peeps/index")
end

get "/peeps/new" do
erb(:"peeps/new")
end

post "/peeps" do
Peep.create(message: params[:message])
redirect to("/peeps")
end

run! if app_file == $0
end
1 change: 1 addition & 0 deletions db/migrations/02_add_date_to_peeps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD date DATE;
49 changes: 49 additions & 0 deletions lib/peep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "pg"

class Peep
attr_reader :id, :message, :date

def initialize(id:, message:, date:)
@message = message
@id = id
@date = date
end

def self.create(message:, date: Date.today.to_s)
if ENV["RACK_ENV"] == "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]
)
Peep.new(id: result[0]["id"], message: result[0]["message"], date: result[0]["date"])
end

def self.all
if ENV["RACK_ENV"] == "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"], date: peep["date"])
end
end

def self.sort_by_date
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you kept working on this and wanted to add a new feature to sort by something other than date, could you refactor this method to be more flexible?

if ENV["RACK_ENV"] == "test"
connection = PG.connect(dbname: "chitter_test")
else
connection = PG.connect(dbname: "chitter")
end
result = connection.exec("SELECT * FROM peeps ORDER BY date DESC")
result.map do |peep|
Peep.new(id: peep["id"], message: peep["message"], date: peep["date"])
end
end

end
8 changes: 8 additions & 0 deletions spec/features/posting_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
feature "Posting a peep" do
scenario "User can post a new peep" do
visit("/peeps/new")
fill_in("message", with: "My new peep")
click_button("Submit")
expect(page).to have_content("My new peep")
end
end
11 changes: 11 additions & 0 deletions spec/features/viewing_peep_dates_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
feature "Viewing peep dates" do
scenario "User can see the date a peep was posted" do
first_peep = Peep.create(message: "Test peep 1")
second_peep = Peep.create(message: "Test peep 2", date: "2022-01-01")
visit("/peeps")
expect(page).to have_content("Test peep 1")
expect(page).to have_content(Date.today.to_s)
expect(page).to have_content("Test peep 2")
expect(page).to have_content("2022-01-01")
end
end
15 changes: 15 additions & 0 deletions spec/features/viewing_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
feature "Viewing peeps" do
scenario "visiting the index page" do
visit("/")
expect(page).to have_content("Chitter")
end

scenario "visiting the /peeps page" do
first_peep = Peep.create(message: "Test peep 1")
second_peep = Peep.create(message: "Test peep 2")
visit("/peeps")
expect(page).to have_content("Test peep 1")
expect(page).to have_content("Test peep 2")
end

end
40 changes: 40 additions & 0 deletions spec/peep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "peep"

describe Peep do

describe "::create" do
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be .create not ::create

it "creates a new peep" do
peep = Peep.create(message: "This is a peep")
persisted_data = PG.connect(dbname: "chitter_test").query("SELECT * FROM peeps WHERE id = #{peep.id}")
expect(peep).to be_a(Peep)
expect(peep.id).to eq(persisted_data.first["id"])
expect(peep.message).to eq("This is a peep")
expect(peep.date).to eq(Date.today.to_s)
end
end

describe "::all" do
it "returns all peeps" do
first_peep = Peep.create(message: "Test peep 1")
second_peep = Peep.create(message: "Test peep 2")
peeps = Peep.all
expect(peeps.length).to eq(2)
expect(peeps.first).to be_a(Peep)
expect(peeps.first.id).to eq(first_peep.id)
expect(peeps.first.message).to eq("Test peep 1")
expect(peeps.first.date).to eq(Date.today.to_s)
end
end

describe "::sort_by_date" do
it "sorts peeps in reverse chronological order" do
first_peep = Peep.create(message: "Test peep 1", date: "2020-01-01")
second_peep = Peep.create(message: "Test peep 2", date: "2022-01-01")
third_peep = Peep.create(message: "Test peep 3", date: "2021-01-01")
sorted_peeps = Peep.sort_by_date
expect(sorted_peeps[0].id).to eq(second_peep.id)
expect(sorted_peeps[1].id).to eq(third_peep.id)
expect(sorted_peeps[2].id).to eq(first_peep.id)
end
end
end
10 changes: 10 additions & 0 deletions views/peeps/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<ul>
<% @peeps.each do |peep| %>
<li class="peep" id="peep-<%= peep.id %>">
<%= peep.message %>
<br>
[<%= peep.date %>]
<br><br>
</li>
<% end %>
</ul>
4 changes: 4 additions & 0 deletions views/peeps/new.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<form action="/peeps" method=post>
<input type=text name="message" placeholder="Message">
<input type=submit value="Submit">
</form>