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

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

Done #1148

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
18 changes: 18 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
85 changes: 85 additions & 0 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h3><a href="/login">Login</a></h3>
<h3><a href="/signup">Sign Up</a></h3>
18 changes: 12 additions & 6 deletions app/views/layout.erb
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
<!doctype html>
<html>
<head>
<title>Fwitter</title>
<title>Twitter</title>
</head>
<body>

<h1>Welcome to Fwitter!</h1>
<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 %>

</div>
</body>
<footer>
<!-- <% if logged_in? %>-->
<!-- <a href="/logout">Log Out</a> -->
<!-- <%end%> -->
<br>
<a href="/"><button type="button">Home</button></a>
</footer>
</html>
11 changes: 11 additions & 0 deletions app/views/tweets/edit_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<form action='/tweets/<%[email protected]%>' method="post">
<input type="hidden" id="hidden" name="_method" value="patch">

Content:
<input id="content" type="text" name="content" value="<%[email protected]%>"><br><br>
Edit Your Tweet:<br>

<input id="submit" type="submit" value="Edit Tweet">


</form>
14 changes: 2 additions & 12 deletions app/views/tweets/new.erb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
<% if Helpers.is_logged_in?(session) %>
<%= @error %>
<form action='/tweets' method="post">
Content:
<input type="text" name="content"><br>
Create Your Tweet:

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

<input type="submit" name="submit">
<input id="content" type="text" name="content">

<input class="btn btn-primary" type="submit" value="submit">
</form>

<% else %>
<h1> You Must Be Logged in to Make Tweets </h1>
<% end %>
7 changes: 7 additions & 0 deletions app/views/tweets/show_tweet.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<p><%= @tweet.content %></p>

<form method="post" action="/tweets/<%[email protected]%>/delete">
<input type="hidden" id="hidden" name="_method" value="delete">
<input id="submit" type="submit" value="Delete Tweet">
</form>
<a href="/tweets/<%[email protected]%>/edit">Edit Tweet</a>
10 changes: 10 additions & 0 deletions app/views/tweets/tweets.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<p> Welcome, <%= current_user.username %>

<h2>Tweets</h2>

<% @tweets.each do |x| %>
<div>
<%= x.content %>
<div>
<% end %>

16 changes: 16 additions & 0 deletions app/views/users/create_user.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h2>Sign Up</h2>

<form method="POST" action='/signup'>

<label for="email">Enter Email Address:</label>
<input type="text" id="email" name="email"><br><br>

<label for="username">Pick a Username:</label>
<input type="text" id="username" name="username"><br><br>

<label for="password">Pick a Password:</label>
<input type="password" id="password" name="password"><br><br>

<input id="submit" type="submit" value="Create Account">

</form>
13 changes: 13 additions & 0 deletions app/views/users/login.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Login</h2>

<form method="POST" action='/login'>

<label for="username">Enter Username:</label>
<input type="text" id="username" name="username"><br><br>

<label for="password">Enter Password:</label>
<input type="password" id="password" name="password"><br><br>

<input id="submit" type="submit" value="Login">

</form>
3 changes: 3 additions & 0 deletions app/views/users/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @user.tweets.each do |x| %>
<p><%= x.content %></p>
<%end%>
Binary file added db/development.sqlite
Binary file not shown.
9 changes: 9 additions & 0 deletions db/migrate/20200402202503_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 |t|
t.string :username
t.string :email
t.string :password_digest
end
end
end
8 changes: 8 additions & 0 deletions db/migrate/20200402202917_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
26 changes: 26 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -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
Binary file modified db/test.sqlite
Binary file not shown.