#Transaction database/sql

2 messages · Page 1 of 1 (latest)

exotic skiff
#

got no error but maybe i did something wrong i do not really know

#
func (u *User) NextTopic(ctx context.Context, tx *sql.Tx, topic_id string) error {
    next := `
  WITH current_sequence AS (
    SELECT sequence 
    FROM topics 
    WHERE id = $1 
                                                      )
  SELECT *
FROM topics
WHERE sequence > (SELECT sequence FROM current_sequence)
ORDER BY sequence ASC
LIMIT 1;`

    var id string
    if err := tx.QueryRowContext(ctx, next, topic_id).Scan(&id); err != nil {
        return err
    }

    first := "select id from words where topics.id = $1 order by sequence asc limit 1;"

    var word_id string
    if err := tx.QueryRowContext(ctx, first, id).Scan(&word_id); err != nil {
        return err
    }

    insert := "insert into learning(user_id, text_id) values($1, $2)"

    if _, err := tx.ExecContext(ctx, insert, u.ID, word_id); err != nil {
        return err
    }

    return nil
}