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 TaskContent string
var TaskCreated time.Time
var TaskPriority string
var getTasksql string
basicSQL := "select id, title, content, created_date, priority from task "
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" {
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" {
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)
@ -49,13 +51,13 @@ func GetTasks(status string) types.Context {
}
defer rows.Close()
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)
if err != nil {
log.Println(err)
}
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)
}
context = types.Context{Tasks: task, Navigation: status}
@ -66,10 +68,8 @@ func GetTasks(status string) types.Context {
func GetTaskByID(id int) types.Context {
var tasks []types.Task
var task types.Task
var TaskID int
var TaskTitle string
var TaskContent string
getTasksql := "select id, title, content from task where id=?"
getTasksql := "select id, title, content, priority from task where id=?"
rows, err := database.Query(getTasksql, id)
if err != nil {
@ -77,11 +77,11 @@ func GetTaskByID(id int) types.Context {
}
defer rows.Close()
if rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
err := rows.Scan(&task.Id, &task.Title, &task.Content, &task.Priority)
if err != nil {
log.Println(err)
//send email to respective people
}
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
}
tasks = append(tasks, task)
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
func AddTask(title, content string) error {
restoreSQL, err := database.Prepare("insert into task(title, content, created_date, last_modified_at) values(?,?,datetime(), datetime())")
func AddTask(title, content string, taskPriority int) error {
restoreSQL, err := database.Prepare("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,datetime(), datetime())")
if err != nil {
log.Println(err)
}
tx, err := database.Begin()
_, err = tx.Stmt(restoreSQL).Exec(title, content)
_, err = tx.Stmt(restoreSQL).Exec(title, content, taskPriority)
if err != nil {
log.Println(err)
tx.Rollback()

View File

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

View File

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

View File

@ -15,6 +15,12 @@
rows="10" style="border:none;border-bottom:1px solid gray; box-shadow:none;">{{.Content}}</textarea>
<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 class="modal-footer">
@ -25,7 +31,7 @@
</div>
</div>
{{end}}
{{template "_footer.gtpl"}}
{{template "_footer.html"}}
</body>

View File

@ -14,14 +14,20 @@
<div class="modal-body">
<form enctype="multipart/form-data" action="/add/" method="POST">
<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="hidden" name="CSRFToken" value={{.CSRFToken}}>
</div>
<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>
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>
@ -42,6 +48,7 @@
<p class="noteContent">{{.Content}}</p>
<span class="notefooter">
<ul class="menu">
<li role="presentation">Priority: {{.Priority}}</li>
<li role="presentation">
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li>
<li role="presentation">

View File

@ -6,6 +6,7 @@
<p class="noteContent">{{.Content}}</p>
<span class="notefooter">
<ul class="menu">
<li role="presentation">Priority: {{.Priority}}</li>
<li role="presentation">
<span class="glyphicon glyphicon-time"></span> {{.Created}}</li>
<!-- <li role="presentation">
@ -34,7 +35,7 @@
</div>
{{end}}
</div>
{{template "_footer.gtpl"}}
{{template "_footer.html"}}
</body>

View File

@ -6,5 +6,5 @@ CREATE TABLE task (
created_date timestamp,
last_modified_at timestamp,
finish_date timestamp
);
, priority int);

View File

@ -6,6 +6,7 @@ type Task struct {
Title string
Content string
Created string
Priority string
}
//Context is the struct passed to templates

View File

@ -3,6 +3,7 @@ package views
import (
"bufio"
"github.com/thewhitetulip/Tasks/db"
"io"
"io/ioutil"
"log"
"net/http"
@ -11,7 +12,6 @@ import (
"strings"
"text/template"
"time"
"io"
)
var homeTemplate *template.Template
@ -113,7 +113,17 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("uploadfile")
if err != nil {
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"))
content := template.HTMLEscapeString(r.Form.Get("content"))
@ -131,11 +141,11 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
}
defer f.Close()
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
}
truth := db.AddTask(title, content)
truth := db.AddTask(title, content, taskPriority)
if truth != nil {
message = "Error adding task"
log.Println("error adding task to db")