From 58534f318cbb4c56ff249a2e17eb1f9784443027 Mon Sep 17 00:00:00 2001 From: Lamia Haider Date: Mon, 8 Mar 2021 19:28:20 -0500 Subject: [PATCH] Done. --- Gemfile | 1 + Gemfile.lock | 110 ++++++++++++---------- Rakefile | 6 +- app/controllers/application_controller.rb | 61 ++++++++++++ app/controllers/helpers.rb | 12 +++ app/controllers/tweets_controller.rb | 70 ++++++++++++++ app/controllers/users_controller.rb | 11 +++ app/models/user.rb | 11 +++ app/views/index.erb | 5 + app/views/layout.erb | 3 +- app/views/signup.erb | 25 +++++ app/views/tweets/edit.erb | 7 ++ app/views/tweets/new.erb | 6 +- app/views/tweets/show.erb | 11 +++ app/views/tweets/tweets.erb | 8 ++ app/views/users/login.erb | 16 ++++ app/views/users/show.erb | 3 + db/development.sqlite | Bin 0 -> 32768 bytes db/migrate/20210305052315_createusers.rb | 9 ++ db/migrate/20210305052329_createtweets.rb | 8 ++ db/schema.rb | 26 +++++ db/test.sqlite | Bin 0 -> 32768 bytes 22 files changed, 354 insertions(+), 55 deletions(-) create mode 100644 app/controllers/helpers.rb create mode 100644 app/views/index.erb create mode 100644 app/views/signup.erb create mode 100644 app/views/tweets/edit.erb create mode 100644 app/views/tweets/show.erb create mode 100644 app/views/tweets/tweets.erb create mode 100644 app/views/users/login.erb create mode 100644 app/views/users/show.erb create mode 100644 db/development.sqlite create mode 100644 db/migrate/20210305052315_createusers.rb create mode 100644 db/migrate/20210305052329_createtweets.rb create mode 100644 db/schema.rb diff --git a/Gemfile b/Gemfile index 691cf319c8b..1b2bf186889 100644 --- a/Gemfile +++ b/Gemfile @@ -11,6 +11,7 @@ gem 'shotgun' gem 'pry' gem 'bcrypt' gem "tux" +gem 'rack-flash3' group :test do gem 'rspec' diff --git a/Gemfile.lock b/Gemfile.lock index 6019d82dce6..c0c2180d098 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,55 +1,63 @@ GEM remote: http://rubygems.org/ specs: - activemodel (6.0.1) - activesupport (= 6.0.1) - activerecord (6.0.1) - activemodel (= 6.0.1) - activesupport (= 6.0.1) - activesupport (6.0.1) + activemodel (6.0.3.5) + activesupport (= 6.0.3.5) + activerecord (6.0.3.5) + activemodel (= 6.0.3.5) + activesupport (= 6.0.3.5) + activesupport (6.0.3.5) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - zeitwerk (~> 2.2) + zeitwerk (~> 2.2, >= 2.2.2) addressable (2.7.0) public_suffix (>= 2.0.2, < 5.0) - bcrypt (3.1.13) + bcrypt (3.1.16) bond (0.5.1) - capybara (3.29.0) + capybara (3.35.3) addressable mini_mime (>= 0.1.3) nokogiri (~> 1.8) rack (>= 1.6.0) rack-test (>= 0.6.3) - regexp_parser (~> 1.5) + regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) - coderay (1.1.2) - concurrent-ruby (1.1.5) + coderay (1.1.3) + concurrent-ruby (1.1.8) daemons (1.3.1) - database_cleaner (1.7.0) - diff-lcs (1.3) + database_cleaner (2.0.1) + database_cleaner-active_record (~> 2.0.0) + database_cleaner-active_record (2.0.0) + activerecord (>= 5.a) + database_cleaner-core (~> 2.0.0) + database_cleaner-core (2.0.1) + diff-lcs (1.4.4) eventmachine (1.2.7) - i18n (1.7.0) + i18n (1.8.9) concurrent-ruby (~> 1.0) - method_source (0.9.2) + method_source (1.0.0) mini_mime (1.0.2) - mini_portile2 (2.4.0) - minitest (5.13.0) - mustermann (1.0.3) - nokogiri (1.10.8) - mini_portile2 (~> 2.4.0) - pry (0.12.2) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (4.0.1) + minitest (5.14.4) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + nokogiri (1.11.1-x86_64-darwin) + racc (~> 1.4) + pry (0.14.0) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) + racc (1.5.2) rack (2.2.3) - rack-protection (2.0.7) + rack-flash3 (1.0.5) + rack + rack-protection (2.1.0) rack rack-test (1.1.0) rack (>= 1.0, < 3) - rake (13.0.1) - regexp_parser (1.6.0) + rake (13.0.3) + regexp_parser (2.1.1) require_all (3.0.0) ripl (0.7.1) bond (~> 0.5.1) @@ -59,31 +67,32 @@ GEM rack (>= 1.0) rack-test (>= 0.5) ripl (>= 0.3.5) - rspec (3.9.0) - rspec-core (~> 3.9.0) - rspec-expectations (~> 3.9.0) - rspec-mocks (~> 3.9.0) - rspec-core (3.9.0) - rspec-support (~> 3.9.0) - rspec-expectations (3.9.0) + rspec (3.10.0) + rspec-core (~> 3.10.0) + rspec-expectations (~> 3.10.0) + rspec-mocks (~> 3.10.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-mocks (3.9.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.9.0) - rspec-support (3.9.0) + rspec-support (~> 3.10.0) + rspec-support (3.10.2) + ruby2_keywords (0.0.4) shotgun (0.9.2) rack (>= 1.0) - sinatra (2.0.7) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.14) - activerecord (>= 3.2) + sinatra-activerecord (2.0.22) + activerecord (>= 4.1) sinatra (>= 1.0) - sqlite3 (1.4.1) - thin (1.7.2) + sqlite3 (1.4.2) + thin (1.8.0) daemons (~> 1.0, >= 1.0.9) eventmachine (~> 1.0, >= 1.0.4) rack (>= 1, < 3) @@ -94,14 +103,14 @@ GEM ripl-multi_line (>= 0.2.4) ripl-rack (>= 0.2.0) sinatra (>= 1.2.1) - tzinfo (1.2.5) + tzinfo (1.2.9) thread_safe (~> 0.1) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (2.2.1) + zeitwerk (2.4.2) PLATFORMS - ruby + x86_64-darwin-19 DEPENDENCIES activerecord (~> 6.0.0) @@ -109,6 +118,7 @@ DEPENDENCIES capybara database_cleaner pry + rack-flash3 rack-test rake require_all @@ -121,4 +131,4 @@ DEPENDENCIES tux BUNDLED WITH - 2.0.2 + 2.2.7 diff --git a/Rakefile b/Rakefile index 6604e339c10..a1b7dab2d18 100644 --- a/Rakefile +++ b/Rakefile @@ -5,4 +5,8 @@ require 'sinatra/activerecord/rake' -# Type `rake -T` on your command line to see the available rake tasks. \ No newline at end of file +# Type `rake -T` on your command line to see the available rake tasks. + +task :console do + Pry.start +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9ac085ac3c3..db6d0d34212 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,10 +1,71 @@ require './config/environment' +require 'rack-flash' class ApplicationController < Sinatra::Base + use Rack::Flash configure do + register Sinatra::ActiveRecordExtension + enable :sessions + set :session_secret, "my_application_secret" set :public_folder, 'public' set :views, 'app/views' end + get '/' do + erb :index + end + + get '/signup' do + if session[:user_id] == nil + erb :signup + else + redirect '/tweets' + end + end + + post '/signup' do + user = User.new(:username => params[:username], :email=> params[:email], :password => params[:password]) + if user.username.empty? || user.email.empty? || !user.save + flash[:notice] = "User creation unsuccessful, please try again." + redirect '/signup' + else user.save + session[:user_id] = user.id + redirect '/tweets' + end + end + + get '/login' do + if Helpers.is_logged_in?(session) + redirect '/tweets' + else + erb :'/users/login' + end + end + + post "/login" do + @user = User.find_by(:username => params[:username]) + + if @user && @user.authenticate(params[:password]) + session[:user_id] = @user.id + redirect "/tweets" + else + flash[:notice] = "Login unsuccessful, please try again." + redirect "/login" + end + end + + get '/logout' do + if session[:user_id] == nil + redirect "/" + else + session.clear + redirect '/login' + end + end + + + + + end diff --git a/app/controllers/helpers.rb b/app/controllers/helpers.rb new file mode 100644 index 00000000000..04d513f83fd --- /dev/null +++ b/app/controllers/helpers.rb @@ -0,0 +1,12 @@ +class Helpers < ApplicationController + + + def self.is_logged_in?(session) + !!session[:user_id] + end + + def self.current_user(user) + @user= User.find(id: user[:user_id]) + end + +end \ No newline at end of file diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c9f05301dc5..41343f3d44e 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,74 @@ class TweetsController < ApplicationController + get '/tweets' do + if Helpers.is_logged_in?(session) + @user = User.find_by(id: session[:user_id]) + erb :'/tweets/tweets' + else + redirect '/login' + end + end + get '/tweets/new' do + if Helpers.is_logged_in?(session) + erb :'/tweets/new' + else + redirect '/login' + end + end + + post '/tweets' do + if params[:content] != "" + @tweet = Tweet.create(:content => params[:content]) + @tweet.user = User.find_by(:id => session[:user_id]) + @tweet.save + redirect "/tweets" + else + redirect "/tweets/new" + end + + end + + get '/tweets/:id' do + if Helpers.is_logged_in?(session) + @tweet = Tweet.find(params[:id]) + erb :'/tweets/show' + else + redirect '/login' + end + end + + delete '/tweets/:id/delete' do + @tweet= Tweet.find(params[:id]) + if Helpers.is_logged_in?(session) && @tweet.user.id == session[:user_id] + @tweet.delete + redirect '/tweets' + else + redirect '/login' + end + end + + get '/tweets/:id/edit' do + if Helpers.is_logged_in?(session) == false + redirect '/login' + else + @tweet = Tweet.find(params[:id]) + erb :'/tweets/edit' + end + + end + + patch '/tweets/:id/edit' do + if Helpers.is_logged_in?(session) + @tweet = Tweet.find(params[:id]) + end + if params[:updated_content] != "" + @tweet.content = params[:updated_content] + @tweet.save + end + redirect "/tweets/#{@tweet.id}/edit" + end end + + + \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a7d1cab4e29..e56caf133ec 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,15 @@ class UsersController < ApplicationController + get '/users/:slug' do + if Helper.is_logged_in?(session) + @user = User.all.find_by_slug(params[:slug]) + erb :'/users/show' + else + redirect '/login' + end + end + + + end diff --git a/app/models/user.rb b/app/models/user.rb index 7d5543b8618..887c3322464 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,15 @@ class User < ActiveRecord::Base has_secure_password has_many :tweets + + + def slug + self.username.gsub(" ", "-").gsub(/[^\w\-]/,"").downcase + end + + + def self.find_by_slug(slug) + self.all.find{|s| s.slug == slug} + end + end diff --git a/app/views/index.erb b/app/views/index.erb new file mode 100644 index 00000000000..a0723092882 --- /dev/null +++ b/app/views/index.erb @@ -0,0 +1,5 @@ +

Welcome

+ +See Tweets Here +
+ Create a New Tweet diff --git a/app/views/layout.erb b/app/views/layout.erb index 88e49546076..b79e3e0513d 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -7,8 +7,7 @@

Welcome to Fwitter!

-

This is located in layout.erb!

-

Don't forget to yield your views!

+ <%= yield %>
diff --git a/app/views/signup.erb b/app/views/signup.erb new file mode 100644 index 00000000000..23f68dfbc6b --- /dev/null +++ b/app/views/signup.erb @@ -0,0 +1,25 @@ +

Sign Up

+ +<% if flash.has?(:notice) %> + <%= flash[:notice] %> +<% end %> + +
+
+ + +
+ + + + +
+ + + + + +
+ + + diff --git a/app/views/tweets/edit.erb b/app/views/tweets/edit.erb new file mode 100644 index 00000000000..e38cc6f3bf0 --- /dev/null +++ b/app/views/tweets/edit.erb @@ -0,0 +1,7 @@ +
+ + + + + +
\ No newline at end of file diff --git a/app/views/tweets/new.erb b/app/views/tweets/new.erb index 875ade026b1..5d01f6306c2 100644 --- a/app/views/tweets/new.erb +++ b/app/views/tweets/new.erb @@ -1,16 +1,18 @@ <% if Helpers.is_logged_in?(session) %> <%= @error %> +
Content:
Create Your Tweet: - - +
+ + <% else %>

You Must Be Logged in to Make Tweets

<% end %> diff --git a/app/views/tweets/show.erb b/app/views/tweets/show.erb new file mode 100644 index 00000000000..3833e48282f --- /dev/null +++ b/app/views/tweets/show.erb @@ -0,0 +1,11 @@ +

<%=@tweet.content%>

+
+ + Edit Tweet +
+ +
+ + +
+ diff --git a/app/views/tweets/tweets.erb b/app/views/tweets/tweets.erb new file mode 100644 index 00000000000..50c8f3e5934 --- /dev/null +++ b/app/views/tweets/tweets.erb @@ -0,0 +1,8 @@ + +

Welcome, here are your tweets!

+ +<% Tweet.all.each do |t| %> + <%= t.content %> + + <% end %> + diff --git a/app/views/users/login.erb b/app/views/users/login.erb new file mode 100644 index 00000000000..e052b91d152 --- /dev/null +++ b/app/views/users/login.erb @@ -0,0 +1,16 @@ +

Log In

+ + +<% if flash.has?(:notice) %> + <%= flash[:notice] %> +<% end %> +
+ + +
+ + + +
+ + diff --git a/app/views/users/show.erb b/app/views/users/show.erb new file mode 100644 index 00000000000..827670edc47 --- /dev/null +++ b/app/views/users/show.erb @@ -0,0 +1,3 @@ +<% @user.tweets.each do |t| %> + <%= t.content %> + <% end %> diff --git a/db/development.sqlite b/db/development.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..4a6599ad778fbb855042b4f5fefec4434345252c GIT binary patch literal 32768 zcmeI*%WmRU7zc2hYZx_g+G$0j(P&(fMXbyKhA@*(HvGoBaz{KL(2)<$&ut9jusF8I#ZobZY8 z-NL!WSvqSo7~WsRA84@ zt{OzA580m5_`W|F4u|lJvzFG;^JIPo-TG0 zhC~}o1>fvbVpF9-SeWei)K+!6`dGTy-#KxI&Wf8ik@CJk__GwxsfI#N-kuyXuuXTl zab36#`MO&F2_Fo{V*L2Q*(K5JFxrN$w)@0ZO~bM-S9qpjuB>-dhU^+(JO8_kl=cL| zwb)H5Txqx=U#ar4FPYo7pJyLD!Y`cg8=Ig&00Izz00bZa0SG_<0uX=z1R!wh1QtCW zDG^V|@nk#|PbHFxj~Dsr8aZ``W#tiGJN^IHw=O{h3;_s000Izz00bZa0SG_<0uX?} zdtQm3?*4n;^-7`1Dmmrc|L28oIX0j`00Izz00bZa0SG_<0uX=z1R(JL5ZLAY%ggCB zHHK>6G<5b)D++Cqfo3;YmD96-6i+^ir^NV^r}6mHpgI zl#eQzSTynJmexFN7xJZIy;9Doqqe53pFZC7UenUce5ICO&lH{;^5(u)s_hi5c66YP zTGh`z0Zx*}eoNJyNBG%9)0}4jx=YW9sYRQn{x+P!@Ht&SI!PUOPd8fXp58myDDOu% z<rYh%C7p3f-CiKH`Uvr7#h_L54jqZZ1Y zPIr{49rdzGw-kMmH4ls!Jw7RqSk!y51Cm?cJKjxJii)1v4427JxWVrd>3Awcw{r@+@&%*b@ufk6=u7UI* z009U<00Izz00bZa0SG_<0=HaX&gbJk2z7hi9(z}%$H&RMW|}>b*rMH0Ezx~Jre~h_ OaSz;|0c_Q1fAlw)64pZi literal 0 HcmV?d00001 diff --git a/db/migrate/20210305052315_createusers.rb b/db/migrate/20210305052315_createusers.rb new file mode 100644 index 00000000000..6c420a197c6 --- /dev/null +++ b/db/migrate/20210305052315_createusers.rb @@ -0,0 +1,9 @@ +class Createusers < ActiveRecord::Migration[6.0] + def change + create_table :users do |u| + u.string :username + u.string :email + u.string :password_digest + end + end +end diff --git a/db/migrate/20210305052329_createtweets.rb b/db/migrate/20210305052329_createtweets.rb new file mode 100644 index 00000000000..f67105e23b4 --- /dev/null +++ b/db/migrate/20210305052329_createtweets.rb @@ -0,0 +1,8 @@ +class Createtweets < ActiveRecord::Migration[6.0] + def change + create_table :tweets do |t| + t.string :content + t.integer :user_id + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000000..03a0e502343 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,26 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2021_03_05_052329) do + + create_table "tweets", force: :cascade do |t| + t.string "content" + t.integer "user_id" + end + + create_table "users", force: :cascade do |t| + t.string "username" + t.string "email" + t.string "password_digest" + end + +end diff --git a/db/test.sqlite b/db/test.sqlite index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8ff6e5bd034d37089595e4d9e59ff62f328a5c94 100644 GIT binary patch literal 32768 zcmeI*-%is|90%}rgAIsKl$eGXV>7s*;m_KRqTme$7bFZAFhbCn=CB<`Wo@B71B_SW z348>72z?oR1{1w7UV1tlZv4CO%8~EMTH4>~IsKi_*)9&vU}G)kFskhOflHW@5&8vD z6lN7g5QG7~b@8oHI`~1fHHz3cxaH@7FtPNZSNbI+?|l@c=hElCM}0qf-}kEg6a@kh zfB*y_009U<00IzzK#M>vkxa^igJP}B$WDc_V@g?gzVDh}&sv47Qn2Q7StWHIOerHN zr<770kI^y>l>GYQvbDagEM>P9YqPMrxWZd4XIBc!$|~PBbGb1kRrLAQ9!n`lBq;8Y zfQPCf4eX|l)O;c#kB*Al^)$l63STwZAw8^8uShSQCR-A_R5{`imyYq4YVC1nQkG@$ zbgv0chYi7;|qtfMvi>n@1~vrpe$WN|SU&C|sv;fPeKEy))HN*FEKgr~{x zPnqM=kty|}eKc_gQN_)ds0$ql`L!tuj#r{5e4g~@bfB*y_009U<00Izz00bZafwmLqkHu6&GjuJZnVM;2jOl){HKLn6 zd{i;%EL#7cwcP-bFa#g~0SG_<0uX=z1Rwwb2tWV=58|rWy8MsbkE`9yA>E9g|BKQW zfxl27009U<00Izz00bZa0SG_<0ucDG1YU}rLk}&BdPh#+doKU(3gchhh*tLFT4r1` z747Marq39ei7CU>CMR#MiJt$9(szNsP#^#S2tWV=5P$##AOHafKmY;|xGMsE@uBXf z-y1~F|M?&N6QoafWq=q51Rwwb2tWV=5P$##AOHafK%fl;-iQfdXuQ@L?mLWCXs8>R z#^EgSD-%WE9X80YZVb<-^*J}ck@M%RwVhYS_HKT`DbUp^Ypd+#Po}F|3+aNBKHPu7 zawPrM)s~)3mKHA7|3BN%1}Q@T0uX=z1Rwwb2tWV=5P$##AaJ`tPj~%)O*Gd3w_l7} a2tWV=5P$##AOHafKmY;|fB*#AS>QJd#CT%> literal 0 HcmV?d00001