Exemple #1
0
func TestCreateTask(t *testing.T) {
	tsk := task.Task{
		Name: "New task",
	}
	task.Create(&tsk)
	if tsk.ID == "" {
		t.Errorf("Task should have an unique ID. Got %q", tsk.ID)
	}
}
Exemple #2
0
// createTask get's data from user and returns created task
func createTask(rw http.ResponseWriter, req *http.Request, _ 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 creating new task:", err)
		return

	}

	// Create new task
	task.Create(&tsk)

	// Return to user created task
	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(http.StatusCreated)
	if err = json.NewEncoder(rw).Encode(tsk); err != nil {
		log.Fatal(err)
	}
}