-
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
Charlie Parker Chitter Challenge #2185
Open
cshjp
wants to merge
1
commit into
makersacademy:main
Choose a base branch
from
cshjp: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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
require 'date' | ||
require 'sinatra' | ||
require "sinatra/reloader" | ||
require_relative 'lib/database_connection' | ||
require_relative 'lib/peep_repo' | ||
require_relative 'lib/user_repo' | ||
|
||
DatabaseConnection.connect | ||
|
||
class Application < Sinatra::Base | ||
|
||
enable :sessions | ||
|
||
configure :development do | ||
register Sinatra::Reloader | ||
end | ||
|
||
get '/' do | ||
peeps | ||
end | ||
|
||
post '/' do | ||
return erb(:login) if session[:user_id].nil? | ||
repo = PeepRepo.new | ||
peep = Peep.new | ||
peep.message = params['peep'] | ||
peep.time = DateTime.now | ||
peep.user_account_id = 1 | ||
repo.create(peep) | ||
peeps | ||
end | ||
|
||
get '/signup' do | ||
return erb(:signup) | ||
end | ||
|
||
post '/signup' do | ||
repo = UserRepo.new | ||
new_user = User.new | ||
new_user.username = params[:username] | ||
new_user.password = params[:password] | ||
new_user.email_address = params[:email] | ||
new_user.name = params[:name] | ||
ult = repo.create(new_user) | ||
return erb(:login) if ult == false | ||
return erb(:signup) | ||
end | ||
|
||
get '/login' do | ||
return erb(:login) | ||
end | ||
|
||
post '/login' do | ||
username = params[:username] | ||
password = params[:password] | ||
user = UserRepo.new.find_by_username(username) | ||
return erb(:login) unless user.password == password | ||
session[:user_id] = user.id | ||
peeps | ||
end | ||
|
||
get '/logout' do | ||
session[:user_id] = nil | ||
peeps | ||
end | ||
|
||
private | ||
|
||
def peeps | ||
@session = session[:user_id] | ||
@peeps = PeepRepo.new.all.reverse | ||
return erb(:index) | ||
end | ||
|
||
def new_user | ||
|
||
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,2 @@ | ||
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,39 @@ | ||
|
||
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 the environment variable (set by Render) | ||
# is present, use this to open the connection. | ||
if ENV['DATABASE_URL'] != nil | ||
@connection = PG.connect(ENV['DATABASE_URL']) | ||
return | ||
end | ||
|
||
if ENV['ENV'] == 'test' | ||
database_name = 'test_chitter' | ||
else | ||
database_name = 'chitter' | ||
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, :message, :time, :user_account_id, :username, :name | ||
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,52 @@ | ||
require_relative 'peep' | ||
require_relative 'database_connection' | ||
|
||
class PeepRepo | ||
def initialize | ||
@peeps = [] | ||
end | ||
|
||
def all | ||
sql = 'SELECT posts.*, user_accounts.username, user_accounts.name | ||
FROM posts | ||
INNER JOIN user_accounts | ||
ON posts.user_account_id = user_accounts.id;' | ||
result = DatabaseConnection.exec_params(sql, []) | ||
result.each do |record| | ||
@peeps << new_peep(record) | ||
end | ||
@peeps | ||
end | ||
|
||
def find(id) | ||
sql = 'SELECT * FROM posts WHERE id = $1;' | ||
params = [id] | ||
result = DatabaseConnection.exec_params(sql, params) | ||
record = result[0] | ||
peep = Peep.new | ||
peep.id = record['id'] | ||
peep.message = record['message'] | ||
peep.time = record['time'] | ||
peep.user_account_id = record['user_account_id'] | ||
peep | ||
end | ||
|
||
def create(peep) | ||
sql = 'INSERT INTO posts (message, time, user_account_id) VALUES ($1, $2, $3);' | ||
params = [peep.message, peep.time, peep.user_account_id] | ||
DatabaseConnection.exec_params(sql, params) | ||
end | ||
|
||
private | ||
|
||
def new_peep(record) | ||
peep = Peep.new | ||
peep.id = record['id'] | ||
peep.message = record['message'] | ||
peep.time = record['time'] | ||
peep.user_account_id = record['user_account_id'] | ||
peep.username = record['username'] | ||
peep.name = record['name'] | ||
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, :email_address, :username, :name, :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,55 @@ | ||
require_relative 'user' | ||
require_relative 'database_connection' | ||
|
||
class UserRepo | ||
def initialize | ||
@users = [] | ||
end | ||
|
||
def all | ||
sql = 'SELECT * | ||
FROM user_accounts;' | ||
result = DatabaseConnection.exec_params(sql, []) | ||
result.each do |record| | ||
@users << new_user(record) | ||
end | ||
@users | ||
end | ||
|
||
def create(user) | ||
return true if check_unique(user.email_address, user.username) | ||
sql = 'INSERT INTO user_accounts (email_address, username, name, password) | ||
VALUES ($1, $2, $3, $4);' | ||
params = [user.email_address, user.username, user.name, user.password] | ||
DatabaseConnection.exec_params(sql, params) | ||
return false | ||
end | ||
|
||
def check_unique(email_address, username) | ||
sql = 'SELECT email_address, username | ||
FROM user_accounts | ||
WHERE email_address = $1 OR username = $2;' | ||
params = [email_address, username] | ||
result = DatabaseConnection.exec_params(sql, params) | ||
return !result.first.nil? | ||
end | ||
|
||
def find_by_username(username) | ||
sql = 'SELECT * FROM user_accounts WHERE username = $1;' | ||
params = [username] | ||
result = DatabaseConnection.exec_params(sql, params) | ||
return new_user(result[0]) | ||
end | ||
|
||
private | ||
|
||
def new_user(record) | ||
user = User.new | ||
user.id = record['id'] | ||
user.email_address = record['email_address'] | ||
user.username = record['username'] | ||
user.name = record['name'] | ||
user.password = record['password'] | ||
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 was supposed to be peep.user_account_id = session[user_id]