// promptNewNote prompts the user for the information necessary to create a new // note about the given user. // // Use this to implement the 'note' subcommand, or if a subcommand ever needs // to prompt the user to add a note to a person as a part of it's functionality. // // It returns the new note, which has been saved, and a status code. As always, // 0 indicates success, whereas any other integer indicates failure. When // a failure has occured the first return argument will be nil. The promptNewNote // function handles error message outputting itself. func (c *PeopleCommand) promptNewNote(p *models.Person) (*models.Note, int) { n := models.NewNote() n.SetID(c.DB.NewID()) n.CreatedAt = time.Now() var inputErr error if n.Text, inputErr = stringInput(c.UI, "Content"); inputErr != nil { c.errorf("input err: %s", inputErr) return nil, failure } n.OwnerId = c.UserID n.UpdatedAt = time.Now() if err := c.DB.Save(n); err != nil { c.errorf("error saving note: %s", err) return nil, failure } p.IncludeNote(n) if err := c.DB.Save(p); err != nil { c.errorf("error saving person: %s", err) return nil, failure } return n, success }
func newTestNote(t *testing.T, db data.DB, u *models.User) *models.Note { note := models.NewNote() note.SetID(db.NewID()) note.CreatedAt = time.Now() note.OwnerId = u.ID().String() note.UpdatedAt = time.Now() if err := db.Save(note); err != nil { t.Fatal("newTestNote Error: %s", err) } return note }
func (c *NoteCommand) Run(args []string) int { switch len(args) { case 0: c.Ui.Output(c.Help()) case 1: if c.DB == nil { c.Ui.Error("No database listed") return 1 } if c.Config.UserID == "" { c.Ui.Error("No user id listed") return 1 } switch args[0] { case "new": text, err := c.Ui.Ask("What would you like to make note of?:") if err != nil { return 1 } note := models.NewNote() note.SetID(c.DB.NewID()) note.OwnerId = c.Config.UserID note.CreatedAt = time.Now() note.Text = text note.UpdatedAt = time.Now() err = c.DB.Save(note) if err != nil { c.Ui.Error("Failed to save note") return 1 } c.Ui.Output("Noted") case "list": q := c.DB.Query(models.NoteKind) q.Select(data.AttrMap{ "owner_id": c.Config.UserID, }) iter, err := q.Execute() if err != nil { c.Ui.Error(fmt.Sprintf("Error executing query: %s", err)) return 1 } n := models.NewNote() notes := make([]*models.Note, 0) for iter.Next(n) { notes = append(notes, n) n = models.NewNote() } if err := iter.Close(); err != nil { c.Ui.Error(fmt.Sprintf("Error executing query: %s", err)) return 1 } c.Ui.Output("Here are your notes") for i := range notes { c.Ui.Output(fmt.Sprintf("-----------%d-------------", i)) c.Ui.Output(notes[i].Text) } t, err := c.Ui.Ask("Would you like to [D]elete or [E]dit any? (enter to continue)") if err != nil { return 1 } var i int if t != "" { i, err = intInput(c.Ui, "Which one?") if err != nil { return -1 } } switch t { case "d": fallthrough case "D": err = c.DB.Delete(notes[i]) if err != nil { c.Ui.Error("Error deleting the note") return -1 } case "e": fallthrough case "E": c.Ui.Output(fmt.Sprintf("Current text is: %s", notes[i].Text)) text, err := c.Ui.Ask("What would you like instead?:") if err != nil { return -1 } notes[i].Text = text notes[i].UpdatedAt = time.Now() err = c.DB.Save(notes[i]) if err != nil { c.Ui.Error(fmt.Sprintf("Error saving record: %s", err)) return -1 } } } } return 0 }