// ListChanges lists all the change lists in the database. func (db *DB) ListChanges(by string) ([]*sitedb.Changes, error) { q := func() (*sql.Rows, error) { base := `select id, owner, title, last_modify from changes %s order by last_modify desc` if strings.HasPrefix(by, "!") { user := by[1:] return db.db.Q(fmt.Sprintf(base, "where owner != ?"), user) } if by != "" { return db.db.Q(fmt.Sprintf(base, "where owner = ?"), by) } return db.db.Q(fmt.Sprintf(base, "")) } rows, err := q() if err != nil { return nil, err } var ret []*sitedb.Changes for rows.Next() { c := new(sitedb.Changes) t := int64(0) if err := rows.Scan(&c.ID, &c.Owner, &c.Title, &t); err != nil { return nil, err } c.LastModify = time.Unix(t, 0) ret = append(ret, c) } if err := rows.Close(); err != nil { return nil, err } return ret, nil }
// NewChanges creates a new change list and back fills the id. func (db *DB) NewChanges(c *sitedb.Changes) error { now := time.Now() res, err := db.db.X(`insert into changes (owner, title, last_modify) values (?, ?, ?)`, c.Owner, c.Title, now.Unix(), ) if err != nil { return err } id, err := res.LastInsertId() if err != nil { return err } c.LastModify = now c.ID = id return nil }