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 #1147

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

Done #1147

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
72 changes: 72 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
79 changes: 79 additions & 0 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- <h1>"Welcome to Fwitter"</h1>
<p> Log In or Sign Up below </p>

<% if logged_in? %>
<a href="/tweets/new"> Create Tweet </a><br>
<a href="/logout"> Log Out </a>
<p> Welcome, <%= current_user.username %> </p>
<% else %>
<a href="/signup"> Sign Up </a><br>
<a href="/login"> Log In </a>
<% end %> -->

<h2>Welcome to Fwitter!</h2>

<nav>
<% if logged_in? %>

<a href="/tweets/new"> Create Tweet </a>
<a href="/logout"> Log Out </a>
<p>Welcome, <%=current_user.username %></p>

<% else %>
<a href="/signup">Sign Up</a><br>
<a href="/login"> Log In </a>
<%end %>

</nav>
8 changes: 1 addition & 7 deletions app/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
<title>Fwitter</title>
</head>
<body>

<div class="container">
<h1>Welcome to Fwitter!</h1>
<h2>This is located in layout.erb!</h2>
<h2>Don't forget to <strong>yield</strong> your views!</h2>
</div>

<%= yield %>
</body>
</html>
6 changes: 6 additions & 0 deletions app/views/tweets/create_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<form action="/tweets" method="post">
Tweet:
<input type="text" name="content">
<input type="submit" name="submit">

</form>
5 changes: 5 additions & 0 deletions app/views/tweets/edit_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<form method="post" action="/tweets/<%[email protected]%>">
<input id="hidden" type="hidden" name="_method" value="PATCH">
<input type="text" name="content" value="<%= @tweet.content %>">
<input type="submit" value="Edit Tweet" id="submit">
</form>
16 changes: 0 additions & 16 deletions app/views/tweets/new.erb

This file was deleted.

32 changes: 32 additions & 0 deletions app/views/tweets/show_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- <h2>You just tweeted:<h2>
<h5> <%= @tweet.content %> </h5>

<form method="post" action="/tweets/<%[email protected]%>/delete">
<input id="hidden" type="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete Tweet">
</form>

<a href="/tweets/<%[email protected]%>/edit"> Edit </a><br>
-->
<!-- <%= @tweet.content %>

<form method="post" action="/tweets/<%[email protected]%>/delete">
<input id="hidden" type="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete Tweet">
</form>

<a href="/tweets/<%[email protected]%>/edit">Edit Tweet </a> -->
<h1>Tweet Show Page</h1>
<p><%= @tweet.content %></p>
<a href='/tweets/<%= @tweet.id %>/edit'>Edit Tweet</a>
<br></br>
<a href = "/tweets/new" > New tweet</a>
<br></br>
<a href = "/tweets" > All tweets</a>
<br></br>

<form method="post" action="/tweets/<%[email protected]%>">
<input id="hidden" type="hidden" name="_method" value="delete">

<input type="submit" value="Delete Tweet">
<form>
29 changes: 29 additions & 0 deletions app/views/tweets/tweets.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!-- <h2>Welcome, <%= @user.username %> </h2><br>

<a href="/tweets/new"> Create Tweet </a><br>

<h4>Your Tweets:</h4>

<% @tweets.each do |tweet| %>
<div>
<%= tweet.content %>
</div>
<% end %> -->


<p> Welcome, <%= current_user.username %>
<h2> Tweets </h2>

<% @tweets.each do |tweet| %>
<div>
<%= tweet.content %>
</div>
<% end %>
<!--
<h1>Tweets</h1>
<h3>Welcome, <%[email protected]%> </h3>
<% Tweet.all.each do |tweet| %>
<a href="/tweets/<%=tweet.id%>"> <%= tweet.content %> </a>
<br></br>
<% end %>
<a href = "/tweets/new" >Write a new tweet</a> -->
9 changes: 9 additions & 0 deletions app/views/users/create_user.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<form action="/signup" method="post">
<label for="username"> Username: </label>
<input type="text" name="username">
<label for="email"> Email: </label>
<input type="text" name="email">
<label for="username"> Password: </label>
<input type="text" name="password">
<input type="submit" value="submit">
</form>
7 changes: 7 additions & 0 deletions app/views/users/login.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<form action="/login" method="post">
<label for="username"> Username: </label>
<input type="text" name="username">
<label for="password"> Password: </label>
<input type="text" name="password">
<input type="submit" id="submit" value="Login">
</form>
4 changes: 0 additions & 4 deletions app/views/users/logout.erb

This file was deleted.

6 changes: 6 additions & 0 deletions app/views/users/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p> Here are your tweets, <%= @user.username %>
<% @user.tweets.each do |tweet| %>
<div>
<%= tweet.content %>
</div>
<% end %>
Binary file added db/development.sqlite
Binary file not shown.
Loading