/
main.go
85 lines (70 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
)
type Album struct {
ID int
Title string
Artist string
Price float32
}
func main() {
godotenv.Load()
DATABASE_URL := os.Getenv("DATABASE_URL")
conn, err := pgxpool.New(context.Background(), DATABASE_URL)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer conn.Close()
fetchAllAlbums(conn)
fetchAlbum(9, conn)
albums := []Album{
{Title: "Still Bill", Artist: "Bill Withers", Price: 56.99},
{Title: "Black on Both Sides", Artist: "Mos Def", Price: 32.99},
{Title: "Capital Punishment", Artist: "Big Pun", Price: 19.99},
{Title: "The Low End Theory", Artist: "A Tribe Called Quest", Price: 29.99},
{Title: "The Shape of Jazz to Come", Artist: "Ornette Coleman", Price: 39.99},
{Title: "The Blueprint", Artist: "Jay-Z", Price: 24.99},
}
for _, album := range albums {
fmt.Println(addAlbum(album, conn))
}
}
func fetchAllAlbums(conn *pgxpool.Pool) {
var albums []Album
rows, err := conn.Query(context.Background(), "SELECT * FROM album") //.Scan(&album.ID, &album.Title, &album.Artist, &album.Price)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
for rows.Next() {
var alb Album
err := rows.Scan(&alb.ID, &alb.Title, &alb.Artist, &alb.Price)
if err != nil {
fmt.Println(err)
}
albums = append(albums, alb)
}
fmt.Println(albums)
}
func fetchAlbum(id int, conn *pgxpool.Pool) {
var album = Album{}
err := conn.QueryRow(context.Background(), "SELECT * FROM album WHERE id = $1", id).Scan(&album.ID, &album.Title, &album.Artist, &album.Price)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(album)
}
func addAlbum(alb Album, conn *pgxpool.Pool) (int64, error) {
result, err := conn.Exec(context.Background(), "INSERT INTO album (title, artist, price) VALUES ($1, $2, $3)", alb.Title, alb.Artist, alb.Price)
if err != nil {
return 0, fmt.Errorf("unable to execute the query. %v", err)
}
return result.RowsAffected(), nil
}