diff --git a/.gitignore b/.gitignore index d1a1edf06f..e4e0c084fe 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ # Local cache of Rubocop remote config .rubocop-* +seeds_chitter.sql \ No newline at end of file diff --git a/Gemfile b/Gemfile index b1a320395a..89574ebdd8 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,20 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "pg", "~> 1.4" + +gem "sinatra", "~> 3.0" +gem "sinatra-contrib", "~> 3.0" +gem "webrick", "~> 1.8" +gem "rackup", "~> 1.0" + +gem "activerecord", "~> 7.0" +gem "sinatra-activerecord", "~> 2.0" +gem "rake", "~> 13.0" + +gem "rack", "~> 2.2" + +gem "rack-test", "~> 2.1" + +gem "bcrypt", "~> 3.1" diff --git a/Gemfile.lock b/Gemfile.lock index 66064703c7..49f095c6d9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,14 +1,42 @@ GEM remote: https://rubygems.org/ specs: + activemodel (7.0.4.3) + activesupport (= 7.0.4.3) + activerecord (7.0.4.3) + activemodel (= 7.0.4.3) + activesupport (= 7.0.4.3) + activesupport (7.0.4.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) ansi (1.5.0) ast (2.4.2) + bcrypt (3.1.18) + concurrent-ruby (1.2.2) diff-lcs (1.4.4) docile (1.4.0) + i18n (1.12.0) + concurrent-ruby (~> 1.0) + minitest (5.18.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.4.6) + rack (2.2.6.4) + rack-protection (3.0.6) + rack + rack-test (2.1.0) + rack (>= 1.3) + rackup (1.0.0) + rack (< 3) + webrick rainbow (3.0.0) + rake (13.0.6) regexp_parser (2.1.1) rexml (3.2.5) rspec (3.10.0) @@ -36,6 +64,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,21 +75,50 @@ 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-activerecord (2.0.26) + activerecord (>= 4.1) + sinatra (>= 1.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) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) unicode-display_width (2.0.0) + webrick (1.8.1) PLATFORMS ruby DEPENDENCIES + activerecord (~> 7.0) + bcrypt (~> 3.1) + pg (~> 1.4) + rack (~> 2.2) + rack-test (~> 2.1) + rackup (~> 1.0) + rake (~> 13.0) rspec rubocop (= 1.20) simplecov simplecov-console + sinatra (~> 3.0) + sinatra-activerecord (~> 2.0) + sinatra-contrib (~> 3.0) + webrick (~> 1.8) RUBY VERSION ruby 3.0.2p107 BUNDLED WITH - 2.2.26 + 2.4.12 diff --git a/README.md b/README.md index 465eda879b..0e04e387df 100644 --- a/README.md +++ b/README.md @@ -1,123 +1,5 @@ -Chitter Challenge +My Chitter Challenge ================= -* Feel free to use Google, your notes, books, etc. but work on your own -* If you refer to the solution of another coach or student, please put a link to that in your README -* If you have a partial solution, **still check in a partial solution** -* You must submit a pull request to this repo with your code by 10am Monday morning + -Challenge: -------- - -As usual please start by forking this repo. - -We are going to write a small Twitter clone that will allow the users to post messages to a public stream. - -Features: -------- - -``` -STRAIGHT UP - -As a Maker -So that I can let people know what I am doing -I want to post a message (peep) to chitter - -As a maker -So that I can see what others are saying -I want to see all peeps in reverse chronological order - -As a Maker -So that I can better appreciate the context of a peep -I want to see the time at which it was made - -As a Maker -So that I can post messages on Chitter as me -I want to sign up for Chitter - -HARDER - -As a Maker -So that only I can post messages on Chitter as me -I want to log in to Chitter - -As a Maker -So that I can avoid others posting messages on Chitter as me -I want to log out of Chitter - -ADVANCED - -As a Maker -So that I can stay constantly tapped in to the shouty box of Chitter -I want to receive an email if I am tagged in a Peep -``` - -Technical Approach: ------ - -In the last two weeks, you integrated a database using the `pg` gem and Repository classes. You also implemented small web applications using Sinatra, RSpec, HTML and ERB views to make dynamic webpages. You can continue to use this approach when building Chitter Challenge. - -You can refer to the [guidance on Modelling and Planning a web application](https://github.com/makersacademy/web-applications/blob/main/pills/modelling_and_planning_web_application.md), to help you in planning the different web pages you will need to implement this challenge. If you'd like to deploy your app to Heroku so other people can use it, [you can follow this guidance](https://github.com/makersacademy/web-applications/blob/main/html_challenges/07_deploying.md). - -If you'd like more technical challenge now, try using an [Object Relational Mapper](https://en.wikipedia.org/wiki/Object-relational_mapping) as the database interface, instead of implementing your own Repository classes. - -Some useful resources: -**Ruby Object Mapper** -- [ROM](https://rom-rb.org/) - -**ActiveRecord** -- [ActiveRecord ORM](https://guides.rubyonrails.org/active_record_basics.html) -- [Sinatra & ActiveRecord setup](https://learn.co/lessons/sinatra-activerecord-setup) - -Notes on functionality: ------- - -* You don't have to be logged in to see the peeps. -* Makers sign up to chitter with their email, password, name and a username (e.g. samm@makersacademy.com, password123, Sam Morgan, sjmog). -* The username and email are unique. -* Peeps (posts to chitter) have the name of the maker and their user handle. -* Your README should indicate the technologies used, and give instructions on how to install and run the tests. - -Bonus: ------ - -If you have time you can implement the following: - -* In order to start a conversation as a maker I want to reply to a peep from another maker. - -And/Or: - -* Work on the CSS to make it look good. - -Good luck and let the chitter begin! - -Code Review ------------ - -In code review we'll be hoping to see: - -* All tests passing -* High [Test coverage](https://github.com/makersacademy/course/blob/main/pills/test_coverage.md) (>95% is good) -* The code is elegant: every class has a clear responsibility, methods are short etc. - -Reviewers will potentially be using this [code review rubric](docs/review.md). Referring to this rubric in advance may make the challenge somewhat easier. You should be the judge of how much challenge you want at this moment. - -Notes on test coverage ----------------------- - -Please ensure you have the following **AT THE TOP** of your spec_helper.rb in order to have test coverage stats generated -on your pull request: - -```ruby -require 'simplecov' -require 'simplecov-console' - -SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ - SimpleCov::Formatter::Console, - # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter -]) -SimpleCov.start -``` - -You can see your test coverage when you run your tests. If you want this in a graphical form, uncomment the `HTMLFormatter` line and see what happens! diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..5f9d70a637 --- /dev/null +++ b/app.rb @@ -0,0 +1,74 @@ +require 'sinatra' +require "sinatra/reloader" +require 'sinatra/activerecord' +require_relative './lib/peep' +require_relative './lib/user' +require_relative 'lib/database_connection' +require 'bcrypt' + +# Need to edit database_connection later so this would work when deployed +set :database, { adapter: "postgresql", database: "chitter_test" } + +class Application < Sinatra::Base + configure :development do + register Sinatra::Reloader + register Sinatra::ActiveRecordExtension + also_reload 'lib/peep.rb' + also_reload 'lib/user.rb' + also_reload 'style.css' + end + + get '/' do + @peeps = Peep.all + return erb(:index) + end + + + # look at other http methods - patch (db update/edit), delete + get '/new-peep' do + return erb(:new_peep) + end + + # usually use /peep as convention - restful API + post '/new-peep' do + #params[:content] + peep = Peep.create(content: params[:content]) + return erb(:peeped) + # Need to add user id of person logged in and the created_date_time + end + + get '/new-user' do + return erb(:new_user) + end + + post '/new-user' do + user = User.create( + username: params[:username], + email: params[:email], + full_name: params[:full_name], + password: password_encryption(params[:password]) + ) + return erb(:user_created) + end + + get '/login' do + return erb(:login) + end + + post '/login' do + user = User.find_by( email: params[:email], password: params[:password]) + if user == nil + return 'Incorrect email/password' + else + + return redirect('/') + end + end + + private + def password_encryption(password) + encrypted_password = BCrypt::Password.create(password) + end +end + + diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..c41dba5056 --- /dev/null +++ b/config.ru @@ -0,0 +1,3 @@ +# file: config.ru +require './app' +run Application diff --git a/docs/20230413_ChitterDiagram.png b/docs/20230413_ChitterDiagram.png new file mode 100644 index 0000000000..4c2cc3c427 Binary files /dev/null and b/docs/20230413_ChitterDiagram.png differ diff --git a/docs/design.md b/docs/design.md new file mode 100644 index 0000000000..0012f76d20 --- /dev/null +++ b/docs/design.md @@ -0,0 +1,157 @@ +# Design for Chitter Challenge + +## 1. User Stories + +STRAIGHT UP + +As a Maker +So that I can let people know what I am doing +I want to post a message (peep) to chitter + +As a maker +So that I can see what others are saying +I want to see all peeps in reverse chronological order + +As a Maker +So that I can better appreciate the context of a peep +I want to see the time at which it was made + +As a Maker +So that I can post messages on Chitter as me +I want to sign up for Chitter + +Key Nouns +- peep (message) +- created time + +Key Verbs: +- post +- see all (reverse chronological order) +- sign up + + +HARDER + +As a Maker +So that only I can post messages on Chitter as me +I want to log in to Chitter + +As a Maker +So that I can avoid others posting messages on Chitter as me +I want to log out of Chitter + +ADVANCED + +As a Maker +So that I can stay constantly tapped in to the shouty box of Chitter +I want to receive an email if I am tagged in a Peep + + +## 2. Initial Assumptions + +- Users don't need to be logged in to see all peeps including name, username, created datetime + +## 3. Future Enhancement Ideas + +- Could allow user to login with email or username +- Instead of failing for incorrect credentials, prompts user to re-enter and sends back to login/register page +- Message on page to show incorrect credentials + + +## 4. Database and Table Definitions and Relationships + +createdb chitter +createdb chitter_test + +Table: peeps +id: SERIAL +created_date_time: timestamp +content: text + +Table: users +id: SERIAL +name: text +email: text +username: text +password: text + +'users' can have many 'peeps' but a peep can only belong to one user: users (1) -> peeps (M) + +## 5. Write SQL for seed files + +psql -h 127.0.0.1 chitter_test < spec/seeds/table_creation.sql +psql -h 127.0.0.1 chitter_test < spec/seeds/seeds_chitter.sql + +## 6. Database Connection Class + +```ruby + +# 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_test' + 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 +``` + +## 7. Edit spec_helper.rb + +Add this to the top of the file + +```ruby +require 'database_connection' + +ENV['ENV'] = 'test' + +DatabaseConnection.connect +``` + +## 8. Create Model Classes + +```ruby +def user +attr_accessor :id ... +end +``` + +## 9. TDD Repository Classes + +UserRepository +-> all +-> find +-> create +-> update +-> delete + diff --git a/lib/database_connection.rb b/lib/database_connection.rb new file mode 100644 index 0000000000..ec9c26cd64 --- /dev/null +++ b/lib/database_connection.rb @@ -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_test' + 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 diff --git a/lib/peep.rb b/lib/peep.rb new file mode 100644 index 0000000000..6d817b95c9 --- /dev/null +++ b/lib/peep.rb @@ -0,0 +1,3 @@ +# Active Record Model Class for peeps table +class Peep < ActiveRecord::Base +end diff --git a/lib/user.rb b/lib/user.rb new file mode 100644 index 0000000000..43988f6107 --- /dev/null +++ b/lib/user.rb @@ -0,0 +1,3 @@ +# Active Record Model Class for users table +class User < ActiveRecord::Base +end diff --git a/public/style.css b/public/style.css new file mode 100644 index 0000000000..2d13592873 --- /dev/null +++ b/public/style.css @@ -0,0 +1,11 @@ +body { + font-family: Verdana, Arial, Helvetica, sans-serif; + margin: 15; + padding: 15; +} + +h1 { + font-size: 30px; + font-weight: bold; + color: green; +} diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb new file mode 100644 index 0000000000..0353df7a3c --- /dev/null +++ b/spec/integration/app_spec.rb @@ -0,0 +1,84 @@ +require "spec_helper" +require "rack/test" +require_relative '../../app' + +def reset_tables + seed_sql = File.read('spec/seeds/seeds_chitter.sql') + connection = PG.connect({ host: '127.0.0.1', dbname: 'chitter_test' }) + connection.exec(seed_sql) +end + +describe Application do + include Rack::Test::Methods + let(:app) { Application.new } + before(:each) { reset_tables } + + context "get '/' route " do + it "returns 200 OK and a list of peeps" do + response = get('/') + expect(response.status).to eq 200 + expect(response.body).to include('My first peep') + end + end + + context "get '/new-peep route" do + it "returns 200 OK and the form for a new peep" do + response = get('/new-peep') + expect(response.status).to eq 200 + expect(response.body).to include '
+ + \ No newline at end of file diff --git a/views/new_peep.erb b/views/new_peep.erb new file mode 100644 index 0000000000..039cd336ed --- /dev/null +++ b/views/new_peep.erb @@ -0,0 +1,15 @@ + + +Your peep was successfully posted
\ No newline at end of file diff --git a/views/user_created.erb b/views/user_created.erb new file mode 100644 index 0000000000..74a75d845a --- /dev/null +++ b/views/user_created.erb @@ -0,0 +1 @@ +Your account was successfully created
\ No newline at end of file