func selectLogFromQuery(db ab.DB, query string, args ...interface{}) ([]*Log, error) { // HOOK: beforeLogSelect() entities := []*Log{} rows, err := db.Query(query, args...) if err != nil { return entities, err } for rows.Next() { e := EmptyLog() if err = rows.Scan(&e.UUID, &e.Type, &e.Message, &e.Created); err != nil { return []*Log{}, err } entities = append(entities, e) } // HOOK: afterLogSelect() return entities, err }
func selectWalkthroughFromQuery(db ab.DB, query string, args ...interface{}) ([]*Walkthrough, error) { // HOOK: beforeWalkthroughSelect() entities := []*Walkthrough{} rows, err := db.Query(query, args...) if err != nil { return entities, err } for rows.Next() { e := EmptyWalkthrough() jsonSteps := "" if err = rows.Scan(&e.Revision, &e.UUID, &e.UID, &e.Name, &e.Description, &e.Severity, &jsonSteps, &e.Updated, &e.Published); err != nil { return []*Walkthrough{}, err } json.Unmarshal([]byte(jsonSteps), &e.Steps) entities = append(entities, e) } // HOOK: afterWalkthroughSelect() return entities, err }
func selectUserFromQuery(db ab.DB, query string, args ...interface{}) ([]*User, error) { // HOOK: beforeUserSelect() entities := []*User{} rows, err := db.Query(query, args...) if err != nil { return entities, err } for rows.Next() { e := EmptyUser() if err = rows.Scan(&e.UUID, &e.Name, &e.Mail, &e.Admin, &e.Created, &e.LastSeen); err != nil { return []*User{}, err } entities = append(entities, e) } // HOOK: afterUserSelect() return entities, err }
func (e *Log) Insert(db ab.DB) error { // HOOK: beforeLogInsert() err := db.QueryRow("INSERT INTO \"log\"(type, message, created) VALUES($1, $2, $3) RETURNING uuid", e.Type, e.Message, e.Created).Scan(&e.UUID) // HOOK: afterLogInsert() return err }
func (e *User) Insert(db ab.DB) error { // HOOK: beforeUserInsert() err := db.QueryRow("INSERT INTO \"user\"(name, mail, admin, created, lastseen) VALUES($1, $2, $3, $4, $5) RETURNING uuid", e.Name, e.Mail, e.Admin, e.Created, e.LastSeen).Scan(&e.UUID) // HOOK: afterUserInsert() return err }
func (e *Walkthrough) Insert(db ab.DB) error { beforeWalkthroughInsert(e) jsonSteps := "" bjsonSteps, _ := json.Marshal(e.Steps) jsonSteps = string(bjsonSteps) err := db.QueryRow("INSERT INTO \"walkthrough\"(uuid, uid, name, description, severity, steps, updated, published) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING revision", e.UUID, e.UID, e.Name, e.Description, e.Severity, jsonSteps, e.Updated, e.Published).Scan(&e.Revision) // HOOK: afterWalkthroughInsert() return err }
func (e *Walkthrough) Delete(db ab.DB) error { res, err := db.Exec("UPDATE walkthrough SET published = false WHERE UUID = $1", e.UUID) if err != nil { return err } aff, err := res.RowsAffected() if err != nil { return err } if aff < 1 { return WalkthroughNotFoundError } return nil }
func (e *User) Update(db ab.DB) error { // HOOK: beforeUserUpdate() result, err := db.Exec("UPDATE \"user\" SET name = $1, mail = $2, admin = $3, created = $4, lastseen = $5 WHERE uuid = $6", e.Name, e.Mail, e.Admin, e.Created, e.LastSeen, e.UUID) if err != nil { return err } aff, err := result.RowsAffected() if err != nil { return err } if aff != 1 { return UserNotFoundError } // HOOK: afterUserUpdate() return nil }
func (e *User) Delete(db ab.DB) error { // HOOK: beforeUserDelete() res, err := db.Exec("DELETE FROM \"user\" WHERE uuid = $1", e.UUID) if err != nil { return err } aff, err := res.RowsAffected() if err != nil { return err } if aff != 1 { return UserNotFoundError } // HOOK: afterUserDelete() return nil }