Example #1
0
//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
		completedTemplate.Execute(w, context)
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Example #2
0
//ShowCompleteTasksFunc is used to populate the "/completed/" URL
func ShowCompleteTasksFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		context, err := db.GetTasks("completed", "")
		if err != nil {
			http.Redirect(w, r, "/completed", http.StatusInternalServerError)
		}
		completedTemplate.Execute(w, context)
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Example #3
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)
	}
}
Example #4
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" {
		context := db.GetTasks("deleted") //false when you want deleted notes
		if message != "" {
			context.Message = message
			message = ""
		}
		deletedTemplate.Execute(w, context)
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Example #5
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" {
		context := db.GetTasks("pending") //true when you want non deleted notes
		if message != "" {
			context.Message = message
		}
		homeTemplate.Execute(w, context)
		message = ""
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Example #6
0
//GetDeletedTaskFuncAPI will get the deleted tasks for the user
func GetDeletedTaskFuncAPI(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		var err error
		var message string
		var tasks types.Tasks
		var status types.Status

		token := r.Header["Token"][0]

		w.Header().Set("Content-Type", "application/json; charset=UTF-8")

		IsTokenValid, username := ValidateToken(token)
		//When the token is not valid show the default error JSON document
		if !IsTokenValid {
			status = types.Status{StatusCode: http.StatusInternalServerError, Message: message}
			w.WriteHeader(http.StatusInternalServerError)
			err = json.NewEncoder(w).Encode(status)

			if err != nil {
				panic(err)
			}
			return
		}

		log.Println("token is valid " + username + " is logged in")

		//this is when we get a request for all the deleted tasks for that user
		context, err := db.GetTasks(username, "deleted", "")
		if err != nil {
			message = "GetTasksFuncAPI: api.go: Server error"
			log.Println(message)
			status = types.Status{StatusCode: http.StatusInternalServerError, Message: message}
			w.WriteHeader(http.StatusInternalServerError)
			err = json.NewEncoder(w).Encode(tasks)

			if err != nil {
				panic(err)
			}
			return
		}

		tasks = context.Tasks
		w.WriteHeader(http.StatusOK)
		err = json.NewEncoder(w).Encode(tasks)

		if err != nil {
			panic(err)
		}
		return
	}
}
Example #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" {
		context, err := db.GetTasks("deleted", "")
		if err != nil {
			http.Redirect(w, r, "/trash", http.StatusInternalServerError)
		}
		if message != "" {
			context.Message = message
			message = ""
		}
		deletedTemplate.Execute(w, context)
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Example #8
0
File: views.go Project: gotej/Tasks
//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" {
		context := db.GetTasks("pending") //true when you want non deleted notes
		if message != "" {
			context.Message = message
		}
		context.CSRFToken = "abcd"
		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)
	}
}
Example #9
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)
		}
	}
}
Example #10
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" {
		context, err := db.GetTasks("pending", "")
		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)
	}
}
Example #11
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)
	}
}
Example #12
0
//GetTasksFuncAPI fetches tasks depending on the request, the authorization will be taken care by our middleare
//in this function we will return all the tasks to the user or tasks per category
//GET /api/get-tasks/
func GetTasksFuncAPI(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		var strTaskID string
		var err error
		var message string
		var task types.Task
		var tasks types.Tasks
		var status types.Status

		token := r.Header["Token"][0]

		w.Header().Set("Content-Type", "application/json; charset=UTF-8")

		IsTokenValid, username := ValidateToken(token)
		//When the token is not valid show the default error JSON document
		if !IsTokenValid {
			status = types.Status{StatusCode: http.StatusInternalServerError, Message: message}
			w.WriteHeader(http.StatusInternalServerError)
			err = json.NewEncoder(w).Encode(status)

			if err != nil {
				panic(err)
			}
			return
		}

		log.Println("token is valid " + username + " is logged in")

		strTaskID = r.URL.Path[len("/api/get-task/"):]
		//this is when we get a request for all the tasks for that user
		if strTaskID == "" {
			context, err := db.GetTasks(username, "pending", "")
			if err != nil {
				message = "GetTasksFuncAPI: api.go: Server error"
				log.Println(message)
				status = types.Status{StatusCode: http.StatusInternalServerError, Message: message}
				w.WriteHeader(http.StatusInternalServerError)
				err = json.NewEncoder(w).Encode(tasks)

				if err != nil {
					panic(err)
				}
				return
			}

			tasks = context.Tasks
			w.WriteHeader(http.StatusOK)
			err = json.NewEncoder(w).Encode(tasks)

			if err != nil {
				panic(err)
			}
			return
		}
		//this is when we get a request for a particular task

		taskID, err := strconv.Atoi(strTaskID)
		if err != nil {
			message = "GetTasksFuncAPI: api.go: Invalid taskID " + strTaskID
			log.Println(message)

			status = types.Status{StatusCode: http.StatusInternalServerError, Message: message}
			w.WriteHeader(http.StatusInternalServerError)
			err = json.NewEncoder(w).Encode(status)

			if err != nil {
				panic(err)
			}
			return
		}
		ctx, err := db.GetTaskByID(username, taskID)
		task = ctx.Tasks[0]

		w.WriteHeader(http.StatusOK)

		err = json.NewEncoder(w).Encode(task)
		if err != nil {
			panic(err)
		}

	}
}