diff --git a/Gemfile b/Gemfile index b1a320395a..28d932d4f1 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,14 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "sinatra", "~> 3.0" +gem "sinatra-contrib", "~> 3.0" +gem "webrick", "~> 1.8" +gem "rack-test", "~> 2.1" + +gem "time", "~> 0.2.2" + +gem "pg", "~> 1.5" + +gem "bcrypt", "~> 3.1" diff --git a/Gemfile.lock b/Gemfile.lock index 66064703c7..1c1ed05223 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,11 +3,22 @@ GEM specs: ansi (1.5.0) ast (2.4.2) + bcrypt (3.1.18) + date (3.3.3) 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) @@ -36,6 +47,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) @@ -46,18 +58,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) + time (0.2.2) + date 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) + time (~> 0.2.2) + webrick (~> 1.8) RUBY VERSION ruby 3.0.2p107 diff --git a/README.md b/README.md index 465eda879b..9ddc4f6f58 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,69 @@ -Chitter Challenge +# Chitter Challenge + +![homepage](./images/homepage.png) + +![shoutybox when logged in](./images/shoutybox.png) + +![signup page](./images/sign-up.png) + + +## To Run App +``` + git clone https://github.com/veritywong/chitter-challenge.git + cd chitter-challenge + bundle install + createdb chitter_test + psql -h 127.0.0.1 chitter_challenge < spec/tables_seeds.sql + rspec + rackup + http://localhost:('Port in use')/ +``` + +## Description +This app is the start of a Twitter clone, that allows users to post messages to a public stream. In order to post they must signup and login, however anybody can view the public stream on the 'Shouty Box'. + +## Approach +This was the first web app that we created on the course, so the goal was to get a good understanding of sending HTTP requests from the frontend and how they were then run in the backend. + +I begun by following a design recipe to decide database structure, which in this case includes posts and users tables, which have a many to many relationship. +From there I used TDD to create the user repository and post repository, implementing methods towards creating CRUD functionality. + +Next I worked between the app.rb file and HTML files, completing the features such as signup, login and posting a new peep. + +Passwords are encrypted using bcrypt, and sessions are used to know whether a user is logged in and to determine what they can view. + +## Diagrams +This diagram shows the differente pages in the app. The user can only post or view their account when logged in. + +![Account Diagram](./images/diagram.png) + +Below is a diagram of clicking the link for the stream of posts: + +![Account Diagram](./images/shoutybox-diagram.png) + +## Technology Used +* Ruby +* Sinatra & ERB +* BCrypt +* PostgreSQL +* HTML & CSS + +## Tests +This is a screen shot of the passing tests and test coverage. + +![Account Diagram](./images/passing-tests.png) + + +# Initial Project Information + +Below is the instructuctions given at the start of the project. + +Gems added: +time +sinatra sinatra-contrib webrick rack-test +pg +bcrypt + ================= * Feel free to use Google, your notes, books, etc. but work on your own diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..e60c13a864 --- /dev/null +++ b/app.rb @@ -0,0 +1,123 @@ +# file: app.rb +require 'sinatra/base' +require 'sinatra/reloader' +require 'time' +require_relative 'lib/database_connection' +require_relative 'lib/user_repository' +require_relative 'lib/post_repository' + + +DatabaseConnection.connect + +class Application < Sinatra::Base + enable :sessions + + configure :development do + register Sinatra::Reloader + also_reload 'lib/user_repository' + also_reload 'lib/post_repository' + end + + get '/' do + return erb(:index) + end + + get '/login' do + return erb(:login) + end + + post '/login' do + email = params[:email] + password = params[:password] + + repo = UserRepository.new + @user = repo.find_by_email(email) + + # login_result = repo.sign_in(email, password) + + if repo.sign_in(email, password) == true + session[:user_id] = @user.id + + return erb(:login_success) + else + status 400 + return erb(:login_error) + end + end + + get '/account_page' do + posts = PostRepository.new + @users = UserRepository.new + + @user = @users.find(session[:user_id]) + @peeps = posts.all.reverse + + if session[:user_id] == nil + return redirect('/login') + else + return erb(:account_page) + end + end + + get '/signup' do + return erb(:signup) + end + + post '/signup' do + users = UserRepository.new + new_user = User.new + new_user.name = params[:name] + new_user.username = params[:username] + new_user.email = params[:email] + new_user.password = params[:password] + + users.create(new_user) + + if new_user.name == '' || new_user.name == '