package dbi import ( "context" "database/sql" "fmt" ) type User struct { APIKey string `json:"api_key"` FirstName string `json:"first_name"` LastName string `json:"last_name"` Password string `json:"password"` Status string `json:"status"` Username string `json:"username"` } func CreateUser(conn *sql.Conn, user *User) error { if user.APIKey == "" { user.APIKey = GenUUID() } _, err := conn.ExecContext(context.Background(), `INSERT INTO users (APIKey, FirstName, LastName, Password, Status, Username) VALUES (?, ?, ?, ?, ?, ?)`, user.APIKey, user.FirstName, user.LastName, user.Password, user.Status, user.Username, ) return err } func GetUser(conn *sql.Conn, username string) (User, error) { row := conn.QueryRowContext(context.Background(), `SELECT APIKey, FirstName, LastName, Password, Status, Username FROM users WHERE Username = ?`, username, ) var u User err := row.Scan(&u.APIKey, &u.FirstName, &u.LastName, &u.Password, &u.Status, &u.Username) if err == sql.ErrNoRows { return User{}, fmt.Errorf("user %q not found", username) } return u, err } func UpdateUser(conn *sql.Conn, user *User) error { _, err := conn.ExecContext(context.Background(), `UPDATE users SET APIKey = ?, FirstName = ?, LastName = ?, Password = ?, Status = ? WHERE Username = ?`, user.APIKey, user.FirstName, user.LastName, user.Password, user.Status, user.Username, ) return err } func DeleteUser(conn *sql.Conn, username string) error { _, err := conn.ExecContext(context.Background(), `DELETE FROM users WHERE Username = ?`, username, ) return err }