From bae9bdddd65a34e657ccec1758d121e47ce123be Mon Sep 17 00:00:00 2001 From: Richard Scott Salchunas Date: Sun, 23 Feb 2020 16:56:05 +0000 Subject: [PATCH] Done. --- app/controllers/application_controller.rb | 72 ++++++++++++++++ app/controllers/tweets_controller.rb | 79 ++++++++++++++++++ app/controllers/users_controller.rb | 50 +++++++++++ app/models/user.rb | 12 +++ app/views/index.erb | 27 ++++++ app/views/layout.erb | 8 +- app/views/tweets/create_tweet.erb | 6 ++ app/views/tweets/edit_tweet.erb | 5 ++ app/views/tweets/new.erb | 16 ---- app/views/tweets/show_tweet.erb | 32 +++++++ app/views/tweets/tweets.erb | 29 +++++++ app/views/users/create_user.erb | 9 ++ app/views/users/login.erb | 7 ++ app/views/users/logout.erb | 4 - app/views/users/show.erb | 6 ++ db/development.sqlite | Bin 0 -> 8192 bytes .../20200217174917_create_users_table.rb | 9 ++ .../20200217174937_create_tweets_table.rb | 8 ++ db/schema.rb | 26 ++++++ db/test.sqlite | Bin 0 -> 8192 bytes .../application_controller_spec.rb | 2 +- .../{user_spec.rb => install bundler.rb} | 0 22 files changed, 379 insertions(+), 28 deletions(-) create mode 100644 app/views/index.erb create mode 100644 app/views/tweets/create_tweet.erb create mode 100644 app/views/tweets/edit_tweet.erb delete mode 100644 app/views/tweets/new.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 delete mode 100644 app/views/users/logout.erb create mode 100644 app/views/users/show.erb create mode 100644 db/development.sqlite create mode 100644 db/migrate/20200217174917_create_users_table.rb create mode 100644 db/migrate/20200217174937_create_tweets_table.rb create mode 100644 db/schema.rb rename spec/models/{user_spec.rb => install bundler.rb} (100%) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9ac085ac3c3..4c197914cd5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,6 +5,78 @@ class ApplicationController < Sinatra::Base configure do set :public_folder, 'public' set :views, 'app/views' + enable :sessions + set :session_secret, "my_super_secret" + end + + get '/' do + erb :'index' + end + + get '/signup' do + if !logged_in? + erb :'users/create_user' + else + redirect to '/tweets' + end + end + + post '/signup' do + if params[:username] == "" || params[:email] == "" || params[:password] == "" + redirect to '/signup' + else + @user = User.new(:username => params[:username], :email => params[:email], :password => params[:password]) + @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.destroy + redirect to '/login' + else + redirect to '/' + end + end + + + helpers do + + def logged_in? + !!current_user + end + + def current_user + @current_user ||= User.find_by(id: session[:user_id]) if session[:user_id] + end + + # def self.current_user(session) + # User.find(session[:user_id]) + # end + # + # def self.logged_in?(session) + # session.include?(:user_id) + # end end end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c9f05301dc5..bd6e63b56de 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,83 @@ class TweetsController < ApplicationController + get '/tweets' do + if logged_in? + @user = current_user + @tweets = Tweet.all + erb :'tweets/tweets' + else + redirect to '/login' + end + end + + get '/tweets/new' do + if logged_in? + erb :'tweets/create_tweet' + else + redirect to '/login' + end + end + + post '/tweets' do + if params[:content] == "" + redirect to '/tweets/new' + else + @tweet = Tweet.create(content: params[:content]) + @tweet.user_id = current_user.id + @tweet.save + redirect to "/tweets/#{@tweet.id}" + 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 + if logged_in? + @tweet = Tweet.find_by_id(params[:id]) + if @tweet && @tweet.user == current_user + erb :'tweets/edit_tweet' + else + redirect to '/tweets' + end + 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' do + @tweet = Tweet.find_by_id(params[:id]) + if @tweet.user_id == current_user.id + @tweet.delete + end + redirect to "/tweets" + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a7d1cab4e29..100d640c966 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,54 @@ 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[:email] == "" || params[:password] == "" + # redirect to '/signup' + # else + # @user = User.new(:username => params[:username], :email => params[:email], :password => params[:password]) + # @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.destroy + # redirect to '/login' + # else + # redirect to '/' + # end + # end end diff --git a/app/models/user.rb b/app/models/user.rb index 7d5543b8618..0a2d7bacc00 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,16 @@ class User < ActiveRecord::Base + validates :username, presence: true + validates :email, presence: true + validates :password, presence: true + has_secure_password has_many :tweets + + def slug + username.downcase.gsub(" ", "-") + end + + def self.find_by_slug(slug) + User.all.find{|user| user.slug == slug} + end end diff --git a/app/views/index.erb b/app/views/index.erb new file mode 100644 index 00000000000..a9311c9febd --- /dev/null +++ b/app/views/index.erb @@ -0,0 +1,27 @@ + + +

Welcome to Fwitter!

+ + diff --git a/app/views/layout.erb b/app/views/layout.erb index 88e49546076..2c9ef526b6e 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -4,12 +4,6 @@ Fwitter - -
-

Welcome to Fwitter!

-

This is located in layout.erb!

-

Don't forget to yield your views!

-
- + <%= yield %> diff --git a/app/views/tweets/create_tweet.erb b/app/views/tweets/create_tweet.erb new file mode 100644 index 00000000000..320d3bb753b --- /dev/null +++ b/app/views/tweets/create_tweet.erb @@ -0,0 +1,6 @@ +
+ Tweet: + + + +
diff --git a/app/views/tweets/edit_tweet.erb b/app/views/tweets/edit_tweet.erb new file mode 100644 index 00000000000..0e2fdffd761 --- /dev/null +++ b/app/views/tweets/edit_tweet.erb @@ -0,0 +1,5 @@ +
+ + + +
diff --git a/app/views/tweets/new.erb b/app/views/tweets/new.erb deleted file mode 100644 index 875ade026b1..00000000000 --- a/app/views/tweets/new.erb +++ /dev/null @@ -1,16 +0,0 @@ -<% 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..60720a2bccc --- /dev/null +++ b/app/views/tweets/show_tweet.erb @@ -0,0 +1,32 @@ + + +

Tweet Show Page

+

<%= @tweet.content %>

+ Edit Tweet +

+ New tweet +

+ All tweets +

+ +
+ + + + diff --git a/app/views/tweets/tweets.erb b/app/views/tweets/tweets.erb new file mode 100644 index 00000000000..e6d41fda6e3 --- /dev/null +++ b/app/views/tweets/tweets.erb @@ -0,0 +1,29 @@ + + + +

Welcome, <%= current_user.username %> +

Tweets

+ +<% @tweets.each do |tweet| %> +
+ <%= tweet.content %> +
+<% end %> + diff --git a/app/views/users/create_user.erb b/app/views/users/create_user.erb new file mode 100644 index 00000000000..7b9ccc8bdac --- /dev/null +++ b/app/views/users/create_user.erb @@ -0,0 +1,9 @@ + + + + + + + + +
diff --git a/app/views/users/login.erb b/app/views/users/login.erb new file mode 100644 index 00000000000..e3f01804fe7 --- /dev/null +++ b/app/views/users/login.erb @@ -0,0 +1,7 @@ +
+ + + + + +
diff --git a/app/views/users/logout.erb b/app/views/users/logout.erb deleted file mode 100644 index 48083a0d2f8..00000000000 --- a/app/views/users/logout.erb +++ /dev/null @@ -1,4 +0,0 @@ -
- Logout? - -
diff --git a/app/views/users/show.erb b/app/views/users/show.erb new file mode 100644 index 00000000000..c1b35659c41 --- /dev/null +++ b/app/views/users/show.erb @@ -0,0 +1,6 @@ +

Here are your tweets, <%= @user.username %> +<% @user.tweets.each do |tweet| %> +

+ <%= tweet.content %> +
+<% end %> diff --git a/db/development.sqlite b/db/development.sqlite new file mode 100644 index 0000000000000000000000000000000000000000..44e0f6b02f1d31c16e096c3d1acce1e8b79ed0cf GIT binary patch literal 8192 zcmeI0PjAyO6u{%SOE#pT!6m8?!s;%Iw$UbCDH|68s?fA<9iux`93rprx`-y-61N-T z)O`}Z0VgEB%lHgP=2zdWc|^$Mw5FRB%i8whh`CFeRh;`*GmSU~m-6 zZSTRZ-O65_?A4$iR;z0y*X+P*pXW%A2F(*1fT$a?z)mRA_Z3B3T}6%9jPR@t)a;O* zb(!B}BSST1*vRCHPd&B zz*`w#Mfra{pEXDUr36Ka&-SA{7HgG6hS}`_`eC%(A@H}Wih|! z1|8pnAFU2MrrkETK@?^7dcj;b4PtC=ndPkw^YO-0qi7n_XQKNb;cE#VQ3L@&VAcuT v#mftcuaD^dPyMSm>;4P>1OY){NI>NO5QXRn0=JHU$p2dxa^bKbFeLC30xVfL literal 0 HcmV?d00001 diff --git a/db/migrate/20200217174917_create_users_table.rb b/db/migrate/20200217174917_create_users_table.rb new file mode 100644 index 00000000000..9cdca8cd94e --- /dev/null +++ b/db/migrate/20200217174917_create_users_table.rb @@ -0,0 +1,9 @@ +class CreateUsersTable < 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/20200217174937_create_tweets_table.rb b/db/migrate/20200217174937_create_tweets_table.rb new file mode 100644 index 00000000000..faa0de9bc23 --- /dev/null +++ b/db/migrate/20200217174937_create_tweets_table.rb @@ -0,0 +1,8 @@ +class CreateTweetsTable < 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..8d1e5e75c40 --- /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_02_17_174937) 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..73b8ccacf42500ca880596221c6db18d2b244c7a 100644 GIT binary patch literal 8192 zcmeH}UvJYe5WwxYOE#oRgO{k95LTWVZKzF0sU}`1s6x}aZj@G{;vq7P=^~nROWbaV zr|y&RC6M?o<1-+EK;n^0N=wr}2=Un3l3n}kJKy~-C-=qksw+5s(+fN%=srpzjL~CC z5lX*A2yH?iw-ijGK5lqCS^f()?tlH1CO?seJ|Oald{lOnZ}>enVfjyOsA`&?$>3o} zu-9EKE;$$BXxw~Suap}V+9*G%R;V!&8Z>LTjzL{t@D2~?Y5nlHTyN2%N{g18jg!L~ zXdPE-4O%;a-mF&lsL}4hXV()@L5y15I;ycSF;49bac+o6(J7Jg5o%*p=|V;)<+A*G&h;J?3_&1?Mab zFMEMwJ8p-Ee;}B{2GR6l5nmt0{xRSIn80Ay_PAgU6Ks(zmGpkmktmqA{N2QhNiL?- z1xXmNZa?9CJK#)k$7bLr+|PyU@$5r$q8}yhJla@(#C(=g^;boVT;JhWzmvoIq8G32 zMef?xBI(8z*7RHs5ATgWiS`+wwLP~JFyZ$6Fq{=ANicK!5tRXa1EBnOGGsu_g~ik+N4_sDhVVA t$oD^iLoOtNwIVRCfAWKTU#q}nu#&*q5!jlGcwGN;8&V(%NCGQQ;1`EFKQRCR literal 0 HcmV?d00001 diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 2bae011f0cf..1fa9bd98e88 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -419,4 +419,4 @@ end end end -end \ No newline at end of file +end diff --git a/spec/models/user_spec.rb b/spec/models/ install bundler.rb similarity index 100% rename from spec/models/user_spec.rb rename to spec/models/ install bundler.rb