Esempio n. 1
0
File: habit.go Progetto: elos/elos
// promptNewHabit provides the input prompts necessary to construct a new habit.
//
// Use this to implement the 'new' subcommand, and for any subcommand which requires
// creating a new habit as part of it's functionality.
//
// It returns a habit and status code. A 0 status code indicates success, any other
// status indicates failure, and the caller should exit immediately. promptNewHabit
// will have taken care of printing the error output.
func (c *HabitCommand) promptNewHabit() (*models.Habit, int) {
	var name string
	var inputErr error

	if name, inputErr = stringInput(c.UI, "Name"); inputErr != nil {
		c.errorf("input error: %s", inputErr)
		return nil, failure
	}

	if name == "" {
		c.printf("Name can't be empty")
		return nil, failure
	}

	id, err := c.DB.ParseID(c.UserID)
	if err != nil {
		c.errorf("error parsing user id: %s", err)
		return nil, failure
	}

	u, err := models.FindUser(c.DB, id)
	if err != nil {
		c.errorf("error finding user: %s", err)
		return nil, failure
	}

	h, err := habit.Create(c.DB, u, name)
	if err != nil {
		c.errorf("error creating habit: %s", err)
		return nil, failure
	}

	return h, success
}
Esempio n. 2
0
func newTestHabit(t *testing.T, db data.DB, u *models.User, name string) *models.Habit {
	h, err := habit.Create(db, u, name)
	if err != nil {
		t.Fatalf("newTestHabit Error: %s", err)
	}
	return h
}