예제 #1
0
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}
	redirectURL := utils.GetRedirectUrl(r.Referer())
	username := sessions.GetCurrentUserName(r)
	task, err := db.GetTaskByID(username, id)
	categories := db.GetCategories(username)
	task.Categories = categories
	task.Referer = redirectURL

	if err != nil {
		task.Message = "Error fetching Tasks"
	}
	editTemplate.Execute(w, task)

}
예제 #2
0
//TrashTaskFunc is used to populate the trash tasks
func TrashTaskFunc(w http.ResponseWriter, r *http.Request) {
	//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
	redirectURL := utils.GetRedirectUrl(r.Referer())

	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	id, err := strconv.Atoi(r.URL.Path[len("/trash/"):])
	if err != nil {
		log.Println("TrashTaskFunc", err)
		message = "Incorrect command"
		http.Redirect(w, r, redirectURL, http.StatusFound)
	} else {
		username := sessions.GetCurrentUserName(r)
		err = db.TrashTask(username, id)
		if err != nil {
			message = "Error trashing task"
		} else {
			message = "Task trashed"
		}
		http.Redirect(w, r, redirectURL, http.StatusFound)

	}
}
예제 #3
0
//AddCommentFunc will be used
func AddCommentFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		log.Println(err)
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}
	r.ParseForm()
	text := r.Form.Get("commentText")
	id := r.Form.Get("taskID")

	idInt, err := strconv.Atoi(id)

	if (err != nil) || (text == "") {
		log.Println("unable to convert into integer")
		message = "Error adding comment"
	} else {
		username := sessions.GetCurrentUserName(r)
		err = db.AddComments(username, idInt, text)

		if err != nil {
			log.Println("unable to insert into db")
			message = "Comment not added"
		} else {
			message = "Comment added"
		}
	}

	http.Redirect(w, r, "/", http.StatusFound)

}
예제 #4
0
//DeleteTaskFunc is used to delete a task, trash = move to recycle bin, delete = permanent delete
func DeleteTaskFunc(w http.ResponseWriter, r *http.Request) {
	username := sessions.GetCurrentUserName(r)
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	id := r.URL.Path[len("/delete/"):]
	if id == "all" {
		err := db.DeleteAll(username)
		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(username, id)
			if err != nil {
				message = "Error deleting task"
			} else {
				message = "Task deleted"
			}
			http.Redirect(w, r, "/deleted", http.StatusFound)
		}
	}

}
예제 #5
0
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		username := sessions.GetCurrentUserName(r)
		categories := db.GetCategories(username)
		context, err := db.GetTasks(username, "completed", "")
		context.Categories = categories
		if err != nil {
			http.Redirect(w, r, "/completed", http.StatusInternalServerError)
		}
		completedTemplate.Execute(w, context)
	}
}
예제 #6
0
//DeleteCategoryFunc will delete any category
func DeleteCategoryFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	categoryName := r.URL.Path[len("/del-category/"):]
	username := sessions.GetCurrentUserName(r)
	err := db.DeleteCategoryByName(username, categoryName)
	if err != nil {
		message = "error deleting category"
	} else {
		message = "Category " + categoryName + " deleted"
	}

	http.Redirect(w, r, "/", http.StatusFound)

}
예제 #7
0
//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" {
		username := sessions.GetCurrentUserName(r)
		categories := db.GetCategories(username)
		context, err := db.GetTasks(username, "deleted", "")
		context.Categories = categories
		if err != nil {
			http.Redirect(w, r, "/trash", http.StatusInternalServerError)
		}
		if message != "" {
			context.Message = message
			message = ""
		}
		err = deletedTemplate.Execute(w, context)
		if err != nil {
			log.Fatal(err)
		}
	}
}
예제 #8
0
//SearchTaskFunc is used to handle the /search/ url, handles the search function
func SearchTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}
	r.ParseForm()
	query := r.Form.Get("query")

	username := sessions.GetCurrentUserName(r)
	context, err := db.SearchTask(username, query)
	if err != nil {
		log.Println("error fetching search results")
	}

	categories := db.GetCategories(username)
	context.Categories = categories

	searchTemplate.Execute(w, context)

}
예제 #9
0
//AddCategoryFunc used to add new categories to the database
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()
	category := r.Form.Get("category")
	if strings.Trim(category, " ") != "" {
		username := sessions.GetCurrentUserName(r)
		log.Println("adding category")
		err := db.AddCategory(username, category)
		if err != nil {
			message = "Error adding category"
			http.Redirect(w, r, "/", http.StatusBadRequest)
		} else {
			message = "Added category"
			http.Redirect(w, r, "/", http.StatusFound)
		}
	}
}
예제 #10
0
//RestoreTaskFunc is used to restore task from trash, handles "/restore/" URL
func RestoreTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	id, err := strconv.Atoi(r.URL.Path[len("/restore/"):])
	if err != nil {
		log.Println(err)
		http.Redirect(w, r, "/deleted", http.StatusBadRequest)
	} else {
		username := sessions.GetCurrentUserName(r)
		err = db.RestoreTask(username, id)
		if err != nil {
			message = "Restore failed"
		} else {
			message = "Task restored"
		}
		http.Redirect(w, r, "/deleted/", http.StatusFound)
	}

}
예제 #11
0
//CompleteTaskFunc is used to show the complete tasks, handles "/completed/" url
func CompleteTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	redirectURL := utils.GetRedirectUrl(r.Referer())
	id, err := strconv.Atoi(r.URL.Path[len("/complete/"):])
	if err != nil {
		log.Println(err)
	} else {
		username := sessions.GetCurrentUserName(r)
		err = db.CompleteTask(username, id)
		if err != nil {
			message = "Complete task failed"
		} else {
			message = "Task marked complete"
		}
		http.Redirect(w, r, redirectURL, http.StatusFound)
	}

}
예제 #12
0
//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" && sessions.IsLoggedIn(r) {
		category := r.URL.Path[len("/category/"):]
		username := sessions.GetCurrentUserName(r)
		context, err := db.GetTasks(username, "", category)
		categories := db.GetCategories(username)

		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)
	}
}
예제 #13
0
//ShowAllTasksFunc is used to handle the "/" URL which is the default ons
//TODO add http404 error
func ShowAllTasksFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		username := sessions.GetCurrentUserName(r)
		context, err := db.GetTasks(username, "pending", "")
		log.Println(context)
		categories := db.GetCategories(username)
		if err != nil {
			http.Redirect(w, r, "/", http.StatusInternalServerError)
		} else {
			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)
		}
	}
}
예제 #14
0
//UpdateCategoryFunc is used to update a task, handes "/upd-category/" URL
func UpdateCategoryFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}
	var redirectURL string
	r.ParseForm()
	oldName := r.URL.Path[len("/upd-category/"):]
	newName := r.Form.Get("catname")
	username := sessions.GetCurrentUserName(r)
	err := db.UpdateCategoryByName(username, oldName, newName)
	if err != nil {
		message = "error updating category"
		log.Println("not updated category " + oldName)
		redirectURL = "/category/" + oldName
	} else {
		message = "cat " + oldName + " -> " + newName
		redirectURL = "/category/" + newName
	}
	log.Println("redirecting to " + redirectURL)
	http.Redirect(w, r, redirectURL, http.StatusFound)

}
예제 #15
0
//UpdateTaskFunc is used to update a task, handes "/update/" URL
func UpdateTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	r.ParseForm()
	id, err := strconv.Atoi(r.Form.Get("id"))
	if err != nil {
		log.Println(err)
	}
	category := r.Form.Get("category")
	title := r.Form.Get("title")
	content := r.Form.Get("content")
	priority, err := strconv.Atoi(r.Form.Get("priority"))
	if err != nil {
		log.Println(err)
	}
	username := sessions.GetCurrentUserName(r)

	var hidden int
	hideTimeline := r.FormValue("hide")
	if hideTimeline != "" {
		hidden = 1
	} else {
		hidden = 0
	}

	err = db.UpdateTask(id, title, content, category, priority, username, hidden)
	if err != nil {
		message = "Error updating task"
	} else {
		message = "Task updated"
		log.Println(message)
	}
	http.Redirect(w, r, "/", http.StatusFound)
}
예제 #16
0
//DeleteCommentFunc will delete any category
func DeleteCommentFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method != "GET" {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}

	id := r.URL.Path[len("/del-comment/"):]
	commentID, err := strconv.Atoi(id)
	if err != nil {
		http.Redirect(w, r, "/", http.StatusBadRequest)
		return
	}
	username := sessions.GetCurrentUserName(r)

	err = db.DeleteCommentByID(username, commentID)

	if err != nil {
		message = "comment not deleted"
	} else {
		message = "comment deleted"
	}

	http.Redirect(w, r, "/", http.StatusFound)
}
예제 #17
0
//AddTaskFunc is used to handle the addition of new task, "/add" URL
func AddTaskFunc(w http.ResponseWriter, r *http.Request) {
	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
	r.ParseForm()
	file, handler, err := r.FormFile("uploadfile")
	if err != nil && handler != nil {
		//Case executed when file is uploaded and yet an error occurs
		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"
	}
	priorityList := []int{1, 2, 3}
	found := false
	for _, priority := range priorityList {
		if taskPriority == priority {
			found = true
		}
	}
	//If someone gives us incorrect priority number, we give the priority
	//to that task as 1 i.e. Low
	if !found {
		taskPriority = 1
	}
	var hidden int
	hideTimeline := r.FormValue("hide")
	if hideTimeline != "" {
		hidden = 1
	} else {
		hidden = 0
	}
	// dueDate := r.FormValue("dueDate")
	category := r.FormValue("category")
	title := template.HTMLEscapeString(r.Form.Get("title"))
	content := template.HTMLEscapeString(r.Form.Get("content"))
	formToken := template.HTMLEscapeString(r.Form.Get("CSRFToken"))

	cookie, _ := r.Cookie("csrftoken")
	if formToken == cookie.Value {
		username := sessions.GetCurrentUserName(r)
		if handler != nil {
			// this will be executed whenever a file is uploaded
			r.ParseMultipartForm(32 << 20) //defined maximum size of file
			defer file.Close()
			randomFileName := md5.New()
			io.WriteString(randomFileName, strconv.FormatInt(time.Now().Unix(), 10))
			io.WriteString(randomFileName, handler.Filename)
			token := fmt.Sprintf("%x", randomFileName.Sum(nil))
			f, err := os.OpenFile("./files/"+token, os.O_WRONLY|os.O_CREATE, 0666)
			if err != nil {
				log.Println(err)
				return
			}
			defer f.Close()
			io.Copy(f, file)

			if strings.HasSuffix(handler.Filename, ".png") || strings.HasSuffix(handler.Filename, ".jpg") {
				filelink = "<br> <img src='/files/" + token + "'/>"
			} else {
				filelink = "<br> <a href=/files/" + token + ">" + handler.Filename + "</a>"
			}
			content = content + filelink

			fileTruth := db.AddFile(handler.Filename, token, username)
			if fileTruth != nil {
				message = "Error adding filename in db"
				log.Println("error adding task to db")
			}
		}
		//taskTruth := db.AddTask(title, content, category, taskPriority, username, dueDate)
		taskTruth := db.AddTask(title, content, category, taskPriority, username, hidden)
		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.Println("CSRF mismatch")
		message = "Server Error"
		http.Redirect(w, r, "/", http.StatusInternalServerError)
	}

}