-
Notifications
You must be signed in to change notification settings - Fork 235
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
base: main
Are you sure you want to change the base?
Laila #221
Changes from all commits
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,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" | ||
end | ||
|
||
post '/peeps' 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. 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE peeps ADD COLUMN username VARCHAR(60); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE peeps ADD COLUMN posted_at TIMESTAMP DEFAULT NOW(); |
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' | ||
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 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 |
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 |
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 |
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 |
This file was deleted.
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 |
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 |
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> | ||
|
||
|
||
|
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> |
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.
keep quotation marks consistent