Skip to content

Commit

Permalink
fixed timestamp. added signup page. fixed rspec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joehannis committed Jun 2, 2023
1 parent d74453e commit a34fc34
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 35 deletions.
53 changes: 27 additions & 26 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
require 'active_record'
require 'sinatra'
require "sinatra/reloader"
require 'sinatra/reloader'
require 'active_record'
require_relative 'lib/database_connection'
require_relative 'lib/post'
require_relative 'lib/user'

# Establish the database connection
establish_database_connection

class Application < Sinatra::Base
configure :development do
register Sinatra::Reloader
also_reload 'lib/database_connection'
also_reload 'lib/post'
also_reload 'lib/user'
end

configure do
# Set the default timezone to GMT
ENV['TZ'] = 'GMT'
end

get '/' do
p "Printing from app.rb line 16"
p Post.all_peeps
@posts = Post.all_peeps
@posts = @posts.reverse
return erb(:index)
@posts = Post.all_peeps.reverse
erb :index
end

post '/' do
# def invalid_request_parameters?
# # Are the params nil?
# return true if params[:message] == nil

# # Are they empty strings?
# return true if params[:message]

# return false
# end
# if invalid_request_parameters?
# status 400

# return 'Please enter a message'
# end

# Parameters are valid,
# the rest of the route can execute.
Post.create(time: Time.now, message: params[:message], user_id: 1)
current_time = Time.now + 1 * 60 * 60 # Get the current time with GMT offset
Post.create(time: current_time, message: params[:message], user_id: 1)
redirect '/'
end

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

end
post '/signup' do
User.create(username: params[:username], email: params[:email], password: params[:password])
redirect '/success'
end

get '/success' do
erb(:success)
end
end
3 changes: 1 addition & 2 deletions lib/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ def self.all_peeps
Post.joins(:user).map do |post|
@posts << "#{post.time} #{post.user.username} #{post.message}"
end
p "we are in al_peeps function"
p @posts
@posts
end

def self.create_post(time, message, user_id)
Expand Down
1 change: 1 addition & 0 deletions lib/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def self.all_records
User.all.map do |user|
@users << user
end
@users
end

def self.create_user(username, email, password)
Expand Down
11 changes: 8 additions & 3 deletions spec/integration/application.spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@
it 'returns HTML page' do
response = get('/')
expect(response.status).to eq(200)
# expect(response.body).to include('<a href="/albums/2">Surfer Rosa</a><br />')
# expect(response.body).to include('<a href="/albums/3">Waterloo</a><br />')
# expect(response.body).to include('<a href="/albums/4">Super Trouper</a><br />')
expect(response.body).to include('Chitter')
end
end
context 'Get /signup' do
it 'returns signup page' do
response = get('/signup')
expect(response.status).to eq(200)
expect(response.body).to include('username')
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/post_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'post'
require 'reset_tables'


RSpec.describe Post do
before(:each) do
reset = ResetTables.new
Expand Down
2 changes: 1 addition & 1 deletion spec/reset_tables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def reset_users_table
connection = PG.connect({ host: '127.0.0.1', dbname: 'chitter_test' })
connection.exec(seed_sql)
end
end
end
3 changes: 1 addition & 2 deletions spec/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require 'user'
require 'reset_tables'


RSpec.describe User do
before(:each) do
reset = ResetTables.new
reset.reset_users_table
end
it 'return all users from the database' do
expect(User.all_records.first[:email]).to eq('[email protected]')
expect(User.all_records[0][:email]).to eq('[email protected]')
expect(User.all_records[1][:email]).to eq('[email protected]')
end
it 'creates a new user and returns the table' do
Expand Down
1 change: 1 addition & 0 deletions views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<head></head>
<body>
<h1>Chitter</h1>
<div>Signup Here: <a href="http://localhost:9292/signup">Signup</a></div>
<div>What would you like to say?</div>
<div><form action="/" method="POST">
<input type="text" name="message">
Expand Down
15 changes: 15 additions & 0 deletions views/signup.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<html>
<head></head>
<body>
<h1>Welcome to Chitter!</h1>
<div>Please register your details below</div>
<div><form action="/signup" method="POST">
<div>Username</div>
<input type="text" name="username">
<div>Password</div>
<input type="text" name="password">
<div>Email</div>
<input type="text" name="email">
<input type="submit" value="Signup">
</body>
</html>
7 changes: 7 additions & 0 deletions views/success.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head></head>
<body>
<h1>Success!</h1>
<div><a href="http://localhost:9292/">click here to return to the homepage</a></div>
</body>
</html>

0 comments on commit a34fc34

Please sign in to comment.