-
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
Natasha Chitter Challenge #2176
Open
natashabuckham
wants to merge
20
commits into
makersacademy:main
Choose a base branch
from
natashabuckham:main
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
20 commits
Select commit
Hold shift + click to select a range
b85344c
files setup
natashabuckham 80a1d5f
added bcrypt and sessions setup
natashabuckham ad6d406
html skeleton and route plan
natashabuckham a86623a
bcrypt gem installation fixed, database connect method fixed, peep re…
natashabuckham d3e320d
peep repo create method tested
natashabuckham 99f3d2b
updated seeds structure to account for foreign key constraint
natashabuckham e8ee635
user repo create method tested
natashabuckham 74c0705
rewritten user repo create test
natashabuckham 408a9fd
user repo find by email method tested
natashabuckham cde3440
user repo find by id method tested
natashabuckham 6713560
get / route tested
natashabuckham c595618
get /signup route tested
natashabuckham 8f88a87
post /signup route tested
natashabuckham a6fb80e
get /login route tested
natashabuckham 45fe0b7
post /login route tested
natashabuckham e70a324
POST /login wrong password route tested
natashabuckham 9b8f9bf
get /peeps/new route tested
natashabuckham 03d5858
post /peeps route tested
natashabuckham e3139be
update password input type in forms, update form submit button labels
natashabuckham 3ef3a5b
added comments for future work
natashabuckham 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
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. Excellent naming convention 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. Unsure about logic convention, something to research |
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,100 @@ | ||
require 'sinatra/base' | ||
require 'sinatra/reloader' | ||
require_relative 'lib/database_connection' | ||
require_relative 'lib/user_repository' | ||
require_relative 'lib/peep_repository' | ||
require 'bcrypt' | ||
|
||
DatabaseConnection.connect | ||
|
||
class Application < Sinatra::Base | ||
enable :sessions | ||
# This allows the app code to refresh | ||
# without having to restart the server. | ||
configure :development do | ||
register Sinatra::Reloader | ||
also_reload 'lib/user_repository' | ||
also_reload 'lib/peep_repository' | ||
end | ||
|
||
get '/' do | ||
user_repo = UserRepository.new | ||
peep_repo = PeepRepository.new | ||
peeps = peep_repo.all | ||
|
||
@complete_peep_info = [] | ||
|
||
peeps.each do |peep| | ||
user = user_repo.find(peep.user_id) | ||
peep_info = {peep: peep, name: user.name, username: user.username} | ||
@complete_peep_info << peep_info | ||
end | ||
|
||
return erb(:index) # add log in status logic | ||
end | ||
|
||
get '/login' do | ||
return erb(:login) | ||
end | ||
|
||
post '/login' do | ||
email = params[:email] | ||
password = params[:password] | ||
|
||
user_repo = UserRepository.new | ||
|
||
user = user_repo.find_by_email(email) | ||
stored_password = BCrypt::Password.new(user.password) | ||
|
||
if stored_password == password | ||
session[:user_id] = user.id | ||
return erb(:login_success) | ||
else | ||
return erb(:wrong_password) | ||
end | ||
end | ||
|
||
get '/peeps/new' do | ||
if session[:user_id] == nil | ||
return erb(:login) | ||
else | ||
return erb(:new_peep) | ||
end | ||
end | ||
|
||
post '/peeps' do | ||
if session[:user_id] == nil | ||
return erb(:login) | ||
else | ||
peep_repo = PeepRepository.new | ||
new_peep = Peep.new | ||
new_peep.time = Time.now | ||
new_peep.content = params[:content] | ||
new_peep.user_id = session[:user_id] | ||
|
||
peep_repo.create(new_peep) | ||
|
||
return erb(:new_peep_success) | ||
end | ||
end | ||
|
||
get '/signup' do | ||
return erb(:signup) | ||
end | ||
|
||
post '/signup' do | ||
repo = UserRepository.new | ||
new_user = User.new | ||
|
||
new_user.name = params[:name] # name and username must be unique | ||
new_user.username = params[:username] | ||
new_user.email = params[:email] | ||
new_user.password = params[:password] | ||
|
||
repo.create(new_user) | ||
|
||
return erb(:signup_success) | ||
end | ||
|
||
# add invalid_parameters? method | ||
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 @@ | ||
# file: config.ru | ||
require './app' | ||
run Application |
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,38 @@ | ||
# file: lib/database_connection.rb | ||
|
||
require 'pg' | ||
|
||
# This class is a thin "wrapper" around the | ||
# PG library. We'll use it in our project to interact | ||
# with the database using SQL. | ||
|
||
class DatabaseConnection | ||
# This method connects to PostgreSQL using the | ||
# PG gem. We connect to 127.0.0.1, and select | ||
# the database name given in argument. | ||
def self.connect | ||
if ENV['DATABASE_URL'] != nil | ||
@connection = PG.connect(ENV['DATABASE_URL']) | ||
return | ||
end | ||
|
||
if ENV['ENV'] == 'test' | ||
database_name = 'chitter_challenge_test' | ||
else | ||
database_name = 'chitter_challenge' | ||
end | ||
@connection = PG.connect({ host: '127.0.0.1', dbname: database_name }) | ||
end | ||
|
||
# This method executes an SQL query | ||
# on the database, providing some optional parameters | ||
# (you will learn a bit later about when to provide these parameters). | ||
def self.exec_params(query, params) | ||
if @connection.nil? | ||
raise 'DatabaseConnection.exec_params: Cannot run a SQL query as the connection to'\ | ||
'the database was never opened. Did you make sure to call first the method '\ | ||
'`DatabaseConnection.connect` in your app.rb file (or in your tests spec_helper.rb)?' | ||
end | ||
@connection.exec_params(query, params) | ||
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,3 @@ | ||
class Peep | ||
attr_accessor :id, :time, :content, :user_id | ||
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,30 @@ | ||
require_relative 'peep' | ||
|
||
class PeepRepository | ||
def all | ||
peeps = [] | ||
|
||
sql = 'SELECT id, time, content, user_id FROM peeps ORDER BY time DESC;' | ||
result_set = DatabaseConnection.exec_params(sql, []) | ||
|
||
result_set.each do |record| | ||
peep = Peep.new | ||
peep.id = record['id'] | ||
peep.time = record['time'] | ||
peep.content = record['content'] | ||
peep.user_id = record['user_id'] | ||
|
||
peeps << peep | ||
end | ||
|
||
return peeps | ||
end | ||
|
||
def create(peep) | ||
sql = 'INSERT INTO peeps (time, content, user_id) VALUES ($1, $2, $3);' | ||
params = [peep.time, peep.content, peep.user_id] | ||
result_set = DatabaseConnection.exec_params(sql, params) | ||
|
||
return 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,3 @@ | ||
class User | ||
attr_accessor :id, :name, :username, :email, :password | ||
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,42 @@ | ||
require_relative 'user' | ||
require 'bcrypt' | ||
|
||
class UserRepository | ||
def create(new_user) | ||
encrypted_password = BCrypt::Password.create(new_user.password) | ||
|
||
sql = 'INSERT INTO users (name, username, email, password) VALUES ($1, $2, $3, $4);' | ||
params = [new_user.name, new_user.username, new_user.email, encrypted_password] | ||
DatabaseConnection.exec_params(sql, params) | ||
|
||
return new_user | ||
end | ||
|
||
def find_by_email(email) | ||
sql = 'SELECT id, name, username, email, password FROM users WHERE email = $1;' | ||
result_set = DatabaseConnection.exec_params(sql, [email]) | ||
|
||
user = User.new | ||
user.id = result_set[0]['id'] | ||
user.name = result_set[0]['name'] | ||
user.username = result_set[0]['username'] | ||
user.email = result_set[0]['email'] | ||
user.password = result_set[0]['password'] | ||
|
||
return user | ||
end | ||
|
||
def find(id) | ||
sql = 'SELECT id, name, username, email, password FROM users WHERE id = $1;' | ||
result_set = DatabaseConnection.exec_params(sql, [id]) | ||
|
||
user = User.new | ||
user.id = result_set[0]['id'] | ||
user.name = result_set[0]['name'] | ||
user.username = result_set[0]['username'] | ||
user.email = result_set[0]['email'] | ||
user.password = result_set[0]['password'] | ||
|
||
return user | ||
end | ||
end |
Oops, something went wrong.
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.
This looks good. Could you check all the gems are in use please? Thank you.