-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
30 lines (24 loc) · 1.02 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require "sinatra" # Sinatra web server
require "erb" # Ruby templates
require "date" # date parser
class TaskList < Sinatra::Base
task_list = []
id_counter = 1
def overdue?(due_date) #function to check if task is overdue
Date.parse(due_date) < Date.today #parse the date string
end
get "/" do #get request from index page (home page)
@tasks = task_list #set tasks to task list array
erb :index #redirect to index page (home page)
end
post "/add" do #post request to add task to task list
task_list << { id: id_counter, name: params[:task], due_date: params[:due_date] } #add task based on entered form inputs
id_counter += 1 #increase id counter by 1 when task is added
redirect "/" #redirect to index page (home page)
end
post "/delete/:id" do #post request to delete task from task id
task_list.reject! { |task| task[:id] == params[:id].to_i } #find task to deleet based on task id
redirect "/" #redirect to index page (home page)
end
end
TaskList.run! #run the task list application