Skip to content

Instantly share code, notes, and snippets.

View sushant12's full-sized avatar
😎
Focusing

Suss Buzz sushant12

😎
Focusing
  • Kathmandu
View GitHub Profile
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:04
setting up the views
module TemplateView
def self.home_view
home_view = <<-HTML
<html>
<head> <title> Home </title> </head>
<body>
<form method=”post” action=”save_task”>
Task Name: <input type=”text” name=”task”>
<input type=”submit” value=”save” >
</form>
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:07
render home view when user visits /
module Todo
class Application
include TemplateView
class << self
def call(env)
if(env[‘PATH_INFO’] == ‘/’)
view = TemplateView.home_view
end
end
end
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:08
requiring
require ‘erb’
require “sqlite3”
DB = SQLite3::Database.new “test.db”
rows = db.execute <<-SQL
create table tasks ( name varchar(30), finished int );
SQL
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:09
saving to db
elsif(env[‘PATH_INFO’] == ‘/save_task’ && env[‘REQUEST_METHOD’] == “POST”)
req = Rack::Request.new(env)
task_name = req.params[“task”]
DB.execute(“INSERT INTO tasks (name, finished) VALUES (?, ?)”, [task_name,0])
return [ 302, {‘Location’ =>”/”}, [] ]
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:09
listing the tasks
<% DB.execute( “select rowid, * from tasks” ) do |row| %>
<li><%= row[1] %> <% if(row[2] == 1) %>(finished) <% end %><a href=”/edit?id=<%= row[0]%>”>edit </a> <a href=”/delete?id=<%= row[0]%>”> del</a></li>
<% end %>
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:10
deleting the task
elsif(env[‘PATH_INFO’] ==’/delete’)
req = Rack::Request.new(env) id = req.params[“id”] DB.execute(“delete from tasks where rowid=’#{id}’”)
return [ 302, {‘Location’ =>”/”}, [] ]
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:10
edit and update the task
elsif(env[‘PATH_INFO’] == ‘/edit’)
req = Rack::Request.new(env)
id = req.params[‘id’]
view = TemplateView.edit_view(id)
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:11
fetching data for edit form
<% DB.execute(“select rowid, * from tasks where rowid = #{id}”) do |row| %>
<form method=”post” action=”/update?id=<%= row[0]%>”>
Task Name: <input type=”text” name=”task” value=”<%= row[1] %>”>
Finished?: <input type=”checkbox” name=”finished” value=”1" <% if(row[2] == 1) %> checked <% end%>>
<input type=”submit” value=”Update” >
</form>
<% end %>
@sushant12
sushant12 / config.ru
Created June 2, 2017 09:12
updating the task
elsif(env[‘PATH_INFO’] ==’/update’ && env[‘REQUEST_METHOD’] == “POST”)
req = Rack::Request.new(env)
id = req.params[“id”]
task = req.params[“task”]
finish = 0
if(req.params.include? ‘finished’)
finish = 1
end
DB.execute(“UPDATE tasks set name = ‘#{task}’, finished = ‘#{finish}’ where rowid=’#{id}’”)
return [ 302, {‘Location’ =>”/”}, [] ]
source 'https://rubygems.org'
gem 'rack'
gem 'pg'
gem 'puma'
gem 'tilt'