-
Notifications
You must be signed in to change notification settings - Fork 229
Will M - Chitter #223
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
base: main
Are you sure you want to change the base?
Will M - Chitter #223
Changes from all commits
80cb647
d1c0f59
d80237c
29ca323
ea56f75
2ab76b6
215c139
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,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! |
| 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 | ||
| 'Test page' | ||
| end | ||
|
|
||
| get "/" 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. 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ALTER TABLE peeps ADD date DATE; |
| 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 | ||
|
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. 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 | ||
| 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 |
| 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 |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require "peep" | ||
|
|
||
| describe Peep do | ||
|
|
||
| describe "::create" 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 should be |
||
| 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 | ||
| 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> |
| 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> |
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.
You can probably delete this test route