added priority to tasks

This commit is contained in:
Suraj Patil 2016-01-23 19:17:34 +05:30
parent 43278053cb
commit 43bd75fe78
9 changed files with 75 additions and 48 deletions

View File

@ -33,14 +33,16 @@ func GetTasks(status string) types.Context {
var TaskTitle string var TaskTitle string
var TaskContent string var TaskContent string
var TaskCreated time.Time var TaskCreated time.Time
var TaskPriority string
var getTasksql string var getTasksql string
basicSQL := "select id, title, content, created_date, priority from task "
if status == "pending" { if status == "pending" {
getTasksql = "select id, title, content, created_date from task where finish_date is null and is_deleted='N' order by created_date asc" getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, created_date asc"
} else if status == "deleted" { } else if status == "deleted" {
getTasksql = "select id, title, content, created_date from task where is_deleted='Y' order by created_date asc" getTasksql = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc"
} else if status == "completed" { } else if status == "completed" {
getTasksql = "select id, title, content, created_date from task where finish_date is not null order by created_date asc" getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc"
} }
rows, err := database.Query(getTasksql) rows, err := database.Query(getTasksql)
@ -49,13 +51,13 @@ func GetTasks(status string) types.Context {
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated) err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1) TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
TaskCreated = TaskCreated.Local() TaskCreated = TaskCreated.Local()
a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20]} a := types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent, Created: TaskCreated.Format(time.UnixDate)[0:20], Priority: TaskPriority}
task = append(task, a) task = append(task, a)
} }
context = types.Context{Tasks: task, Navigation: status} context = types.Context{Tasks: task, Navigation: status}
@ -66,10 +68,8 @@ func GetTasks(status string) types.Context {
func GetTaskByID(id int) types.Context { func GetTaskByID(id int) types.Context {
var tasks []types.Task var tasks []types.Task
var task types.Task var task types.Task
var TaskID int
var TaskTitle string getTasksql := "select id, title, content, priority from task where id=?"
var TaskContent string
getTasksql := "select id, title, content from task where id=?"
rows, err := database.Query(getTasksql, id) rows, err := database.Query(getTasksql, id)
if err != nil { if err != nil {
@ -77,11 +77,11 @@ func GetTaskByID(id int) types.Context {
} }
defer rows.Close() defer rows.Close()
if rows.Next() { if rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent) err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
//send email to respective people
} }
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
} }
tasks = append(tasks, task) tasks = append(tasks, task)
context := types.Context{Tasks: tasks, Navigation: "edit"} context := types.Context{Tasks: tasks, Navigation: "edit"}
@ -209,13 +209,13 @@ func DeleteTask(id int) error {
} }
//AddTask is used to add the task in the database //AddTask is used to add the task in the database
func AddTask(title, content string) error { func AddTask(title, content string, taskPriority int) error {
restoreSQL, err := database.Prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())") restoreSQL, err := database.Prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,datetime(), datetime())")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
tx, err := database.Begin() tx, err := database.Begin()
_, err = tx.Stmt(restoreSQL).Exec(title, content) _, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
tx.Rollback() tx.Rollback()

View File

@ -7,6 +7,7 @@
<p class="noteContent">{{.Content}}</p> <p class="noteContent">{{.Content}}</p>
<span class="notefooter"> <span class="notefooter">
<ul class="menu"> <ul class="menu">
<li role="presentation">Priority: {{.Priority}}</li>
<li role="presentation"> <li role="presentation">
<a role="menuitem" tabindex="-1" href="/incomplete/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/incomplete/{{.Id}}">
<span class="glyphicon glyphicon-eye-close"></span></a> <span class="glyphicon glyphicon-eye-close"></span></a>
@ -35,7 +36,7 @@
</div> </div>
{{end}} {{end}}
</div> </div>
{{template "_footer.gtpl"}} {{template "_footer.html"}}
</body> </body>

View File

@ -25,6 +25,7 @@
<a role="menuitem" tabindex="-1" href="/archive/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/archive/{{.Id}}">
<span class="glyphicon glyphicon-inbox"></span> Edit</a> <span class="glyphicon glyphicon-inbox"></span> Edit</a>
</li>!--> </li>!-->
<li role="presentation">Priority: {{.Priority}}</li>
<li role="presentation"> <li role="presentation">
<a role="menuitem" tabindex="-1" href="/delete/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/delete/{{.Id}}">
<span class="glyphicon glyphicon-trash"></span></a> <span class="glyphicon glyphicon-trash"></span></a>
@ -43,7 +44,7 @@
</div> </div>
{{end}} {{end}}
</div> </div>
{{template "footer.gtpl"}} {{template "footer.html"}}
</body> </body>

View File

@ -15,6 +15,12 @@
rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;">{{.Content}}</textarea> rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;">{{.Content}}</textarea>
<input type="text" name="id" value="{{.Id}}" class="hidden" /> <input type="text" name="id" value="{{.Id}}" class="hidden" />
Priority: <select name="priority">
<option>---</option>
<option {{if eq .Priority "3"}} selected="true" {{end}} value="high">High</option>
<option {{if eq .Priority "2"}} selected="true" {{end}} value="medium">Medium</option>
<option {{if eq .Priority "1"}} selected="true" {{end}} value="low">Low</option>
</select>
</div> </div>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
@ -25,7 +31,7 @@
</div> </div>
</div> </div>
{{end}} {{end}}
{{template "_footer.gtpl"}} {{template "_footer.html"}}
</body> </body>

View File

@ -14,14 +14,20 @@
<div class="modal-body"> <div class="modal-body">
<form enctype="multipart/form-data" action="/add/" method="POST"> <form enctype="multipart/form-data" action="/add/" method="POST">
<div class="form-group"> <div class="form-group">
<!-- <label for="note-title" class="control-label">Title:</label> -->
<input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;"> <input type="text" name="title" class="form-control" id="add-note-title" placeholder="Title" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
<input type="hidden" name="CSRFToken" value={{.CSRFToken}}> <input type="hidden" name="CSRFToken" value={{.CSRFToken}}>
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="file" name="uploadfile" />
<!-- <label for="note-content" class="control-label">Content:</label> -->
<textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;"></textarea> <textarea class="form-control" name="content" id="add-note-content" placeholder="Content" rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;"></textarea>
File: <input type="file" name="uploadfile" /> <br>
Priority: <select name="priority">
<option>---</option>
<option value="3">High</option>
<option value="2">Medium</option>
<option value="1">Low</option>
</select>
</div> </div>
</div> </div>
@ -41,11 +47,12 @@
<p class="noteContent">{{.Content}}</p> <p class="noteContent">{{.Content}}</p>
<span class="notefooter"> <span class="notefooter">
<ul class="menu"> <ul class="menu">
<li role="presentation"> <li role="presentation">Priority: {{.Priority}}</li>
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li> <li role="presentation">
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li>
<li role="presentation"> <li role="presentation">
<a role="menuitem" tabindex="-1" href="/mask/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/mask/{{.Id}}">
<span class="glyphicon glyphicon-lock"></span></a></li> <span class="glyphicon glyphicon-lock"></span></a></li>
<li role="presentation"> <li role="presentation">
<a role="menuitem" tabindex="-1" href="/trash/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/trash/{{.Id}}">

View File

@ -5,9 +5,10 @@
<p class="noteHeading">{{.Title}}</p> <span class="toggle glyphicon glyphicon-resize-full"></span> <p class="noteHeading">{{.Title}}</p> <span class="toggle glyphicon glyphicon-resize-full"></span>
<p class="noteContent">{{.Content}}</p> <p class="noteContent">{{.Content}}</p>
<span class="notefooter"> <span class="notefooter">
<ul class="menu"> <ul class="menu">
<li role="presentation"> <li role="presentation">Priority: {{.Priority}}</li>
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li> <li role="presentation">
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li>
<!-- <li role="presentation"> <!-- <li role="presentation">
<a role="menuitem" tabindex="-1" href="/mask/{{.Id}}"> <a role="menuitem" tabindex="-1" href="/mask/{{.Id}}">
<span class="glyphicon glyphicon-lock"></span> Mask</a></li> !--> <span class="glyphicon glyphicon-lock"></span> Mask</a></li> !-->
@ -34,7 +35,7 @@
</div> </div>
{{end}} {{end}}
</div> </div>
{{template "_footer.gtpl"}} {{template "_footer.html"}}
</body> </body>

View File

@ -1,10 +1,10 @@
CREATE TABLE task ( CREATE TABLE task (
id integer primary key autoincrement, id integer primary key autoincrement,
title varchar(100), title varchar(100),
content text, content text,
is_deleted char(1) default 'N', is_deleted char(1) default 'N',
created_date timestamp, created_date timestamp,
last_modified_at timestamp, last_modified_at timestamp,
finish_date timestamp finish_date timestamp
); , priority int);

View File

@ -2,10 +2,11 @@ package types
//Task is the struct used to identify tasks //Task is the struct used to identify tasks
type Task struct { type Task struct {
Id int Id int
Title string Title string
Content string Content string
Created string Created string
Priority string
} }
//Context is the struct passed to templates //Context is the struct passed to templates

View File

@ -3,6 +3,7 @@ package views
import ( import (
"bufio" "bufio"
"github.com/thewhitetulip/Tasks/db" "github.com/thewhitetulip/Tasks/db"
"io"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -11,7 +12,6 @@ import (
"strings" "strings"
"text/template" "text/template"
"time" "time"
"io"
) )
var homeTemplate *template.Template var homeTemplate *template.Template
@ -113,12 +113,22 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("uploadfile") file, handler, err := r.FormFile("uploadfile")
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return }
}
taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))
if priorityErr != nil {
log.Print("Someone trying to hack")
}
priorityList := []int{1, 2, 3}
for _, priority := range priorityList {
if taskPriority != priority {
log.Println("someone trying to hack")
}
}
title := template.HTMLEscapeString(r.Form.Get("title")) title := template.HTMLEscapeString(r.Form.Get("title"))
content := template.HTMLEscapeString(r.Form.Get("content")) content := template.HTMLEscapeString(r.Form.Get("content"))
formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken")) formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))
cookie, _ := r.Cookie("csrftoken") cookie, _ := r.Cookie("csrftoken")
if formToken == cookie.Value { if formToken == cookie.Value {
if handler != nil { if handler != nil {
@ -131,11 +141,11 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
} }
defer f.Close() defer f.Close()
io.Copy(f, file) io.Copy(f, file)
filelink := "<br> <a href=./files/"+handler.Filename+">"+ handler.Filename+"</a>" filelink := "<br> <a href=/files/" + handler.Filename + ">" + handler.Filename + "</a>"
content = content + filelink content = content + filelink
} }
truth := db.AddTask(title, content) truth := db.AddTask(title, content, taskPriority)
if truth != nil { if truth != nil {
message = "Error adding task" message = "Error adding task"
log.Println("error adding task to db") log.Println("error adding task to db")