code refactoring
This commit is contained in:
parent
b1c88a5bc6
commit
f34db50464
|
@ -22,7 +22,10 @@ import (
|
||||||
|
|
||||||
// UploadedFileHandler is used to handle the uploaded file related requests
|
// UploadedFileHandler is used to handle the uploaded file related requests
|
||||||
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
|
func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
token := r.URL.Path[len("/files/"):]
|
token := r.URL.Path[len("/files/"):]
|
||||||
|
|
||||||
//file, err := db.GetFileName(token)
|
//file, err := db.GetFileName(token)
|
||||||
|
@ -30,12 +33,15 @@ func UploadedFileHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println("serving file ./files/" + token)
|
log.Println("serving file ./files/" + token)
|
||||||
http.ServeFile(w, r, "./files/"+token)
|
http.ServeFile(w, r, "./files/"+token)
|
||||||
//}
|
//}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
//AddTaskFunc is used to handle the addition of new task, "/add" URL
|
||||||
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" { // Will work only for POST requests, will redirect to home
|
if r.Method != "POST" { // Will work only for POST requests, will redirect to home
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
var filelink string // will store the html when we have files to be uploaded, appened to the note content
|
var filelink string // will store the html when we have files to be uploaded, appened to the note content
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
file, handler, err := r.FormFile("uploadfile")
|
file, handler, err := r.FormFile("uploadfile")
|
||||||
|
@ -126,12 +132,15 @@ func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddCategoryFunc used to add new categories to the database
|
//AddCategoryFunc used to add new categories to the database
|
||||||
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
func AddCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" { // We respond only to POST requests, redirect to home for others
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
category := r.Form.Get("category")
|
category := r.Form.Get("category")
|
||||||
if strings.Trim(category, " ") != "" {
|
if strings.Trim(category, " ") != "" {
|
||||||
|
@ -160,7 +169,7 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
http.Redirect(w, r, "/", http.StatusBadRequest)
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
} else {
|
}
|
||||||
redirectURL := utils.GetRedirectUrl(r.Referer())
|
redirectURL := utils.GetRedirectUrl(r.Referer())
|
||||||
username := sessions.GetCurrentUserName(r)
|
username := sessions.GetCurrentUserName(r)
|
||||||
task, err := db.GetTaskByID(username, id)
|
task, err := db.GetTaskByID(username, id)
|
||||||
|
@ -172,7 +181,7 @@ func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
task.Message = "Error fetching Tasks"
|
task.Message = "Error fetching Tasks"
|
||||||
}
|
}
|
||||||
editTemplate.Execute(w, task)
|
editTemplate.Execute(w, task)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//AddCommentFunc will be used
|
//AddCommentFunc will be used
|
||||||
|
|
|
@ -19,7 +19,12 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
//for best UX we want the user to be returned to the page making
|
//for best UX we want the user to be returned to the page making
|
||||||
//the delete transaction, we use the r.Referer() function to get the link
|
//the delete transaction, we use the r.Referer() function to get the link
|
||||||
redirectURL := utils.GetRedirectUrl(r.Referer())
|
redirectURL := utils.GetRedirectUrl(r.Referer())
|
||||||
if r.Method == "GET" {
|
|
||||||
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("TrashTaskFunc", err)
|
log.Println("TrashTaskFunc", err)
|
||||||
|
@ -34,13 +39,17 @@ func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
message = "Task trashed"
|
message = "Task trashed"
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, redirectURL, http.StatusFound)
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
|
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
|
||||||
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -55,13 +64,17 @@ func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
http.Redirect(w, r, "/deleted/", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
|
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
|
||||||
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
username := sessions.GetCurrentUserName(r)
|
username := sessions.GetCurrentUserName(r)
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id := r.URL.Path[len("/delete/"):]
|
id := r.URL.Path[len("/delete/"):]
|
||||||
if id == "all" {
|
if id == "all" {
|
||||||
err := db.DeleteAll(username)
|
err := db.DeleteAll(username)
|
||||||
|
@ -85,12 +98,16 @@ func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Redirect(w, r, "/deleted", http.StatusFound)
|
http.Redirect(w, r, "/deleted", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//RestoreFromCompleteFunc restores the task from complete to pending
|
//RestoreFromCompleteFunc restores the task from complete to pending
|
||||||
func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
|
func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/incomplete/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
|
@ -105,12 +122,16 @@ func RestoreFromCompleteFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/completed", http.StatusFound)
|
http.Redirect(w, r, "/completed", http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DeleteCategoryFunc will delete any category
|
//DeleteCategoryFunc will delete any category
|
||||||
func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
categoryName := r.URL.Path[len("/del-category/"):]
|
categoryName := r.URL.Path[len("/del-category/"):]
|
||||||
username := sessions.GetCurrentUserName(r)
|
username := sessions.GetCurrentUserName(r)
|
||||||
err := db.DeleteCategoryByName(username, categoryName)
|
err := db.DeleteCategoryByName(username, categoryName)
|
||||||
|
@ -121,12 +142,16 @@ func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DeleteCommentFunc will delete any category
|
//DeleteCommentFunc will delete any category
|
||||||
func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
|
func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
id := r.URL.Path[len("/del-comment/"):]
|
id := r.URL.Path[len("/del-comment/"):]
|
||||||
commentID, err := strconv.Atoi(id)
|
commentID, err := strconv.Atoi(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -144,5 +169,4 @@ func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,11 @@ func PopulateTemplates() {
|
||||||
|
|
||||||
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
|
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
|
||||||
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != "GET" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
redirectURL := utils.GetRedirectUrl(r.Referer())
|
redirectURL := utils.GetRedirectUrl(r.Referer())
|
||||||
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
|
id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -71,12 +75,15 @@ func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, redirectURL, http.StatusFound)
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//SearchTaskFunc is used to handle the /search/ url, handles the search function
|
//SearchTaskFunc is used to handle the /search/ url, handles the search function
|
||||||
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method != "POST" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
query := r.Form.Get("query")
|
query := r.Form.Get("query")
|
||||||
|
|
||||||
|
@ -90,12 +97,16 @@ func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
context.Categories = categories
|
context.Categories = categories
|
||||||
|
|
||||||
searchTemplate.Execute(w, context)
|
searchTemplate.Execute(w, context)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//UpdateTaskFunc is used to update a task, handes "/update/" URL
|
//UpdateTaskFunc is used to update a task, handes "/update/" URL
|
||||||
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method != "POST" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -126,12 +137,14 @@ func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Println(message)
|
log.Println(message)
|
||||||
}
|
}
|
||||||
http.Redirect(w, r, "/", http.StatusFound)
|
http.Redirect(w, r, "/", http.StatusFound)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL
|
//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL
|
||||||
func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method != "POST" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
var redirectURL string
|
var redirectURL string
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
oldName := r.URL.Path[len("/upd-category/"):]
|
oldName := r.URL.Path[len("/upd-category/"):]
|
||||||
|
@ -148,12 +161,15 @@ func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
log.Println("redirecting to " + redirectURL)
|
log.Println("redirecting to " + redirectURL)
|
||||||
http.Redirect(w, r, redirectURL, http.StatusFound)
|
http.Redirect(w, r, redirectURL, http.StatusFound)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//SignUpFunc will enable new users to sign up to our service
|
//SignUpFunc will enable new users to sign up to our service
|
||||||
func SignUpFunc(w http.ResponseWriter, r *http.Request) {
|
func SignUpFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method != "POST" {
|
||||||
|
http.Redirect(w, r, "/", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
username := r.Form.Get("username")
|
username := r.Form.Get("username")
|
||||||
|
@ -168,5 +184,4 @@ func SignUpFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
} else {
|
} else {
|
||||||
http.Redirect(w, r, "/login/", 302)
|
http.Redirect(w, r, "/login/", 302)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,5 +60,7 @@ func LoginFunc(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
log.Print("Invalid user " + username)
|
log.Print("Invalid user " + username)
|
||||||
loginTemplate.Execute(w, nil)
|
loginTemplate.Execute(w, nil)
|
||||||
|
default:
|
||||||
|
http.Redirect(w, r, "/login/", http.StatusUnauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue