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

Chitter Challenge Most Recent #2195

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b5d7e05
recipe added - initial routes_recipe design added
MattHammond94 May 8, 2023
8ea473f
Files added to complete initial project setup
MattHammond94 May 8, 2023
67dec59
Minor changes made to sql/files to account for timestamp later on - f…
MattHammond94 May 8, 2023
9487813
maker repo updated - tests passed - rubocop run and adjustments made
MattHammond94 May 8, 2023
59f087c
tests added for peep repo - tests passed
MattHammond94 May 8, 2023
e083de5
Delete test added for peep repo - test passed
MattHammond94 May 8, 2023
3b15cd2
Sinatra setup completed
MattHammond94 May 9, 2023
8e42b35
first route tests added for app.rb - tests passed
MattHammond94 May 9, 2023
3c315c1
route test added
MattHammond94 May 9, 2023
bf24a67
ENV variable assigned to account for multiple DBs - spec files update…
MattHammond94 May 9, 2023
f2cc15d
find method added to maker repo - tests passed
MattHammond94 May 9, 2023
e377829
peep route tests passed
MattHammond94 May 9, 2023
703e70c
log in page added - tests passed - tests added for makers page
MattHammond94 May 9, 2023
d743bc6
Typos corrected
MattHammond94 May 9, 2023
8d59140
Signuup page added - tests passed - tests added for post method
MattHammond94 May 9, 2023
a2fec3a
sign up form and post new maker routes completed - fail tests to be a…
MattHammond94 May 9, 2023
fc47989
Fail tests added for makers/new route - tests passed
MattHammond94 May 9, 2023
cf17480
user page/makers id route updated - tests added - tests passed
MattHammond94 May 9, 2023
2cd4e40
find by username method added to maker repo and tested
MattHammond94 May 10, 2023
e69498a
login page successfully implemented - tests passed
MattHammond94 May 10, 2023
7ea2364
homepage updated to include new peep route - new peep route test adde…
MattHammond94 May 10, 2023
baecac6
new peep route tests added - new peep route updated
MattHammond94 May 10, 2023
584c1ab
Typos corrected
MattHammond94 May 10, 2023
474e48c
syntax added to order peeps display - peep by maker method added to p…
MattHammond94 May 10, 2023
e8b1fc8
functionality improved by adding userpage and logout routes - userpag…
MattHammond94 May 10, 2023
1885443
spec tests updated to reflect user login and alternative pages depend…
MattHammond94 May 11, 2023
40ccec4
logout page tests added - tests passed
MattHammond94 May 11, 2023
f90335f
inserts added to seeds - delete route tests added - final route paths…
MattHammond94 May 11, 2023
0044099
delete tests added to app spec
MattHammond94 May 11, 2023
22f2dca
delete peep tests added - files updated accordingly - erb files added
MattHammond94 May 11, 2023
2fac326
find by title method added for peep repo - tests passed
MattHammond94 May 11, 2023
d6d9ce2
Delete route completed - tests passed
MattHammond94 May 11, 2023
0fbc6af
fail tests added for delete peep - == nil replaced with .nil - views …
MattHammond94 May 11, 2023
b05113e
Tests added for user page - tests passed
MattHammond94 May 11, 2023
9b505db
Minor code refactor - no major changes
MattHammond94 May 11, 2023
8a9b3fe
update method added to maker repo - test passed
MattHammond94 May 11, 2023
dc68728
update tests added - tests passed
MattHammond94 May 11, 2023
18e7b0d
Update details function completed
MattHammond94 May 12, 2023
ac2b294
Fail test added for title input - peep new route
MattHammond94 May 12, 2023
16f4a22
title validation method added - route updated - tests passed
MattHammond94 May 12, 2023
79d943f
signup route refactored - method added
MattHammond94 May 12, 2023
8102175
signup route updated - validation methodf refactored further
MattHammond94 May 12, 2023
51ef9b3
BCrypt gem added and implemented in maker repo
MattHammond94 May 12, 2023
4ba0a39
create method updated to include BCrypt - unit tests passed
MattHammond94 May 12, 2023
91015ba
All integration tests updated to include a sign up client - tests inc…
MattHammond94 May 12, 2023
c0a0490
password mask and autocomplete added to forms
MattHammond94 May 14, 2023
5551183
CSS added to homepage
MattHammond94 May 14, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ end
group :development, :test do
gem 'rubocop', '1.20'
end

gem "pg", "~> 1.5"

gem "sinatra", "~> 3.0"
gem "sinatra-contrib", "~> 3.0"
gem "webrick", "~> 1.8"
gem "rack-test", "~> 2.1"

gem "bcrypt", "~> 3.1"
32 changes: 31 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ GEM
specs:
ansi (1.5.0)
ast (2.4.2)
bcrypt (3.1.18)
diff-lcs (1.4.4)
docile (1.4.0)
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
parallel (1.20.1)
parser (3.0.2.0)
ast (~> 2.4.1)
pg (1.5.3)
rack (2.2.7)
rack-protection (3.0.6)
rack
rack-test (2.1.0)
rack (>= 1.3)
rainbow (3.0.0)
regexp_parser (2.1.1)
rexml (3.2.5)
Expand Down Expand Up @@ -36,6 +46,7 @@ GEM
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -46,21 +57,40 @@ GEM
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
sinatra (3.0.6)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.0.6)
tilt (~> 2.0)
sinatra-contrib (3.0.6)
multi_json
mustermann (~> 3.0)
rack-protection (= 3.0.6)
sinatra (= 3.0.6)
tilt (~> 2.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
tilt (2.1.0)
unicode-display_width (2.0.0)
webrick (1.8.1)

PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1)
pg (~> 1.5)
rack-test (~> 2.1)
rspec
rubocop (= 1.20)
simplecov
simplecov-console
sinatra (~> 3.0)
sinatra-contrib (~> 3.0)
webrick (~> 1.8)

RUBY VERSION
ruby 3.0.2p107

BUNDLED WITH
2.2.26
2.4.12
180 changes: 180 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
require 'sinatra/base'
require 'sinatra/reloader'
require_relative 'lib/peep_repository'
require_relative 'lib/maker_repository'
require_relative 'lib/database_connection'

DatabaseConnection.connect

class Application < Sinatra::Base

enable :sessions

configure :development do
register Sinatra::Reloader
also_reload 'lib/maker_repository'
also_reload 'lib/peep_repository'
end

get '/' do
return erb(:homepage)
end

get '/peeps' do
peep_repo = PeepRepository.new
@maker_repo = MakerRepository.new
@all_peeps = peep_repo.all.sort_by(&:date_posted).reverse
return erb(:peeps)
end

get '/peeps/:id' do
if session[:id].nil?
return erb(:user_peeps_no_session)
else
peep_repo = PeepRepository.new
maker_repo = MakerRepository.new
@maker = maker_repo.find(session[:id])
@makers_peeps = peep_repo.by_maker(session[:id]).sort_by(&:date_posted).reverse
return erb(:user_peeps)
end
end

get '/signup' do
return erb(:signuppage)
end

post '/signup' do
repo = MakerRepository.new
maker = Maker.new
maker.name = params[:name].strip
maker.username = params[:username].strip
maker.email_address = params[:email_address].strip
maker.password = params[:password].strip

if signup_param_validation(maker.name, maker.username, maker.email_address, maker.password)
status 400
else
repo.create(maker)
redirect '/loginpage'
end
end

get '/loginpage' do
return erb(:loginpage)
end

post '/loginpage' do
username = params[:username]
password = params[:password]
repo = MakerRepository.new
@maker = repo.find_by_username(username)

stored_password = BCrypt::Password.new(@maker.password)

if stored_password == password
session[:id] = @maker.id
return erb(:userpage)
else
return erb(:login_error)
end

# if @maker.password == password
# session[:id] = @maker.id
# return erb(:userpage)
# else
# return erb(:login_error)
# end
end

get '/userpage' do
if session[:id].nil?
return erb(:loginpage)
else
repo = MakerRepository.new
@maker = repo.find(session[:id])
return erb(:userpage)
end
end

get '/peep/new' do
if session[:id].nil?
return erb(:post_peep_no_session)
else
return erb(:new_peep)
end
end

post '/peep/new' do
time = Time.new
repo = PeepRepository.new
peep = Peep.new
peep.title = params[:title].strip
peep.content = params[:content].strip
peep.date_posted = time
peep.maker_id = session[:id]

if title_valid?(peep.title)
status 400
else
repo.create(peep)
return erb(:peep_created)
end
end

get '/delete_peep' do
if session[:id].nil?
return erb(:delete_no_session)
else
return erb(:delete_peep)
end
end

post '/delete_peep' do
title = params[:title]
repo = PeepRepository.new
@selected = repo.find_by_title(title)

if @selected.maker_id != session[:id]
status 400 # add an error stating you cannot delete a peep which you have not posted.
else
id = @selected.id
repo.delete(id)
return erb(:peep_deleted)
end
end

get '/update_details' do
if session[:id].nil?
return erb(:update_no_session)
else
return erb(:update_maker)
end
end

post '/update_details' do
@new_name = params[:name]
repo = MakerRepository.new
maker = repo.find(session[:id])
maker.name = @new_name
repo.update(maker)
return erb(:updated_maker)
end

get '/logout' do
session[:id] = nil
return redirect('/')
end

def title_valid?(title)
repo = PeepRepository.new
return repo.all.any? { |row| row.title == title } || title.empty? || title.nil?
end

def signup_param_validation(name, username, email_address, password)
repo = MakerRepository.new
all = [name, username, email_address, password]
return (repo.all.any? { |row| row.username == username } ||
repo.all.any? { |row| row.email_address == email_address } ||
all.any?(nil) || all.any?(&:empty?))
end
end
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require './app'
run Application
21 changes: 21 additions & 0 deletions lib/database_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'pg'

class DatabaseConnection
def self.connect
if ENV['ENV'] == 'test'
database_name = 'chitter_base_test'
else
database_name = 'chitter_base'
end
@connection = PG.connect({ host: '127.0.0.1', dbname: database_name })
end

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
3 changes: 3 additions & 0 deletions lib/maker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Maker
attr_accessor :id, :name, :username, :email_address, :password
end
57 changes: 57 additions & 0 deletions lib/maker_repository.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require_relative 'maker'
require 'bcrypt'

class MakerRepository

def all
result = DatabaseConnection.exec_params('SELECT * FROM makers;', [])
makers = []
result.each do |row|
maker = Maker.new
maker.id = row['id'].to_i
maker.name = row['name']
maker.username = row['username']
maker.email_address = row['email_address']
maker.password = row['password']
makers << maker
end
makers
end

def find_by_username(username)
sql = 'SELECT * FROM makers WHERE username = $1;'
result = DatabaseConnection.exec_params(sql, [username])
maker = Maker.new
maker.id = result[0]['id'].to_i
maker.name = result[0]['name']
maker.username = result[0]['username']
maker.email_address = result[0]['email_address']
maker.password = result[0]['password']
return maker
end

def find(id)
sql = 'SELECT * FROM makers WHERE id = $1;'
result = DatabaseConnection.exec_params(sql, [id])
maker = Maker.new
maker.id = result[0]['id'].to_i
maker.name = result[0]['name']
maker.username = result[0]['username']
maker.email_address = result[0]['email_address']
maker.password = result[0]['password']
return maker
end

def create(maker_obj)
encrypted_password = BCrypt::Password.create(maker_obj.password)
sql = 'INSERT INTO makers(name, username, email_address, password) VALUES($1, $2, $3, $4);'
params = [maker_obj.name, maker_obj.username, maker_obj.email_address, encrypted_password]
result = DatabaseConnection.exec_params(sql, params)
end

def update(maker)
sql = 'UPDATE makers SET name = $1 WHERE id = $2;'
params = [maker.name, maker.id]
update = DatabaseConnection.exec_params(sql, params)
end
end
3 changes: 3 additions & 0 deletions lib/peep.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Peep
attr_accessor :id, :title, :content, :date_posted, :maker_id
end
Loading