-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Partial Solution to Chitter Challenge #1230
Open
samkitchen94
wants to merge
8
commits into
makersacademy:main
Choose a base branch
from
samkitchen94:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
101d04d
first commit
samkitchen94 d745346
testing infrastructure working
samkitchen94 21956a3
first user story satisfied, user can now sign in
samkitchen94 c500b9f
second user story satisfied, now user can view peeps
samkitchen94 701be81
test added for viewing peep spec
samkitchen94 388c747
third feature test passing, user can now add a peep
samkitchen94 8b1d2cd
updated readme with instructions
samkitchen94 46c6894
refactored
samkitchen94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
require 'sinatra/base' | ||
require './lib/user' | ||
require './lib/peep' | ||
|
||
class ChitterChallenge < Sinatra::Base | ||
enable :sessions | ||
|
||
get '/' do | ||
erb :index | ||
end | ||
|
||
post '/chitter' do | ||
user = User.create(name: params[:name], username: params[:username], email: params[:email], password: params[:password]) | ||
@user = user.username | ||
redirect '/chitter' | ||
end | ||
|
||
get '/chitter' do | ||
@peeps = Peep.all | ||
erb :chitter | ||
end | ||
|
||
post '/chitter/new' do | ||
Peep.create(message: params[:message]) | ||
redirect '/chitter' | ||
end | ||
|
||
run! if app_file == $0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require_relative "./app" | ||
|
||
run ChitterChallenge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE TABLE users(id SERIAL PRIMARY KEY, name, username, email, password VARCHAR(60)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE TABLE peeps(id SERIAL PRIMARY KEY, message, sent_time VARCHAR(200) NOT NULL); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Peep | ||
attr_reader :id, :message, :sent_time | ||
|
||
def initialize(id:, message:, sent_time:) | ||
@id = id | ||
@message = message | ||
@sent_time = sent_time | ||
end | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
peeps = connection.exec("SELECT * FROM peeps ORDER BY sent_time DESC;") | ||
peeps.map { |peep| to_peep(peep) } | ||
end | ||
|
||
def self.create(message:) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
|
||
peep = connection.exec("INSERT INTO peeps (message, sent_time) VALUES('#{message}', '#{Time.now}') RETURNING id, message, sent_time;").first | ||
|
||
to_peep(peep) | ||
end | ||
|
||
def self.to_peep(peep_record) | ||
Peep.new(id: peep_record["id"], \ | ||
message: peep_record["message"], \ | ||
sent_time: Time.parse(peep_record["sent_time"])) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'pg' | ||
|
||
class User | ||
attr_reader :id, :name, :username, :email, :password | ||
|
||
def initialize(id:, name:, username:, email:, password:) | ||
@id = id | ||
@name = name | ||
@username = username | ||
@email = email | ||
@password = password | ||
end | ||
|
||
|
||
def self.create(name:, username:, email:, password:) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
result = connection.exec("INSERT INTO users (name, username, email, password) VALUES('#{name}', '#{username}', '#{email}', '#{password}')RETURNING id, name, username, email, password") | ||
User.new(id: result[0]['id'], name: result[0]['name'], username: result[0]['username'], email: result[0]['email'], password: result[0]['password']) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
feature "posting a peep" do | ||
scenario "it allows the user to post a peep" do | ||
visit '/chitter' | ||
fill_in 'peep', with: 'First peep' | ||
click_button 'Peep!' | ||
expect(page).to have_content("First peep") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
feature "signing up" do | ||
scenario "the homepage has a sign up feature" do | ||
visit '/' | ||
fill_in('name', with: "Mo Salah") | ||
fill_in('username', with: "champsleagueisours2k19") | ||
fill_in('email', with: "[email protected]") | ||
fill_in('password', with: "ilovelfc") | ||
click_button('Sign up!') | ||
expect(page).to have_content "You are now signed in to Chitter" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
feature "viewing peeps" do | ||
scenario "it allows you to view a list of peeps" do | ||
peep_1 = Peep.create(message: "First peep") | ||
peep_2 = Peep.create(message: "Second peep") | ||
peep_3 = Peep.create(message: "Third peep") | ||
|
||
visit '/chitter' | ||
|
||
expect(page).to have_content("Third peep") | ||
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. There might be a way to test if this is the first element on the web page. This would test if the peeps are in reverse order with most recent first. Something like, |
||
expect(page).to have_content("#{peep_3.sent_time}") | ||
expect(page).to have_content("Second peep") | ||
expect(page).to have_content("#{peep_2.sent_time}") | ||
expect(page).to have_content("First peep") | ||
expect(page).to have_content("#{peep_1.sent_time}") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'peep' | ||
|
||
describe Peep do | ||
let(:time_now) { Time.parse(Time.now.strftime("%Y-%m-%d %H:%M:%S :z")) } | ||
|
||
describe '#new' do | ||
it 'creates a peep with id, message and time' do | ||
peep = Peep.new(id: 1, message: "New peep", sent_time: time_now) | ||
|
||
expect(peep.id).to eq(1) | ||
expect(peep.message).to eq("New peep") | ||
expect(peep.sent_time).to eq(time_now) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require 'pg' | ||
|
||
def setup_test_database | ||
p "Setting up test database..." | ||
|
||
connection = PG.connect(dbname: 'chitter_test') | ||
|
||
# Clear the bookmarks table | ||
connection.exec("TRUNCATE users;") | ||
connection.exec("TRUNCATE peeps;") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require 'user' | ||
|
||
describe User do | ||
describe '.create' do | ||
it "creates a new user" do | ||
user = User.create(name: 'Sam', username: 'samk94', email: '[email protected]', password: 'pass') | ||
expect(user.name).to eq('Sam') | ||
expect(user.username).to eq('samk94') | ||
expect(user.email).to eq('[email protected]') | ||
expect(user.password).to eq('pass') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<h1> Welcome to Chitter! </h1> | ||
<br> | ||
<h2> You are now signed in to Chitter </h2> | ||
<br> | ||
<form action="chitter/new" method="post" > | ||
<input type="text" name="message" placeholder="peep" /> | ||
<input type="submit" value="Peep!" /> | ||
</form> | ||
<div class='feed'> | ||
<table> | ||
<% @peeps.each do |peep| %> | ||
<tr class='peep-details'> | ||
<td> | ||
<%= peep.sent_time %> | ||
</td> | ||
</tr> | ||
<tr class='peep-message'> | ||
<td> | ||
<%= peep.message %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<h1> Welcome to Chitter! </h1> | ||
<br> | ||
<h2> Please sign up using the form below: </h2> | ||
<br> | ||
<form method="post" action="/chitter"> | ||
Enter your name: | ||
<input type="text" name="name" placeholder="Name" /> | ||
<br> | ||
<br> | ||
Enter your username: | ||
<input type="text" name="username" placeholder="Username" /> | ||
<br> | ||
<br> | ||
Enter your email address: | ||
<input type="text" name="email" placeholder="Email" /> | ||
<br> | ||
<br> | ||
Finally, enter your password and hit sign up: | ||
<input type="text" name="password" placeholder="Password" /> | ||
<br> | ||
<br> | ||
<input type="submit" value="Sign up!" /> | ||
</form> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 refactor these into separate files so you DRY out the connections.