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

Laila #221

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Laila #221

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Your name

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

Laila Gharzai
# 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"
- [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"
- [ ] 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"

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ GEM
mini_mime (1.1.1)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
nokogiri (1.12.3-arm64-darwin)
racc (~> 1.4)
nokogiri (1.12.3-x86_64-darwin)
racc (~> 1.4)
parallel (1.20.1)
Expand Down Expand Up @@ -83,6 +85,7 @@ GEM
nokogiri (~> 1.8)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand Down
21 changes: 18 additions & 3 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
require 'sinatra/base'

require './lib/peep'
class Chitter < Sinatra::Base
get '/test' do
'Test page'

get '/' do
'Chitter'
end

get '/peeps' do
@peeps = Peep.all
erb :'index'
end

get '/peeps/new' do
erb :"new"

Choose a reason for hiding this comment

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

keep quotation marks consistent

end

post '/peeps' do

Choose a reason for hiding this comment

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

I would try and keep your route naming consistent, as above you are getting '/peeps/new/' I would also post 'peeps/new' here as this post is creating a new peep.

Peep.add(message: params[:peep], username: params[:username], posted_at: params[:posted_at])
redirect '/peeps'
end

run! if app_file == $0
Expand Down
1 change: 1 addition & 0 deletions db/migrations/02_add_username_to_peeps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD COLUMN username VARCHAR(60);
1 change: 1 addition & 0 deletions db/migrations/03_add_timestamp_to_peeps.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE peeps ADD COLUMN posted_at TIMESTAMP DEFAULT NOW();
39 changes: 39 additions & 0 deletions lib/peep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'pg'

class Peep

attr_reader :id, :message, :username, :posted_at

def initialize(id:, message:, username:, posted_at:)
@id = id
@message = message
@username = username
@posted_at = posted_at
end

def self.all
if ENV['ENVIRONMENT'] == 'test'

Choose a reason for hiding this comment

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

This if else statement is going to be required in any function in this class and its already adding a lot of bloat just being used twice, could this be extracted into its own function?

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'], username: peep['username'], posted_at: peep['posted_at'])
end
end

def self.add(message:, username:, posted_at:)
if ENV['ENVIRONMENT'] == 'test'
connection = PG.connect(dbname: 'chitter_test')
else
connection = PG.connect(dbname: 'chitter')
end

result = connection.exec("INSERT INTO peeps (id, message, username, posted_at) VALUES (DEFAULT, '#{message}', '#{username}', CURRENT_TIMESTAMP) RETURNING id, message, username, posted_at;")
Peep.new(id: result[0]['id'], message: result[0]['message'], username:result[0]['username'], posted_at:result[0]['posted_at'])
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
10 changes: 10 additions & 0 deletions spec/features/adding_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
feature 'Adding a peep' do
scenario 'A user can add a new peep (message) to Chitter database' do
visit('/peeps/new')
fill_in('peep', with: 'This is my message I would like to post')
fill_in('username', with: 'jenny123')
click_button('Submit')

expect(page).to have_content('This is my message I would like to post', 'jenny123')
end
end
6 changes: 6 additions & 0 deletions spec/features/home_page_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
feature 'Viewing home page' do
scenario 'visiting the home page' do
visit('/')
expect(page).to have_content "Chitter"
end
end
6 changes: 0 additions & 6 deletions spec/features/test_page_spec.rb

This file was deleted.

14 changes: 14 additions & 0 deletions spec/features/viewing_peeps_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require './lib/peep'

feature 'Viewing peeps' do
scenario 'A user can see peeps' do

Peep.add(message: "peep1", username: 'jenny123')
Peep.add(message: "peep2", username: 'benny123')

visit '/peeps'

expect(page).to have_content('peep1', 'jenny123')
expect(page).to have_content('peep2', 'benny123')
end
end
36 changes: 36 additions & 0 deletions spec/peep_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'pg'
require 'peep'
require 'database_helpers'

describe Bookmark do

describe '.all' do
it 'returns a list of peeps and their users' do
connection = PG.connect(dbname: 'chitter_test')

peep = Peep.add(message: 'hello', username:'jenny1')
Peep.add(message: 'hello2', username:'jenny2')
Peep.add(message: 'hello3', username:'jenny3')

peeps = Peep.all

expect(peeps.length).to eq 3
expect(peeps.first).to be_a Peep
expect(peeps.first.id).to eq peep.id
expect(peeps.first.message).to eq 'hello'
expect(peeps.first.username).to eq 'jenny1'
end

describe '.add' do
it 'creates a new peep' do
peep = Peep.add(message: 'hello', username: 'jenny123')
persisted_data = persisted_data(id: peep.id)

expect(peep).to be_a Peep
expect(peep.id).to eq persisted_data.first['id']
expect(peep.message).to eq 'hello'
expect(peep.username).to eq 'jenny123'
end
end

end
12 changes: 12 additions & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

<ul>
<% @peeps.each do |peep| %>
<li>
<big><%=peep.username%> says: "<%=peep.message%>"</big>
<p style="color:#808080"> <smal><em> (posted on: <%=peep.posted_at%>) </em></smal></p>
</li>
<% end %>
</ul>



8 changes: 8 additions & 0 deletions views/new.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<form action="/peeps" method="post">
Type in your peep:<br>
<input type="text" name="peep" /><br>
Type in your username:<br>
<input type="text" name="username" /><br>
<input type="submit" value="Submit" />
</form>