From 97c2b0b568959d64bbdfc4dc046f914b9376ccb9 Mon Sep 17 00:00:00 2001 From: Ian Gutwinski Date: Thu, 2 Apr 2020 22:50:35 -0400 Subject: [PATCH] Done. --- app/controllers/application_controller.rb | 18 +++++ app/controllers/tweets_controller.rb | 85 +++++++++++++++++++++ app/controllers/users_controller.rb | 51 +++++++++++++ app/models/user.rb | 9 +++ app/views/index.erb | 2 + app/views/layout.erb | 18 +++-- app/views/tweets/edit_tweet.erb | 11 +++ app/views/tweets/new.erb | 14 +--- app/views/tweets/show_tweet.erb | 7 ++ app/views/tweets/tweets.erb | 10 +++ app/views/users/create_user.erb | 16 ++++ app/views/users/login.erb | 13 ++++ app/views/users/show.erb | 3 + db/development.sqlite | Bin 0 -> 32768 bytes db/migrate/20200402202503_create_users.rb | 9 +++ db/migrate/20200402202917_create_tweets.rb | 8 ++ db/schema.rb | 26 +++++++ db/test.sqlite | Bin 0 -> 32768 bytes 18 files changed, 282 insertions(+), 18 deletions(-) create mode 100644 app/views/index.erb create mode 100644 app/views/tweets/edit_tweet.erb create mode 100644 app/views/tweets/show_tweet.erb create mode 100644 app/views/tweets/tweets.erb create mode 100644 app/views/users/create_user.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/20200402202503_create_users.rb create mode 100644 db/migrate/20200402202917_create_tweets.rb create mode 100644 db/schema.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9ac085ac3c3..7e7c599090b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,24 @@ class ApplicationController < Sinatra::Base configure do set :public_folder, 'public' set :views, 'app/views' + enable :sessions + set :session_secret, "fwitter_secret" end + get '/' do + @session = session + erb :index + end + + helpers do + def logged_in? + !!session[:user_id] + end + + def current_user + User.find(session[:user_id]) + end + + end + end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c9f05301dc5..14a065153a5 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,89 @@ class TweetsController < ApplicationController + get '/tweets' do + if logged_in? + @tweets = Tweet.all + erb :'/tweets/tweets' + else + redirect to '/login' + end + end + + get '/tweets/new' do + if logged_in? + erb :'/tweets/new' + else + redirect to '/login' + end + end + + post '/tweets' do + if logged_in? + if params[:content] == "" + redirect to "tweets/new" + else + @tweet = Tweet.new(params) + @tweet.user = current_user + if @tweet.save + redirect to "/tweets/#{@tweet.id}" + else + redirect to '/tweets/new' + end + end + else + redirect to "/login" + end + end + + get '/tweets/:id' do + if logged_in? + @tweet = Tweet.find_by_id(params[:id]) + erb :'/tweets/show_tweet' + else + redirect to '/login' + end + end + + get '/tweets/:id/edit' do + @tweet = Tweet.find_by_id(params[:id]) + if logged_in? && current_user == @tweet.user + erb :'/tweets/edit_tweet' + else + redirect to '/login' + end + end + + patch '/tweets/:id' do + if logged_in? + if params[:content] == "" + redirect to "/tweets/#{params[:id]}/edit" + else + @tweet = Tweet.find_by_id(params[:id]) + if @tweet && @tweet.user == current_user + if @tweet.update(content: params[:content]) + redirect to "/tweets/#{@tweet.id}" + else + redirect to "/tweets/#{@tweet.id}/edit" + end + else + redirect to '/tweets' + end + end + else + redirect to '/login' + end + end + + delete '/tweets/:id/delete' do + if logged_in? + @tweet = Tweet.find_by_id(params[:id]) + if @tweet && @tweet.user == current_user + @tweet.delete + end + redirect to '/tweets' + else + redirect to '/login' + end + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a7d1cab4e29..b12b3bd7be2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,55 @@ class UsersController < ApplicationController + get '/users/:slug' do + @user = User.find_by_slug(params[:slug]) + erb :'users/show' + end + + get '/signup' do + if !logged_in? + erb :'/users/create_user' + else + redirect to '/tweets' + end + end + + post '/signup' do + if params[:username] == "" || params[:password] == "" || params[:email] == "" + redirect to '/signup' + else + @user = User.new(params) + @user.save + session[:user_id] = @user.id + redirect to '/tweets' + end + end + + get '/login' do + if !logged_in? + erb :'/users/login' + else + redirect to '/tweets' + end + end + + post '/login' do + @user = User.find_by(:username => params[:username]) + + if @user && @user.authenticate(params[:password]) + session[:user_id] = @user.id + redirect to '/tweets' + else + redirect to '/signup' + end + end + + get '/logout' do + if logged_in? + session.clear + redirect to '/login' + else + redirect to '/' + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 7d5543b8618..bd6acaca75d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,13 @@ class User < ActiveRecord::Base has_secure_password has_many :tweets + + def self.find_by_slug(slug) + User.all.find {|user| user.slug == slug} + end + + def slug + username.downcase.gsub(" ","-") + end + end diff --git a/app/views/index.erb b/app/views/index.erb new file mode 100644 index 00000000000..ab17c84f7a5 --- /dev/null +++ b/app/views/index.erb @@ -0,0 +1,2 @@ +

Login

+

Sign Up

diff --git a/app/views/layout.erb b/app/views/layout.erb index 88e49546076..e311d0c65e5 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -1,15 +1,21 @@ - Fwitter + Twitter - +

Welcome to Fwitter!

-

Welcome to Fwitter!

-

This is located in layout.erb!

-

Don't forget to yield your views!

-
+ + <%= yield %> + + diff --git a/app/views/tweets/edit_tweet.erb b/app/views/tweets/edit_tweet.erb new file mode 100644 index 00000000000..c5446751d06 --- /dev/null +++ b/app/views/tweets/edit_tweet.erb @@ -0,0 +1,11 @@ +
+ + +Content: +

+Edit Your Tweet:
+ + + + +
diff --git a/app/views/tweets/new.erb b/app/views/tweets/new.erb index 875ade026b1..e0985513501 100644 --- a/app/views/tweets/new.erb +++ b/app/views/tweets/new.erb @@ -1,16 +1,6 @@ -<% 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_tweet.erb b/app/views/tweets/show_tweet.erb new file mode 100644 index 00000000000..05a7752ffee --- /dev/null +++ b/app/views/tweets/show_tweet.erb @@ -0,0 +1,7 @@ +

<%= @tweet.content %>

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

Welcome, <%= current_user.username %> + +

Tweets

+ +<% @tweets.each do |x| %> +
+ <%= x.content %> +
+<% end %> + diff --git a/app/views/users/create_user.erb b/app/views/users/create_user.erb new file mode 100644 index 00000000000..14fd3afa17c --- /dev/null +++ b/app/views/users/create_user.erb @@ -0,0 +1,16 @@ +

Sign Up

+ +
+ + +

+ + +

+ + +

+ + + +
\ No newline at end of file diff --git a/app/views/users/login.erb b/app/views/users/login.erb new file mode 100644 index 00000000000..797ead6848e --- /dev/null +++ b/app/views/users/login.erb @@ -0,0 +1,13 @@ +

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..0ce4e3930a1 --- /dev/null +++ b/app/views/users/show.erb @@ -0,0 +1,3 @@ +<% @user.tweets.each do |x| %> +

<%= x.content %>

+<%end%> \ No newline at end of file diff --git a/db/development.sqlite b/db/development.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..d87ff2290f8bb6f21568a74f08a15e517b9eab05 GIT binary patch literal 32768 zcmeI*OON8n9RP6C4|-nR%+6$|HQHS*GqX~rHM_=u!LYe34d!LQ7|g@Wp;O>882kWi z116`QQx5qCxkSn>m*kRLq(5=9#+*UTkSB1Ot6O3{zq>7AM0RW8Xy{}MKK+5T76 zuZ-1h8`G33x{Je_JvWgXj#GE3K!Exp9HuDhDH(2%;WF)ziOq1C0+$olyg#Mr4}Y~E z`X{w_=Vw&t2ccgdd~oph{U7Zgky%I}00JNY0w4eaAOHd&00JOzMc_2Jw|DsPVc>M= zqP~IMCC09EKE9D{@EMs8%bDW}AAWqE`8fR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x{dvP^SRZnm08;`1F+06Er_kag{0M&eS7 z+M*EElRd%6#)^K&WQTOm%%S+PQJkrv5SyXDXsaIzG=iA>cc;5GyIBs^U zB|X#0wObO>r-nC4*oM2BPP<%lluJ&cCA+q+St}=5#{H<}J>U9n!3{%J2I|wjuGy+#<4?q340`7CgRM2}Y^h)}r-kj)_~m zkdv2qQHs@Kg4IjXUA@l4k;Rnga@r{$7o5>L+c|&!-%Bd=kI+l_Lk0vu00ck)1V8`; zKmY_l00ck)1VG>&7r3>x{V;I;8yR~y1KWpzv!9xHusQ$V3H^c!{U!9f&~M2EBoF`r z5C8!X009sH0T2KI5C8!X0D=FKz=x#V`+GaoQGk5ge@N~)8j{;@$c;A-Lc2TEqd+Na zkUM+AeXQPpu(Lx&0^|;g?>k|4w8?YaM~@!ed$6_1&yI8pg>@&acq9$+qxb#|15X6s literal 0 HcmV?d00001 diff --git a/db/migrate/20200402202503_create_users.rb b/db/migrate/20200402202503_create_users.rb new file mode 100644 index 00000000000..e9a61010d1f --- /dev/null +++ b/db/migrate/20200402202503_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[6.0] + def change + create_table :users do |t| + t.string :username + t.string :email + t.string :password_digest + end + end +end diff --git a/db/migrate/20200402202917_create_tweets.rb b/db/migrate/20200402202917_create_tweets.rb new file mode 100644 index 00000000000..17c92736bca --- /dev/null +++ b/db/migrate/20200402202917_create_tweets.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..5240e6b7204 --- /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: 2020_04_02_202917) 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..ba4a7e671df53a80c31a2ba619cda5478e30740e 100644 GIT binary patch literal 32768 zcmeI&T~E_c7{KwigB6XVXiOTS#%4FtKxBZ^7;l_rF@*6Vjtjy?bKH)jaWBwzfbnX4 z5C*7OZ6+e>ByH3y&k+Y;tX}YdG zcN|T7;AxsRq1OH~rViq@zv%sg%lE2}hj%~ctY4aW~m?l{qJ0>!`ciQQkQaL2XwA zD%1^S;0<)-Ze+9e?5w_>q!I2n)vU=2<$hPTYx3NwnM&;3#hJEf$vM^1?cEzUZQIt5 zr;|S8Cx*!;n`)n=68=|Rac}YC1`Crd;Wq~Zdm;=EJ3-y6`wbcXMR0r7jA<8(`teGV zpHLo(Np;xtS~3!K5s6{3v2^ysZi$O|FF%}RaW)o%!=(n{Ks39l