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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Done #1155

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
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

class ApplicationController < Sinatra::Base


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

get "/" do
erb :root
end



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

get "/tweets" do
if session[:user_id]
@tweets = Tweet.all
erb :"tweets/index"
else
redirect "/login"
end
end

get "/tweets/new" do
if session[:user_id]
erb :"tweets/new"
else
redirect "/login"
end
end

post "/tweets" do
@tweet = Tweet.new(params)
if @tweet.save
redirect "/tweets"
else
redirect "/tweets/new"
end
end

get "/tweets/:id" do
if session[:user_id]
@tweet = Tweet.find(params[:id])
erb :"tweets/show"
else
redirect "/login"
end
end

delete "/tweets/:id" do
if !session[:user_id]
redirect "/login"
end
@tweet = Tweet.find(params[:id])
if session[:user_id] == @tweet.id
@tweet.destroy
redirect "/tweets"
else
redirect "/tweets/#{@tweet.id}"
end
end

get "/tweets/:id/edit" do
if !session[:user_id]
redirect "/login"
end
@tweet = Tweet.find(params[:id])

if session[:user_id] == @tweet.user.id
erb :"tweets/edit"
else
redirect "/tweets/#{@tweet.id}"
end
end

patch "/tweets/:id" do
@tweet = Tweet.find(params[:id])
if @tweet.update(params[:tweet])
redirect "/tweets/#{@tweet.id}"
else
redirect "/tweets/#{@tweet.id}/edit"
end
end

end
49 changes: 49 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,53 @@
class UsersController < ApplicationController

get "/signup" do
if session[:user_id]
redirect "/tweets"
else
erb :"users/signup"
end
end

get "/login" do
if session[:user_id]
redirect "/tweets"
else
erb :"users/login"
end
end

post "/login" do
@user = User.find_by(username: params[:username])
if @user.authenticate(params[:password])
session[:user_id] = @user.id
redirect "/tweets"
else
redirect "/login"
end
end

post "/signup" do
@user = User.new(params)
if @user.save
session[:user_id] = @user.id
redirect "/tweets"
else
redirect "/signup"
end
end

get "/logout" do
if session[:user_id]
session.clear
redirect "/login"
else
redirect "/login"
end
end

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

end
9 changes: 9 additions & 0 deletions app/helpers/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Helpers
def self.current_user(session)
User.find(session[:user_id])
end

def self.is_logged_in?(session)
!!session[:user_id]
end
end
1 change: 1 addition & 0 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Tweet < ActiveRecord::Base
belongs_to :user
validates :content, length: { minimum: 1 }
end
10 changes: 10 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class User < ActiveRecord::Base
has_secure_password
has_many :tweets
validates :username, presence: true
validates :email, presence: true

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

def self.find_by_slug(slug)
self.all.find { |user| user.slug == slug }
end
end
9 changes: 6 additions & 3 deletions app/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
<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>
<% if session[:user_id]%>
<h1>Welcome, <%= User.find(session[:user_id]) %></h1>
<% else%>
<h1>Welcome to Fwitter!</h1>
<% end %>
<%= yield %>
</div>

</body>
Expand Down
Empty file added app/views/root.erb
Empty file.
13 changes: 13 additions & 0 deletions app/views/tweets/edit.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h1>Edit Tweet:</h1>

<form action=<%= "/tweets/#{@tweet.id}"%> method="POST">
<input type="hidden" id="hidden" name="_method" value="PATCH">
Content:
<input type="text" id="content" name="tweet[content]" value=<%= @tweet.content %>><br>
Update Your Tweet:
<input type="hidden" id="user_id" name="tweet[user_id]" value=<%= Helpers.current_user(session).id %>>

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


</form>
10 changes: 10 additions & 0 deletions app/views/tweets/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Tweets:</h1>

<ul>
<% @tweets.each do |tweet|%>
<li>
<h5><%=tweet.user.username%></h5>
<p><%= tweet.content %></p>
</li>
<% end%>
</ul>
4 changes: 2 additions & 2 deletions app/views/tweets/new.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
Content:
<input type="text" name="content"><br>
Create Your Tweet:
<input type="hidden" name="user_id" value=<%= Helpers.current_user(session).id %>>

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

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

</form>

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

<h2>Content:</h2>
<p><%= @tweet.content %></p>

<form method="POST" action=<%="/tweets/#{@tweet.id}"%>>
<input type="hidden" id="hidden" name="_method" value="DELETE">
<input type="submit" value="Delete Tweet">
</form>
<form method="GET" action=<%="/tweets/#{@tweet.id}/edit"%>>
<input type="submit" value="Edit Tweet">
</form>
8 changes: 8 additions & 0 deletions app/views/users/login.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Login:</h1>
<form method="POST" action="/login">
<label for="username">Username:</label>
<input type="text" name="username"><br>
<label for="password">Password:</label>
<input type="text" name="password"><br>
<input type="submit" id="submit" value="Submit">
</form>
7 changes: 7 additions & 0 deletions app/views/users/show.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1><%[email protected]%></h1>

<ul>
<% @user.tweets.each do |tweet|%>
<li><%= tweet.content %></li>
<% end%>
</ul>
11 changes: 11 additions & 0 deletions app/views/users/signup.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Signup</h1>

<form method="POST" action="/signup">
<label for="username">Username:</label>
<input type="text" name="username"><br>
<label for="email">Email:</label>
<input type="text" name="email"><br>
<label for="password">Password:</label>
<input type="text" name="password"><br>
<input type="submit">
</form>
Binary file added db/development.sqlite
Binary file not shown.
9 changes: 9 additions & 0 deletions db/migrate/20210217140035_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/20210217140042_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.belongs_to :user
end
end
end
27 changes: 27 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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: 2021_02_17_140042) do

create_table "tweets", force: :cascade do |t|
t.string "content"
t.integer "user_id"
t.index ["user_id"], name: "index_tweets_on_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.