Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Done #1142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Done #1142

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ gem 'shotgun'
gem 'pry'
gem 'bcrypt'
gem "tux"
gem 'rack-flash3'

group :test do
gem 'rspec'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ GEM
method_source (~> 0.9.0)
public_suffix (4.0.1)
rack (2.0.7)
rack-flash3 (1.0.5)
rack
rack-protection (2.0.7)
rack
rack-test (1.1.0)
Expand Down Expand Up @@ -109,6 +111,7 @@ DEPENDENCIES
capybara
database_cleaner
pry
rack-flash3
rack-test
rake
require_all
Expand Down
19 changes: 19 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
require './config/environment'
require 'rack-flash'

class ApplicationController < Sinatra::Base

helpers do
def logged_in?
!!session[:user_id]
end

def current_user
User.find(session[:user_id])
end
end


configure do
set :public_folder, 'public'
set :views, 'app/views'
enable :sessions
use Rack::Flash
end


get '/' do
erb :index
end

end
83 changes: 83 additions & 0 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,87 @@
class TweetsController < ApplicationController

get '/tweets' do
if !logged_in?
flash[:message] = "You must log in first."
redirect to '/login'
else
erb :'/tweets/tweets'
end
end


get '/tweets/new' do
if !logged_in?
flash[:message] = "You must log in first."
redirect to '/login'
else
erb :'/tweets/new'
end
end


post '/tweets' do
if !params[:content].empty?
@tweet = Tweet.create(params)
@tweet.user = current_user
@tweet.save
flash[:message] = "Tweet posted!"
redirect to "/tweets/#{@tweet.id}"
else
flash[:message] = "Your tweet cannot be empty."
redirect to '/tweets/new'
end
end


get '/tweets/:id' do
if !logged_in?
flash[:message] = "You must log in first."
redirect to '/login'
else
@tweet = Tweet.find(params[:id])
erb :'tweets/show_tweet'
end
end


get '/tweets/:id/edit' do
@tweet = Tweet.find_by_id(params[:id])
if logged_in? && @tweet.user == current_user
erb :'/tweets/edit_tweet'
elsif logged_in? && @tweet.user != current_user
flash[:message] = "You cannot edit other people's tweets."
redirect to "/tweets/#{@tweet.id}"
else
flash[:message] = "You must log in first."
redirect to '/login'
end
end


patch '/tweets/:id' do
@tweet = Tweet.find(params[:id])
if !params[:content].blank?
@tweet.update("content" => params[:content])
flash[:message] = "Tweet successfully updated."
redirect to "/tweets/#{@tweet.id}"
else
flash[:message] = "Please enter content to update tweet."
redirect to "/tweets/#{@tweet.id}/edit"
end
end


delete '/tweets/:id' do
@tweet = Tweet.find(params[:id])
if @tweet.user_id == current_user.id
@tweet.destroy
flash[:message] = "Tweet successfully deleted."
else
flash[:message] = "You cannot delete other people's tweets."
end
redirect to '/tweets'
end


end
63 changes: 62 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,65 @@
class UsersController < ApplicationController


get '/signup' do
if !logged_in?
erb :'/users/signup'
else
redirect to '/tweets'
end
end


post '/signup' do
if params[:username].empty? || params[:email].empty? || params[:password].empty?
flash[:message] = "Missing Username, Email or Password."
redirect to '/signup'
else
user = User.create(params)
flash[:message] = "User created successfully. Logging you in.."
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
flash[:message] = "Logged in successfully."
redirect to '/tweets'
else
flash[:message] = "Username or Password is incorrect."
redirect to '/login'
end
end


get '/logout' do
if !logged_in?
redirect to '/'
else
flash[:message] = "Logged out successfully."
session.clear
redirect to '/login'
end
end


get '/users/:slug' do
@user = User.find_by_slug(params[:slug])
erb :'/users/show'
end


end
15 changes: 15 additions & 0 deletions app/models/concerns/slugifiable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Slugifiable

module InstanceMethods
def slug
self.username.gsub(" ", '-').downcase
end
end

module ClassMethods
def find_by_slug(slug)
self.all.find {|instance| instance.slug == slug}
end
end

end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require_relative './concerns/slugifiable.rb'

class User < ActiveRecord::Base
extend Slugifiable::ClassMethods
include Slugifiable::InstanceMethods

has_secure_password
has_many :tweets
end
2 changes: 2 additions & 0 deletions app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<a href="/login">Log In</a>
<a href="/signup">Sign Up</a>
1 change: 1 addition & 0 deletions app/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<h1>Welcome to Fwitter!</h1>
<h2>This is located in layout.erb!</h2>
<h2>Don't forget to <strong>yield</strong> your views!</h2>
<%= yield %>
</div>

</body>
Expand Down
12 changes: 12 additions & 0 deletions app/views/tweets/edit_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<h1>Welcome, <%= current_user.username %></h1>

<h3>Edit Tweet</h3>

<form action='/tweets/<%= @tweet.id %>' method="post">
<input type="hidden" id="hidden" name="_method" value="patch">

<label for="content">Tweet:</label>
<input type="text" id="content" name="content" value="<%= @tweet.content %>">

<button type="submit" value="submit">Edit Tweet</button>
</form>
22 changes: 8 additions & 14 deletions app/views/tweets/new.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
<% if Helpers.is_logged_in?(session) %>
<%= @error %>
<form action='/tweets' method="post">
Content:
<input type="text" name="content"><br>
Create Your Tweet:
<h1>Welcome, <%= current_user.username %></h1>

<input type="submit" name="submit" id="submit">
<h3>Post a New Tweet</h3>

<input type="submit" name="submit">

</form>

<% else %>
<h1> You Must Be Logged in to Make Tweets </h1>
<% end %>
<form action='/tweets' method="post">
<label for="content">Tweet:</label>
<input type="text" id="content" name="content">

<button type="submit" value="submit">Post Tweet</button>
</form>
14 changes: 14 additions & 0 deletions app/views/tweets/show_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h1>Welcome, <%= current_user.username %></h1>

<h3>View Tweet</h3>
<h4><%= @tweet.content %></h4><br>
<p>--<%= @tweet.user.username %></p>


<form action="/tweets/<%= @tweet.id %>" method="post">
<input type="hidden" id="hidden" name="_method" value="delete">
<button type="submit" value="submit">Delete Tweet</button>
</form>

<a href="/tweets/<%= @tweet.id %>/edit">Edit Tweet</a>

8 changes: 8 additions & 0 deletions app/views/tweets/tweets.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Welcome, <%= current_user.username %></h1>

<h3>Latest Tweets</h3>
<ul>
<% Tweet.all.each do |tweet| %>
<li><%= tweet.content %></li>
<% end %>
</ul>
9 changes: 9 additions & 0 deletions app/views/users/login.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<form action="/login" method="post">
<label for="username">Usermame</label>
<input type="text" id="username" name="username">

<label for="password">Password</label>
<input type="text" id="password" name="password">

<button type="submit" value="submit">Log In</button>
</form>
2 changes: 1 addition & 1 deletion app/views/users/logout.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<form action="/logout" method="post">
Logout?
<input type="submit" name="logout" >
<button type="submit" value="submit">Log Out</button>
</form>
10 changes: 10 additions & 0 deletions app/views/users/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Welcome, <%= current_user.username %></h1>

<h3><%= @user.username %>'s Tweets</h3>
<ul>
<% @user.tweets.each do |tweet| %>
<li><%= tweet.content %></li>
<% end %>
</ul>

<a href="/logout">Log Out</a>
12 changes: 12 additions & 0 deletions app/views/users/signup.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<form action="/signup" method="post">
<label for="username">Usermame</label>
<input type="text" id="username" name="username">

<label for ="email">Email</label>
<input type="text" id="email" name="email">

<label for="password">Password</label>
<input type="text" id="password" name="password">

<button type="submit" value="submit">Sign Up</button>
</form>
Binary file added db/development.sqlite
Binary file not shown.
9 changes: 9 additions & 0 deletions db/migrate/20200108214445_create_users.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions db/migrate/20200108214451_create_tweets.rb
Original file line number Diff line number Diff line change
@@ -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
Loading