added categories

This commit is contained in:
Suraj 2016-02-02 23:10:44 +05:30
parent 8be63f0bf1
commit 5bdbe6f7b2
8 changed files with 156 additions and 14 deletions

View File

@ -40,3 +40,43 @@ func GetFileName(token string) (string, error) {
return fileName, nil return fileName, nil
} }
//GetCategories will return the list of categories to be
//rendered in the template
func GetCategories() []string {
stmt := "select name from category"
rows := database.query(stmt)
var categories []string
var category string
for rows.Next() {
err := rows.Scan(&category)
if err != nil {
log.Println(err)
}
categories = append(categories, category)
}
return categories
}
//AddCategory is used to add the task in the database
func AddCategory(category string) error {
err := taskQuery("insert into category(name) values(?)", category)
return err
}
// GetCategoryById will return the ID of that category passed as args
// used while inserting tasks into the table
func GetCategoryById(category string) int {
stmt := "select id from category where name=?"
rows := database.query(stmt, category)
var categoryID int
for rows.Next() {
err := rows.Scan(&categoryID)
if err != nil {
log.Println(err)
}
}
return categoryID
}

View File

@ -67,7 +67,7 @@ func Close() {
//GetTasks retrieves all the tasks depending on the //GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed //status pending or trashed or completed
func GetTasks(status string) (types.Context, error) { func GetTasks(status, category string) (types.Context, error) {
var task []types.Task var task []types.Task
var context types.Context var context types.Context
var TaskID int var TaskID int
@ -76,9 +76,10 @@ func GetTasks(status string) (types.Context, error) {
var TaskCreated time.Time var TaskCreated time.Time
var TaskPriority string var TaskPriority string
var getTasksql string var getTasksql string
var rows *sql.Rows
basicSQL := "select id, title, content, created_date, priority from task " basicSQL := "select id, title, content, created_date, priority from task t"
if status == "pending" { if status == "pending" && category == "" {
getTasksql = basicSQL + " where finish_date is null and is_deleted='N' order by priority desc, 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 = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc" getTasksql = basicSQL + " where is_deleted='Y' order by priority desc, created_date asc"
@ -86,7 +87,16 @@ func GetTasks(status string) (types.Context, error) {
getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc" getTasksql = basicSQL + " where finish_date is not null order by priority desc, created_date asc"
} }
rows := database.query(getTasksql) if category != "" {
status = category
getTasksql = "select t.id, title, content, created_date, priority from task t, category c where c.id = t.cat_id and name = ?"
rows, err = database.db.Query(getTasksql, category)
if err != nil {
log.Println("something went wrong while getting query")
}
} else {
rows = database.query(getTasksql)
}
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority) err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated, &TaskPriority)
@ -161,8 +171,14 @@ 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, taskPriority int) error { func AddTask(title, content, category string, taskPriority int) error {
err := taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority) var err error
if category == "" {
err = taskQuery("insert into task(title, content, priority, created_date, last_modified_at) values(?,?,?,datetime(), datetime())", title, content, taskPriority)
} else {
categoryID := GetCategoryById(category)
err = taskQuery("insert into task(title, content, priority, created_date, last_modified_at, cat_id) values(?,?,?,datetime(), datetime(), ?)", title, content, taskPriority, categoryID)
}
return err return err
} }

View File

@ -16,6 +16,8 @@ func main() {
values := config.ReadConfig("config.json") values := config.ReadConfig("config.json")
views.PopulateTemplates() views.PopulateTemplates()
http.HandleFunc("/", views.ShowAllTasksFunc) http.HandleFunc("/", views.ShowAllTasksFunc)
http.HandleFunc("/add-category/", views.AddCategoryFunc)
http.HandleFunc("/category/", views.ShowCategoryFunc)
http.HandleFunc("/complete/", views.CompleteTaskFunc) http.HandleFunc("/complete/", views.CompleteTaskFunc)
//delete permanently deletes from db //delete permanently deletes from db
http.HandleFunc("/delete/", views.DeleteTaskFunc) http.HandleFunc("/delete/", views.DeleteTaskFunc)

View File

@ -8,6 +8,7 @@
{{ else if eq .Navigation "completed"}}Completed {{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted {{ else if eq .Navigation "deleted"}}Deleted
{{ else if eq .Navigation "edit"}} Edit {{ else if eq .Navigation "edit"}} Edit
{{else }} {{.Navigation}}
{{end}} {{end}}
</title> </title>
@ -49,11 +50,17 @@
{{if .Search}} <a class="navbar-brand"> Results for: {{.Search}}</a> {{if .Search}} <a class="navbar-brand"> Results for: {{.Search}}</a>
{{else}} {{else}}
<a class="navbar-brand" <a class="navbar-brand"
href='{{ if eq .Navigation "pending"}} / {{else}} /{{.Navigation}} {{end}}'> href='{{ if eq .Navigation "pending"}} /
{{else if eq .Navigation "completed"}} /{{.Navigation}}
{{else if eq .Navigation "deleted"}} /{{.Navigation}}
{{else if eq .Navigation "edit"}} /{{.Navigation}}
{{else}} /category/{{.Navigation}}
{{end}}'>
{{if eq .Navigation "pending"}} Pending {{if eq .Navigation "pending"}} Pending
{{ else if eq .Navigation "completed"}}Completed {{ else if eq .Navigation "completed"}}Completed
{{ else if eq .Navigation "deleted"}}Deleted {{ else if eq .Navigation "deleted"}}Deleted
{{ else if eq .Navigation "edit"}} Edit {{ else if eq .Navigation "edit"}} Edit
{{else }} {{.Navigation}}
{{end}}</a> {{end}}</a>
{{end}} {{end}}
<span id="icons"> <span id="icons">
@ -77,16 +84,38 @@
<ul class="sidebar-menu"> <ul class="sidebar-menu">
<li class="sidebar-group"><span>Tasks</span> <li class="sidebar-group"><span>Tasks</span>
<ul class="sidebar-group-menu"> <ul class="sidebar-group-menu">
<li class="sidebar-item">
<li class="sidebar-item">
<a href="/" {{ if eq .Navigation "pending"}} class="active" {{end}} ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a> <a href="/" {{ if eq .Navigation "pending"}} class="active" {{end}} ><span class="glyphicon glyphicon-tasks"></span> <span class="nav-item">Pending</span></a>
</li> </li>
<li class="sidebar-item">
<li class="sidebar-item">
<a href="/completed/" {{ if eq .Navigation "completed"}} class="active" {{end}}><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a> <a href="/completed/" {{ if eq .Navigation "completed"}} class="active" {{end}}><span class="glyphicon glyphicon-check"></span> <span class="nav-item"> Completed</span></a>
</li> </li>
<li class="sidebar-item">
<li class="sidebar-item">
<a href="/deleted/" {{ if eq .Navigation "deleted"}} class="active" {{end}}><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a> <a href="/deleted/" {{ if eq .Navigation "deleted"}} class="active" {{end}}><span class="glyphicon glyphicon-trash"></span> <span class="nav-item"> Deleted</span></a>
</li> </li>
<hr>
<li class="sidebar-item">
<h5> Categories</h5>
<!--<button title="New category" id="toggleCategoryForm" data-placement="left" data-toggle="tooltip" class="btn glyphicon glyphicon-plus" style="font-size:small; margin-left:120px;"></button>-->
<span id="categoryForm">
<form action="/add-category/" method="POST">
<input type="text" name="category" width="50px" style="border:none;border-bottom:1px solid gray; box-shadow:none;">
<input type="submit" text="submit" class="btn btn-default" width="50px" height="30px"/>
</form>
</span>
</li>
{{ range .Categories }}
<li class="sidebar-item">
<a href="/category/{{.}}" > <span class="glyphicon glyphicon-stop"></span> <span class="nav-item"> {{.}}</span></a>
</li>
{{end}}
</ul> </ul>
</li> </li>
</ul> </ul>

View File

@ -27,6 +27,13 @@
<input type="radio" name="priority" value="2" /> Medium <input type="radio" name="priority" value="2" /> Medium
<input type="radio" name="priority" value="1" /> Low <input type="radio" name="priority" value="1" /> Low
</div> </div>
<select name="category">
<option>---</option>
{{range .Categories}}
<option value="{{.}}" > {{.}} </option>
{{end}}
</select>
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>

View File

@ -20,4 +20,5 @@ type Context struct {
Search string Search string
Message string Message string
CSRFToken string CSRFToken string
Categories []string
} }

View File

@ -61,6 +61,8 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
if !found { if !found {
taskPriority = 1 taskPriority = 1
} }
category := r.FormValue("category")
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"))
@ -93,7 +95,7 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
} }
} }
taskTruth := db.AddTask(title, content, taskPriority) taskTruth := db.AddTask(title, content, category, taskPriority)
if taskTruth != nil { if taskTruth != nil {
message = "Error adding task" message = "Error adding task"
@ -115,3 +117,19 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//AddCategoryFunc used to add new categories to the database
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
category := r.Form.Get("category")
if category != "" {
err := db.AddCategory(category)
if err != nil {
message = "Error adding category"
http.Redirect(w, r, "/", http.StatusBadRequest)
} else {
message = "Added category"
http.Redirect(w, r, "/", http.StatusFound)
}
}
}

View File

@ -23,7 +23,8 @@ var err error
//TODO add http404 error //TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context, err := db.GetTasks("pending") context, err := db.GetTasks("pending", "")
categories := db.GetCategories()
if err != nil { if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError) http.Redirect(w, r, "/", http.StatusInternalServerError)
} }
@ -31,6 +32,7 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
context.Message = message context.Message = message
} }
context.CSRFToken = "abcd" context.CSRFToken = "abcd"
context.Categories = categories
message = "" message = ""
expiration := time.Now().Add(365 * 24 * time.Hour) expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration} cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
@ -45,7 +47,7 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks //ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) { func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context, err := db.GetTasks("deleted") context, err := db.GetTasks("deleted", "")
if err != nil { if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError) http.Redirect(w, r, "/trash", http.StatusInternalServerError)
} }
@ -63,7 +65,7 @@ func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
//ShowCompleteTasksFunc is used to populate the "/completed/" URL //ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) { func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { if r.Method == "GET" {
context, err := db.GetTasks("completed") context, err := db.GetTasks("completed", "")
if err != nil { if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError) http.Redirect(w, r, "/completed", http.StatusInternalServerError)
} }
@ -73,3 +75,30 @@ func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
} }
} }
//ShowCategoryFunc will populate the /category/<id> URL which shows all the tasks related
// to that particular category
func ShowCategoryFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
category := r.URL.Path[len("/category/"):]
context, err := db.GetTasks("", category)
categories := db.GetCategories()
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
}
context.CSRFToken = "abcd"
context.Categories = categories
message = ""
expiration := time.Now().Add(365 * 24 * time.Hour)
cookie := http.Cookie{Name: "csrftoken", Value: "abcd", Expires: expiration}
http.SetCookie(w, &cookie)
homeTemplate.Execute(w, context)
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
}
}