Ejemplo n.º 1
0
// updateTask update given task
func updateTask(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
	var err error
	tsk := task.Task{}

	// Get task data from user
	err = json.NewDecoder(req.Body).Decode(&tsk)
	if err != nil {
		rw.WriteHeader(http.StatusBadRequest)
		fmt.Println("There was a problem with updating task:", err)
		return
	}

	// Update task
	t := task.Update(p.ByName("id"), &tsk)

	// If task doesn't exists
	if t == nil {
		rw.WriteHeader(http.StatusBadRequest)
		if _, err = rw.Write([]byte("Task was not updated")); err != nil {
			log.Fatal(err)
		}
		return
	}

	// Return to user created task
	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(http.StatusOK)
	if err = json.NewEncoder(rw).Encode(tsk); err != nil {
		log.Fatal(err)
	}
}
Ejemplo n.º 2
0
func TestUpdateTask(t *testing.T) {
	tsk := task.Task{
		Name: "Task updated",
		Done: true,
	}
	updated := task.Update("1", &tsk)
	if updated.Done != true || updated.Name != "Task updated" {
		t.Error("The task was not updated correctly")
	}

	tsk = task.Task{
		Name: "I'm not exitsing",
		Done: true,
	}
	updated = task.Update("1000", &tsk)
	if updated != nil {
		t.Error("Updated non existing task")
	}
}