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 00000000000..d87ff2290f8 Binary files /dev/null and b/db/development.sqlite differ 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 e69de29bb2d..ba4a7e671df 100644 Binary files a/db/test.sqlite and b/db/test.sqlite differ