Ejemplo n.º 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)

}
Ejemplo n.º 2
0
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
		if err != nil {
			fmt.Println(err)
		} else {
			task := db.GetTaskByID(id)
			editTemplate.Execute(w, task)
		}
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Ejemplo n.º 3
0
//EditTaskFunc is used to edit tasks, handles "/edit/" URL
func EditTaskFunc(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		id, err := strconv.Atoi(r.URL.Path[len("/edit/"):])
		if err != nil {
			log.Println(err)
			http.Redirect(w, r, "/", http.StatusBadRequest)
		} else {
			task, err := db.GetTaskByID(id)
			categories := db.GetCategories()
			task.Categories = categories

			if err != nil {
				task.Message = "Error fetching Tasks"
			}
			editTemplate.Execute(w, task)
		}
	} else {
		message = "Method not allowed"
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Ejemplo n.º 4
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)
		}

	}
}