Better error handling in views

This commit is contained in:
Suraj 2016-02-01 19:15:04 +05:30
parent 534cb71064
commit a64c630767
6 changed files with 60 additions and 24 deletions

View File

@ -2,12 +2,13 @@ package db
import (
"database/sql"
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
md "github.com/shurcooL/github_flavored_markdown"
"github.com/thewhitetulip/Tasks/types"
"log"
"strings"
"time"
_ "github.com/mattn/go-sqlite3" //we want to use sqlite natively
md "github.com/shurcooL/github_flavored_markdown"
"github.com/thewhitetulip/Tasks/types"
)
var database Database
@ -22,6 +23,7 @@ func (db Database) begin() (tx *sql.Tx) {
tx, err := db.db.Begin()
if err != nil {
log.Println(err)
return nil
}
return tx
}
@ -30,6 +32,7 @@ func (db Database) prepare(q string) (stmt *sql.Stmt) {
stmt, err := db.db.Prepare(q)
if err != nil {
log.Println(err)
return nil
}
return stmt
}
@ -38,6 +41,7 @@ func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) {
rows, err := db.db.Query(q, args...)
if err != nil {
log.Println(err)
return nil
}
return rows
}
@ -45,7 +49,7 @@ func (db Database) query(q string, args ...interface{}) (rows *sql.Rows) {
func init() {
database.db, err = sql.Open("sqlite3", "./tasks.db")
if err != nil {
log.Println(err)
log.Fatal(err)
}
}
@ -56,7 +60,7 @@ func Close() {
//GetTasks retrieves all the tasks depending on the
//status pending or trashed or completed
func GetTasks(status string) types.Context {
func GetTasks(status string) (types.Context, error) {
var task []types.Task
var context types.Context
var TaskID int
@ -89,11 +93,11 @@ func GetTasks(status string) types.Context {
task = append(task, a)
}
context = types.Context{Tasks: task, Navigation: status}
return context
return context, nil
}
//GetTaskByID function gets the tasks from the ID passed to the function, used to populate EditTask
func GetTaskByID(id int) types.Context {
func GetTaskByID(id int) (types.Context, error) {
var tasks []types.Task
var task types.Task
@ -110,7 +114,7 @@ func GetTaskByID(id int) types.Context {
}
tasks = append(tasks, task)
context := types.Context{Tasks: tasks, Navigation: "edit"}
return context
return context, nil
}
//TrashTask is used to delete the task

View File

@ -5,10 +5,11 @@ package main
* License: MIT
**/
import (
"github.com/thewhitetulip/Tasks/config"
"github.com/thewhitetulip/Tasks/views"
"log"
"net/http"
"github.com/thewhitetulip/Tasks/config"
"github.com/thewhitetulip/Tasks/views"
)
func main() {

View File

@ -3,7 +3,6 @@ package views
import (
"crypto/md5"
"fmt"
"github.com/thewhitetulip/Tasks/db"
"io"
"log"
"net/http"
@ -11,12 +10,13 @@ import (
"strconv"
"text/template"
"time"
"github.com/thewhitetulip/Tasks/db"
)
// UploadedFileHandler is used to handle the uploaded file related requests
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
log.Println("into the handler")
token := r.URL.Path[len("/files/"):]
//file, err := db.GetFileName(token)
@ -34,11 +34,15 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("uploadfile")
if err != nil {
log.Println(err)
message = "Error uploading file"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
taskPriority, priorityErr := strconv.Atoi(r.FormValue("priority"))
if priorityErr != nil {
log.Print(priorityErr)
message = "Bad task priority"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
priorityList := []int{1, 2, 3}
found := false
@ -88,13 +92,16 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
if taskTruth != nil {
message = "Error adding task"
log.Println("error adding task to db")
http.Redirect(w, r, "/", http.StatusInternalServerError)
} else {
message = "Task added"
log.Println("added task to db")
}
http.Redirect(w, r, "/", http.StatusFound)
} else {
log.Fatal("CSRF mismatch")
log.Println("CSRF mismatch")
message = "Server Error"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
} else {

View File

@ -1,10 +1,11 @@
package views
import (
"github.com/thewhitetulip/Tasks/db"
"log"
"net/http"
"strconv"
"github.com/thewhitetulip/Tasks/db"
)
//TrashTaskFunc is used to populate the trash tasks
@ -13,6 +14,7 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/trash", http.StatusBadRequest)
} else {
err = db.TrashTask(id)
if err != nil {
@ -20,7 +22,7 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
} else {
message = "Task trashed"
}
http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/trash", http.StatusFound)
}
} else {
message = "Method not allowed"
@ -34,6 +36,7 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/deleted", http.StatusBadRequest)
} else {
err = db.RestoreTask(id)
if err != nil {
@ -55,8 +58,12 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
} else {
task := db.GetTaskByID(id)
task, err := db.GetTaskByID(id)
if err != nil {
task.Message = "Error fetching Tasks"
}
editTemplate.Execute(w, task)
}
} else {
@ -70,12 +77,17 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
id := r.URL.Path[len("/delete/"):]
if id == "all" {
db.DeleteAll()
err := db.DeleteAll()
if err != nil {
message = "Error deleting tasks"
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
http.Redirect(w, r, "/", http.StatusFound)
} else {
id, err := strconv.Atoi(id)
if err != nil {
log.Println(err)
http.Redirect(w, r, "/", http.StatusBadRequest)
} else {
err = db.DeleteTask(id)
if err != nil {
@ -98,6 +110,7 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
if err != nil {
log.Println(err)
http.Redirect(w, r, "/completed", http.StatusBadRequest)
} else {
err = db.RestoreTaskFromComplete(id)
if err != nil {
@ -105,10 +118,10 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
} else {
message = "Task restored"
}
http.Redirect(w, r, "/pending/", http.StatusFound)
http.Redirect(w, r, "/completed", http.StatusFound)
}
} else {
message = "Method not allowed"
http.Redirect(w, r, "/", http.StatusFound)
http.Redirect(w, r, "/completed", http.StatusFound)
}
}

View File

@ -1,7 +1,6 @@
package views
import (
"github.com/thewhitetulip/Tasks/db"
"io/ioutil"
"log"
"net/http"
@ -9,6 +8,8 @@ import (
"strconv"
"strings"
"text/template"
"github.com/thewhitetulip/Tasks/db"
)
//PopulateTemplates is used to parse all templates present in

View File

@ -2,12 +2,13 @@ package views
import (
"bufio"
"github.com/thewhitetulip/Tasks/db"
"net/http"
"os"
"strings"
"text/template"
"time"
"github.com/thewhitetulip/Tasks/db"
)
var homeTemplate *template.Template
@ -23,7 +24,10 @@ var err error
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("pending") //true when you want non deleted notes
context, err := db.GetTasks("pending")
if err != nil {
http.Redirect(w, r, "/", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
}
@ -42,7 +46,10 @@ func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
//ShowTrashTaskFunc is used to handle the "/trash" URL which is used to show the deleted tasks
func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("deleted") //false when you want deleted notes
context, err := db.GetTasks("deleted")
if err != nil {
http.Redirect(w, r, "/trash", http.StatusInternalServerError)
}
if message != "" {
context.Message = message
message = ""
@ -57,7 +64,10 @@ func ShowTrashTaskFunc(w http.ResponseWriter, r *http.Request) {
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
context := db.GetTasks("completed") //false when you want finished notes
context, err := db.GetTasks("completed")
if err != nil {
http.Redirect(w, r, "/completed", http.StatusInternalServerError)
}
completedTemplate.Execute(w, context)
} else {
message = "Method not allowed"