From 08e096ba45871d9da7b4f825b2da41d092253930 Mon Sep 17 00:00:00 2001 From: andy-aguilar Date: Wed, 17 Feb 2021 08:09:39 -0600 Subject: [PATCH 1/6] Done. --- app/models/user.rb | 8 ++++++ db/development.sqlite | Bin 0 -> 36864 bytes db/migrate/20210217140035_create_users.rb | 9 +++++++ db/migrate/20210217140042_create_tweets.rb | 8 ++++++ db/schema.rb | 27 +++++++++++++++++++++ db/test.sqlite | Bin 0 -> 36864 bytes 6 files changed, 52 insertions(+) create mode 100644 db/development.sqlite create mode 100644 db/migrate/20210217140035_create_users.rb create mode 100644 db/migrate/20210217140042_create_tweets.rb create mode 100644 db/schema.rb diff --git a/app/models/user.rb b/app/models/user.rb index 7d5543b8618..7947c75b6ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,12 @@ class User < ActiveRecord::Base has_secure_password has_many :tweets + + def slug + self.username.downcase.gsub(" ", "-") + end + + def self.find_by_slug(slug) + self.all.find { |user| user.slug == slug } + end end diff --git a/db/development.sqlite b/db/development.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..dde4c0ab0a2b1f3bc4f7533fe684c7c71930a481 GIT binary patch literal 36864 zcmeI)U2ob}7{GBG!rPRDY&TXkRXuWUV)Z3JAW_M)t+J~9i&oHUI+U1TRWRQeOyIa`*$Ts+~&+&79$3i-B;HMw=y-0~y-Jl~Q zv1zOrrfEDD!Z3`KUUPaKmw10+yrW;uiTAVKri}ZKeqYM}X=E3-jQqp=Z_D2Ec32X$eOYS?1kK5xQy2I)-3TGf|2iR8<+8l5rPX3tx0RKMG= zO;+|=w($H&ov1)OJKTL*Jvp=b*-nhdPCF?^m??ve^=Peu0Gq3Y|)oN z^Hc^pG)`dLkn=2^w(i_9kB4c5XKg)eazl02Q+`uj+RaQQcIn`{FFWe4uIUVJr81Uf znIBe%ZN^s&hnw_N{b4F$jPkGh6}J{&Zn9&lBfa)y;I$0HH{GD+w!9M+{*B-a8p(`R zshA)3hWSZ7rkY7N*mgTAk}Vm@>tYMj*u6(E7p^qy7O#Yo z`a>ogf6y%HHIe^h6a)}J009ILKmY**5I_I{1Q3{G0p|a6Jh*fX0R#|0009IL zKmY**5I_Kda{=c6+zJRFfB*srAbYoG{{Mge$C(}?fB*srAbxHULmP=pinsF8lFTMgnn&Bdq)@AtmrK0Xs$Qy+QklPM#o|MftlRu( z%S)1d>eO4*;YFtboHnGcMWf2xoVXQ);qG<#sL62IUXNLIc4jj+7TB4cvli{LhrFiR zzZZ!qiXy(A4%+n3=mxXsSp6Ur*GKu^dHJpRryEU-b*b4IE!d^5dtf^aqhU6g`!9mp zuZChuJ}hfn- zJpSm0KeB(6&OFi{6D{{Noqww3=F_<+nT7P_d;I(V!TA5}hm|3sfdB*`009U<00Izz z00bZa0SG|gJp?9058@-A8w~IN|0plMN2{m`0SG_<0uX=z1Rwwb2tWV=5V#2f-%3$I zRr?Xw?D${d=h05b{|>-l>p8VM^L4vBlc6(OW@aP1UFlYio9*vbT5E?#OT~6WvwE%V z7mJ7ajqTM`eTgJJHK1pIa<%o@AN8#=4UBauk@tn$$#F2jzW$Q zfB*y_009U<00Izz00bZafom%8kr@1{gv8T}$Nitq2=crY4@!sQ|F`ll*ED~m1px>^ z00Izz00bZa0SG_<0uZ>i0*VwCG_hmbZ9+ZbwM>@;znUo}#ISHb_!z)5t>&l0=l}nf n-(6b_BnANpKmY;|fB*y_009U<00I!WkphvBC`wW&6p#M{J Date: Wed, 17 Feb 2021 11:46:02 -0600 Subject: [PATCH 2/6] Done. --- app/controllers/application_controller.rb | 6 ++++++ app/controllers/tweets_controller.rb | 4 ++++ app/controllers/users_controller.rb | 8 ++++++++ app/views/layout.erb | 4 ++-- app/views/root.erb | 0 app/views/tweets/index.erb | 1 + app/views/users/signup.erb | 11 +++++++++++ db/test.sqlite | Bin 36864 -> 36864 bytes 8 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 app/views/root.erb create mode 100644 app/views/tweets/index.erb create mode 100644 app/views/users/signup.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9ac085ac3c3..f375ac7ed65 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,4 +7,10 @@ class ApplicationController < Sinatra::Base set :views, 'app/views' end + get "/" do + erb :root + end + + + end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c9f05301dc5..172b21b51f0 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,8 @@ class TweetsController < ApplicationController + get "/tweets" do + binding.pry + erb :"tweets/index" + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a7d1cab4e29..3ecac55160f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,12 @@ class UsersController < ApplicationController + get "/signup" do + erb :"users/signup" + end + + post "/signup" do + @user = User.create(params) + redirect "/tweets" + end end diff --git a/app/views/layout.erb b/app/views/layout.erb index 88e49546076..8028517cfdc 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -7,8 +7,8 @@

Welcome to Fwitter!

-

This is located in layout.erb!

-

Don't forget to yield your views!

+ + <%= yield %>
diff --git a/app/views/root.erb b/app/views/root.erb new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/views/tweets/index.erb b/app/views/tweets/index.erb new file mode 100644 index 00000000000..aea391fc0a8 --- /dev/null +++ b/app/views/tweets/index.erb @@ -0,0 +1 @@ +

Tweets:

\ No newline at end of file diff --git a/app/views/users/signup.erb b/app/views/users/signup.erb new file mode 100644 index 00000000000..90c8f4aa4da --- /dev/null +++ b/app/views/users/signup.erb @@ -0,0 +1,11 @@ +

Signup

+ +
+ +
+ +
+ +
+ +
\ No newline at end of file diff --git a/db/test.sqlite b/db/test.sqlite index 22538908edfda1bd7487d355458eb965f49a70b6..2a81e262c539769f626bdcaa6c2f86c4dbe70f4c 100644 GIT binary patch delta 180 zcmZozz|^pSX@V3Jo8LqkCm^{oVWmIw8h-c9f&$k3lilNW>NC>y3Oy_JEcC;S4U$p< zvt4t1OiHuD4P8TgQ%g#t0*VSuj17Ym3w+!|GkvRK8QB<=bsJfWvonK$OkN1Hq$DS` z*dZ}LM=v=)SH&n%#n4D4%C(>$G;-IxH7OnFQ+Qfqskzl ZA}ob3&@)os&m|=^IMb=rr*ct(0RWpQIIaKy delta 180 zcmZozz|^pSX@V3JbM{0TCm^{oVWmIwQhxW%f&$k3lilNW>W!lUa|5f=v$MQ20xBv! ze6mvv^GY+KoZKr+!=rrllRXmC!_z}UE7K!P3%!ag3PSxO8QB~#g%+JwF&d*gbN>nj4QgL?;h=@!oEh!02c1_7PE$~UJ3^LYBD-Co^x706o4J-|> b$WIOP=JO9La?LF_(XXm1&u|T2lwbe=d5}7q From 96af9f3fcd02420db43f8c1288379f5141e5f19f Mon Sep 17 00:00:00 2001 From: andy-aguilar Date: Wed, 17 Feb 2021 12:04:35 -0600 Subject: [PATCH 3/6] Done. --- app/controllers/users_controller.rb | 8 ++++++-- app/models/user.rb | 2 ++ db/test.sqlite | Bin 36864 -> 36864 bytes 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3ecac55160f..f2e07c2639c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,8 +5,12 @@ class UsersController < ApplicationController end post "/signup" do - @user = User.create(params) - redirect "/tweets" + @user = User.new(params) + if @user.save + redirect "/tweets" + else + redirect "/signup" + end end end diff --git a/app/models/user.rb b/app/models/user.rb index 7947c75b6ee..27b3ccb0e03 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,8 @@ class User < ActiveRecord::Base has_secure_password has_many :tweets + validates :username, presence: true + validates :email, presence: true def slug self.username.downcase.gsub(" ", "-") diff --git a/db/test.sqlite b/db/test.sqlite index 2a81e262c539769f626bdcaa6c2f86c4dbe70f4c..313aa67891820191499b9afbb64cce89e04be98f 100644 GIT binary patch delta 120 zcmZozz|^pSX@WH4w23m#jMFwItn_DI$nU;cP{5jhvU_}LNJ&XfYO$e_adCDgnCXz1 zpQD$YpQ~b&sA6cO66|Z~5nxbh;#H7t5>%WL VW*J!->>i&Q62!>Hz^mKHTAZC(Qj(Ke z?2wqBqnDhYt74R>VrZli*a;Z*1WlRp?`ym}P2`<6n_pTp3uPms1t#QDqQN Y5tb6@8L98*k`fx6=~U`dxhTN^0P=4p^#A|> From 8544b7534d95d6b36f758e5d9d96c987cf7345e6 Mon Sep 17 00:00:00 2001 From: andy-aguilar Date: Wed, 17 Feb 2021 12:14:32 -0600 Subject: [PATCH 4/6] Done. --- app/controllers/application_controller.rb | 2 ++ app/controllers/users_controller.rb | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f375ac7ed65..aca97a053e9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,7 +2,9 @@ class ApplicationController < Sinatra::Base + configure do + enable :sessions set :public_folder, 'public' set :views, 'app/views' end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f2e07c2639c..cf6cda7ce84 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,12 +1,17 @@ class UsersController < ApplicationController get "/signup" do - erb :"users/signup" + if !session[:user_id] + erb :"users/signup" + else + erb :"users/tweets" + end end post "/signup" do @user = User.new(params) if @user.save + session[:user_id] = @user.id redirect "/tweets" else redirect "/signup" From 3e107826a7c4a0ac4115078bbc7150ce19abfd2e Mon Sep 17 00:00:00 2001 From: andy-aguilar Date: Wed, 17 Feb 2021 12:18:10 -0600 Subject: [PATCH 5/6] Done. --- app/controllers/users_controller.rb | 6 +++--- db/test.sqlite | Bin 36864 -> 36864 bytes 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index cf6cda7ce84..9f7bdb0b270 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,10 +1,10 @@ class UsersController < ApplicationController get "/signup" do - if !session[:user_id] - erb :"users/signup" + if session[:user_id] + redirect "/tweets" else - erb :"users/tweets" + erb :"users/signup" end end diff --git a/db/test.sqlite b/db/test.sqlite index 313aa67891820191499b9afbb64cce89e04be98f..84669431bc62b3db7c6c6daa4b29662676d4c27e 100644 GIT binary patch delta 78 zcmZozz|^pSX@V3Jd%{E+Cm^{oAv@kQC)iLwqs-SZBCOOrGpNkXJk&72u++lBFeuO1 gDYwEa!Ys_$)7vGWz}Pa%sNBEUvedx8a#4Z-0KD!Pi~s-t delta 78 zcmZozz|^pSX@V3J+q8)?PC#;FLUz1qu&<>@fI+2+S3$Z-P;p9-Q&5C=s9s2Am{(eO gpohOfR6s#Qg`sPiWn^WPU!_-hZm6MQ@S+3*0Oy<;umAu6 From bc2cb73b287e894156098aab90c08f54c3dc4a43 Mon Sep 17 00:00:00 2001 From: andy-aguilar Date: Wed, 17 Feb 2021 14:11:09 -0600 Subject: [PATCH 6/6] Done. --- app/controllers/tweets_controller.rb | 69 ++++++++++++++++++++++++++- app/controllers/users_controller.rb | 32 +++++++++++++ app/helpers/helpers.rb | 9 ++++ app/models/tweet.rb | 1 + app/views/layout.erb | 7 ++- app/views/tweets/edit.erb | 13 +++++ app/views/tweets/index.erb | 11 ++++- app/views/tweets/new.erb | 4 +- app/views/tweets/show.erb | 12 +++++ app/views/users/login.erb | 8 ++++ app/views/users/show.erb | 7 +++ db/test.sqlite | Bin 36864 -> 36864 bytes 12 files changed, 166 insertions(+), 7 deletions(-) create mode 100644 app/helpers/helpers.rb create mode 100644 app/views/tweets/edit.erb create mode 100644 app/views/tweets/show.erb create mode 100644 app/views/users/login.erb create mode 100644 app/views/users/show.erb diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 172b21b51f0..b58a4fd9f84 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,8 +1,73 @@ class TweetsController < ApplicationController get "/tweets" do - binding.pry - erb :"tweets/index" + if session[:user_id] + @tweets = Tweet.all + erb :"tweets/index" + else + redirect "/login" + end + end + + get "/tweets/new" do + if session[:user_id] + erb :"tweets/new" + else + redirect "/login" + end + end + + post "/tweets" do + @tweet = Tweet.new(params) + if @tweet.save + redirect "/tweets" + else + redirect "/tweets/new" + end + end + + get "/tweets/:id" do + if session[:user_id] + @tweet = Tweet.find(params[:id]) + erb :"tweets/show" + else + redirect "/login" + end + end + + delete "/tweets/:id" do + if !session[:user_id] + redirect "/login" + end + @tweet = Tweet.find(params[:id]) + if session[:user_id] == @tweet.id + @tweet.destroy + redirect "/tweets" + else + redirect "/tweets/#{@tweet.id}" + end + end + + get "/tweets/:id/edit" do + if !session[:user_id] + redirect "/login" + end + @tweet = Tweet.find(params[:id]) + + if session[:user_id] == @tweet.user.id + erb :"tweets/edit" + else + redirect "/tweets/#{@tweet.id}" + end + end + + patch "/tweets/:id" do + @tweet = Tweet.find(params[:id]) + if @tweet.update(params[:tweet]) + redirect "/tweets/#{@tweet.id}" + else + redirect "/tweets/#{@tweet.id}/edit" + end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9f7bdb0b270..28198b336ee 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,6 +8,24 @@ class UsersController < ApplicationController end end + get "/login" do + if session[:user_id] + redirect "/tweets" + else + erb :"users/login" + end + end + + post "/login" do + @user = User.find_by(username: params[:username]) + if @user.authenticate(params[:password]) + session[:user_id] = @user.id + redirect "/tweets" + else + redirect "/login" + end + end + post "/signup" do @user = User.new(params) if @user.save @@ -18,4 +36,18 @@ class UsersController < ApplicationController end end + get "/logout" do + if session[:user_id] + session.clear + redirect "/login" + else + redirect "/login" + end + end + + get "/users/:slug" do + @user = User.find_by_slug(params[:slug]) + erb :"users/show" + end + end diff --git a/app/helpers/helpers.rb b/app/helpers/helpers.rb new file mode 100644 index 00000000000..2a71c1a525a --- /dev/null +++ b/app/helpers/helpers.rb @@ -0,0 +1,9 @@ +class Helpers + def self.current_user(session) + User.find(session[:user_id]) + end + + def self.is_logged_in?(session) + !!session[:user_id] + end +end \ No newline at end of file diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 798b1babdc3..f80e65ec7a6 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,3 +1,4 @@ class Tweet < ActiveRecord::Base belongs_to :user + validates :content, length: { minimum: 1 } end diff --git a/app/views/layout.erb b/app/views/layout.erb index 8028517cfdc..b4bd0bcaaa6 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -6,8 +6,11 @@
-

Welcome to Fwitter!

- + <% if session[:user_id]%> +

Welcome, <%= User.find(session[:user_id]) %>

+ <% else%> +

Welcome to Fwitter!

+ <% end %> <%= yield %>
diff --git a/app/views/tweets/edit.erb b/app/views/tweets/edit.erb new file mode 100644 index 00000000000..893a096a868 --- /dev/null +++ b/app/views/tweets/edit.erb @@ -0,0 +1,13 @@ +

Edit Tweet:

+ +
method="POST"> + + Content: + >
+ Update Your Tweet: + > + + + + +
diff --git a/app/views/tweets/index.erb b/app/views/tweets/index.erb index aea391fc0a8..93373e9ebab 100644 --- a/app/views/tweets/index.erb +++ b/app/views/tweets/index.erb @@ -1 +1,10 @@ -

Tweets:

\ No newline at end of file +

Tweets:

+ +
    + <% @tweets.each do |tweet|%> +
  • +
    <%=tweet.user.username%>
    +

    <%= tweet.content %>

    +
  • + <% end%> +
\ No newline at end of file diff --git a/app/views/tweets/new.erb b/app/views/tweets/new.erb index 875ade026b1..f5bfc440980 100644 --- a/app/views/tweets/new.erb +++ b/app/views/tweets/new.erb @@ -4,10 +4,10 @@ Content:
Create Your Tweet: + > - + - diff --git a/app/views/tweets/show.erb b/app/views/tweets/show.erb new file mode 100644 index 00000000000..a76a8cdb726 --- /dev/null +++ b/app/views/tweets/show.erb @@ -0,0 +1,12 @@ +

<%= @tweet.user.username%>

+ +

Content:

+

<%= @tweet.content %>

+ +
> + + +
+
> + +
diff --git a/app/views/users/login.erb b/app/views/users/login.erb new file mode 100644 index 00000000000..5d7b2edc90e --- /dev/null +++ b/app/views/users/login.erb @@ -0,0 +1,8 @@ +

Login:

+
+ +
+ +
+ +
\ No newline at end of file diff --git a/app/views/users/show.erb b/app/views/users/show.erb new file mode 100644 index 00000000000..5f2b1d98496 --- /dev/null +++ b/app/views/users/show.erb @@ -0,0 +1,7 @@ +

<%=@user.username%>

+ +
    + <% @user.tweets.each do |tweet|%> +
  • <%= tweet.content %>
  • + <% end%> +
\ No newline at end of file diff --git a/db/test.sqlite b/db/test.sqlite index 84669431bc62b3db7c6c6daa4b29662676d4c27e..8da85870552cf506786bc9fab5891f0516ddb0c4 100644 GIT binary patch delta 181 zcmZozz|^pSX@V4!O!h<>Cm^{oVWmIwQhxW%f&$k3lilOB>obe|%!_g&T{86yoh-8~ zBXeCs3UW##{Zb7aJqwcbiX&Xp%H2$h^^*!c+`L^Qd|gXQBN^EkWVIVvOHzwV6by}w zK{SwZNX*aCOU}<#F-lZ1G*WT(EKM#l$kKE5@G`Tk^2l*a56U$4&oMBs%nPW<4fKwv cD30>+%%kB^$I;J^(^$mj17`f z0<&Fnd`wES!VO(Rd{awGqXLQwOpFbK6AOIYLo