modified mark as incomplete
This commit is contained in:
parent
18cb0645e9
commit
cf9cb0eea5
69
db/db.go
69
db/db.go
|
@ -2,7 +2,7 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"log"
|
||||||
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
|
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
|
||||||
"github.com/thewhitetulip/Tasks/types"
|
"github.com/thewhitetulip/Tasks/types"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -15,7 +15,7 @@ var err error
|
||||||
func init() {
|
func init() {
|
||||||
database, err = sql.Open("sqlite3", "./tasks.db")
|
database, err = sql.Open("sqlite3", "./tasks.db")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,14 +45,14 @@ func GetTasks(status string) types.Context {
|
||||||
|
|
||||||
rows, err := database.Query(getTasksql)
|
rows, err := database.Query(getTasksql)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
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)
|
||||||
TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
|
TaskContent = strings.Replace(TaskContent, "\n", "<br>", -1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.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]}
|
||||||
|
@ -73,13 +73,13 @@ func GetTaskByID(id int) types.Context {
|
||||||
|
|
||||||
rows, err := database.Query(getTasksql, id)
|
rows, err := database.Query(getTasksql, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
if rows.Next() {
|
if rows.Next() {
|
||||||
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
|
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
|
task = types.Task{Id: TaskID, Title: TaskTitle, Content: TaskContent}
|
||||||
}
|
}
|
||||||
|
@ -92,15 +92,15 @@ func GetTaskByID(id int) types.Context {
|
||||||
func TrashTask(id int) error {
|
func TrashTask(id int) error {
|
||||||
trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
|
trashSQL, err := database.Prepare("update task set is_deleted='Y',last_modified_at=datetime() where id=?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(trashSQL).Exec(id)
|
_, err = tx.Stmt(trashSQL).Exec(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("doing rollback")
|
log.Println("doing rollback")
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -112,15 +112,15 @@ func TrashTask(id int) error {
|
||||||
func CompleteTask(id int) error {
|
func CompleteTask(id int) error {
|
||||||
stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
|
stmt, err := database.Prepare("update task set is_deleted='Y', finish_date=datetime(),last_modified_at=datetime() where id=?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(stmt).Exec(id)
|
_, err = tx.Stmt(stmt).Exec(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -132,15 +132,15 @@ func CompleteTask(id int) error {
|
||||||
func DeleteAll() error {
|
func DeleteAll() error {
|
||||||
stmt, err := database.Prepare("delete from task where is_deleted='Y'")
|
stmt, err := database.Prepare("delete from task where is_deleted='Y'")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(stmt).Exec()
|
_, err = tx.Stmt(stmt).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("doing rollback")
|
log.Println("doing rollback")
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -152,15 +152,15 @@ func DeleteAll() error {
|
||||||
func RestoreTask(id int) error {
|
func RestoreTask(id int) error {
|
||||||
restoreSQL, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
|
restoreSQL, err := database.Prepare("update task set is_deleted='N',last_modified_at=datetime() where id=?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(restoreSQL).Exec(id)
|
_, err = tx.Stmt(restoreSQL).Exec(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("doing rollback")
|
log.Println("doing rollback")
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -168,19 +168,20 @@ func RestoreTask(id int) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//RestoreTaskFromComplete is used to restore tasks from the Trash
|
|
||||||
|
//RestoreTask is used to restore tasks from the Trash
|
||||||
func RestoreTaskFromComplete(id int) error {
|
func RestoreTaskFromComplete(id int) error {
|
||||||
restoreSQL, err := database.Prepare("update task set is_deleted='N',finish_date=null,last_modified_at=datetime() where id=?")
|
restoreSQL, err := database.Prepare("update task set finish_date=null,last_modified_at=datetime() where id=?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(restoreSQL).Exec(id)
|
_, err = tx.Stmt(restoreSQL).Exec(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("doing rollback")
|
log.Println("doing rollback")
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -192,15 +193,15 @@ func RestoreTaskFromComplete(id int) error {
|
||||||
func DeleteTask(id int) error {
|
func DeleteTask(id int) error {
|
||||||
deleteSQL, err := database.Prepare("delete from task where id = ?")
|
deleteSQL, err := database.Prepare("delete from task where id = ?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(deleteSQL).Exec(id)
|
_, err = tx.Stmt(deleteSQL).Exec(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -212,12 +213,12 @@ func DeleteTask(id int) error {
|
||||||
func AddTask(title, content string) error {
|
func AddTask(title, content string) 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, created_date, last_modified_at) values(?,?,datetime(), datetime())")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
@ -229,19 +230,19 @@ func AddTask(title, content string) error {
|
||||||
func UpdateTask(id int, title string, content string) error {
|
func UpdateTask(id int, title string, content string) error {
|
||||||
SQL, err := database.Prepare("update task set title=?, content=? where id=?")
|
SQL, err := database.Prepare("update task set title=?, content=? where id=?")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
tx, err := database.Begin()
|
tx, err := database.Begin()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
_, err = tx.Stmt(SQL).Exec(title, content, id)
|
_, err = tx.Stmt(SQL).Exec(title, content, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(tx.Commit())
|
log.Println(tx.Commit())
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -258,12 +259,12 @@ func SearchTask(query string) types.Context {
|
||||||
|
|
||||||
rows, err := database.Query(stmt, query, query)
|
rows, err := database.Query(stmt, query, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
|
err := rows.Scan(&TaskID, &TaskTitle, &TaskContent, &TaskCreated)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
|
TaskTitle = strings.Replace(TaskTitle, query, "<span class='highlight'>"+query+"</span>", -1)
|
||||||
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
|
TaskContent = strings.Replace(TaskContent, query, "<span class='highlight'>"+query+"</span>", -1)
|
||||||
|
|
3
main.go
3
main.go
|
@ -5,7 +5,6 @@ package main
|
||||||
* License: MIT
|
* License: MIT
|
||||||
**/
|
**/
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/thewhitetulip/Tasks/views"
|
"github.com/thewhitetulip/Tasks/views"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -27,6 +26,6 @@ func main() {
|
||||||
http.HandleFunc("/search/", views.SearchTaskFunc)
|
http.HandleFunc("/search/", views.SearchTaskFunc)
|
||||||
//http.HandleFunc("/static/", ServeStaticFunc)
|
//http.HandleFunc("/static/", ServeStaticFunc)
|
||||||
http.Handle("/static/", http.FileServer(http.Dir("public")))
|
http.Handle("/static/", http.FileServer(http.Dir("public")))
|
||||||
fmt.Println("running on 8080")
|
log.Println("running server on 8080")
|
||||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package views
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"log"
|
||||||
"github.com/thewhitetulip/Tasks/db"
|
"github.com/thewhitetulip/Tasks/db"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -28,7 +28,7 @@ func PopulateTemplates() {
|
||||||
templatesDir := "./public/templates/"
|
templatesDir := "./public/templates/"
|
||||||
files, err := ioutil.ReadDir(templatesDir)
|
files, err := ioutil.ReadDir(templatesDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error reading template dir")
|
log.Println("Error reading template dir")
|
||||||
}
|
}
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
filename := file.Name()
|
filename := file.Name()
|
||||||
|
@ -38,12 +38,12 @@ func PopulateTemplates() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
templates, err = template.ParseFiles(allFiles...)
|
templates, err = template.ParseFiles(allFiles...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
homeTemplate = templates.Lookup("home.html")
|
homeTemplate = templates.Lookup("home.html")
|
||||||
|
@ -135,7 +135,7 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
task := db.GetTaskByID(id)
|
task := db.GetTaskByID(id)
|
||||||
editTemplate.Execute(w, task)
|
editTemplate.Execute(w, task)
|
||||||
|
@ -151,7 +151,7 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
err = db.CompleteTask(id)
|
err = db.CompleteTask(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -177,7 +177,7 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
id, err := strconv.Atoi(id)
|
id, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
err = db.DeleteTask(id)
|
err = db.DeleteTask(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -199,7 +199,7 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
err = db.TrashTask(id)
|
err = db.TrashTask(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -220,7 +220,7 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
err = db.RestoreTask(id)
|
err = db.RestoreTask(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -241,7 +241,7 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method == "GET" {
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
err = db.RestoreTaskFromComplete(id)
|
err = db.RestoreTaskFromComplete(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -263,7 +263,7 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
id, err := strconv.Atoi(r.Form.Get("id"))
|
id, err := strconv.Atoi(r.Form.Get("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
title := r.Form.Get("title")
|
title := r.Form.Get("title")
|
||||||
content := r.Form.Get("content")
|
content := r.Form.Get("content")
|
||||||
|
|
Loading…
Reference in New Issue