Last active
December 21, 2015 06:18
-
-
Save facerip/6262798 to your computer and use it in GitHub Desktop.
Sinatra Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'sinatra' | |
require 'data_mapper' | |
DataMapper.setup(:default, 'sqlite:///home/facerip/Learn/Sinatra/Projects/exitstatusone/database/exitstatusone.db') | |
class User | |
include DataMapper::Resource | |
property :id, Serial | |
property :user, String, :required => true | |
property :pass, BCryptHash, :required => true | |
end | |
class Post | |
include DataMapper::Resource | |
property :id, Serial | |
property :title, String | |
property :author, String | |
property :body, Text | |
property :category, String | |
property :created_at, String, :length => 0..20 | |
end | |
DataMapper.finalize | |
DataMapper.auto_migrate! | |
class ExitStatusOne < Sinatra::Base | |
enable :sessions | |
set :sessions, :key => 'exitstatusone.session' | |
set :session_secret, SecureRandom.hex(128) | |
helpers do | |
def authenticate(username, password) | |
if user = User.first(:user => username) | |
user = nil if user.pass != password | |
end | |
user | |
end | |
end | |
get '/' do | |
@post = Post.all :limit => 10 | |
erb :index | |
end | |
post '/login' do | |
if (authenticate(params[:user],params[:pass])) | |
session[:admin] = true | |
redirect '/admin' | |
else | |
redirect '/' | |
end | |
end | |
get '/admin' do | |
if session[:admin] == true | |
@post = Post.all | |
erb :admin | |
else | |
redirect '/' | |
end | |
end | |
get '/logout' do | |
session[:admin] = nil | |
redirect '/' | |
end | |
post '/post/create' do | |
if session[:admin] == true | |
post = Post.new | |
post.title = params[:title] | |
post.author = params[:author] | |
post.body = params[:body] | |
post.category = params[:category] | |
post.created_at = Time.now.strftime('%m-%d-%Y %I:%M%p') | |
if post.save | |
status 201 | |
redirect '/' | |
else | |
status 412 | |
redirect '/' | |
end | |
end | |
end | |
get '/post/:id' do | |
@post = Post.get(params[:id]) | |
erb :post | |
end | |
get '/post/:id/edit' do | |
if session[:admin] == true | |
@post = Post.get(params[:id]) | |
erb :edit | |
end | |
end | |
post '/post/:id/edit' do | |
if session[:admin] == true | |
post = Post.get(params[:id]) | |
post.title = params[:title] | |
post.author = params[:author] | |
post.body = params[:body] | |
post.category = params[:category] | |
post.created_at = Time.now.strftime('%m-%d-%Y %I:%M%p') | |
if post.save | |
status 201 | |
redirect '/admin' | |
else | |
status 412 | |
"Did not save post" | |
end | |
end | |
end | |
get '/post/:id/delete' do | |
if session[:admin] == true | |
@post = Post.get(params[:id]) | |
erb :delete | |
end | |
end | |
post '/post/:id' do | |
if session[:admin] == true | |
Post.get(params[:id]).destroy | |
redirect '/admin' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment