Add initial database setup and user/artist management functionality
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
db "zardzul/music-index/sqlc"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Connect() (*pgxpool.Pool, *db.Queries, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
databaseURL := os.Getenv("DATABASE_URL")
|
||||
if databaseURL == "" {
|
||||
databaseURL = "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
|
||||
}
|
||||
|
||||
pool, err := pgxpool.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
queries := db.New(pool)
|
||||
return pool, queries, nil
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Album struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Artist string `json:"artist"`
|
||||
ReleaseDate pgtype.Date `json:"release_date"`
|
||||
AlbumArtUrl pgtype.Text `json:"album_art_url"`
|
||||
}
|
||||
|
||||
type Artist struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Genre pgtype.Text `json:"genre"`
|
||||
Bio pgtype.Text `json:"bio"`
|
||||
ArtistImageUrl pgtype.Text `json:"artist_image_url"`
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID pgtype.UUID `json:"id"`
|
||||
UserName string `json:"user_name"`
|
||||
UserMail string `json:"user_mail"`
|
||||
Password string `json:"password"`
|
||||
CreatedAt pgtype.Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserAlbum struct {
|
||||
UserID pgtype.UUID `json:"user_id"`
|
||||
AlbumID pgtype.UUID `json:"album_id"`
|
||||
AlbumOwned bool `json:"album_owned"`
|
||||
AlbumWant bool `json:"album_want"`
|
||||
IsVinyl bool `json:"is_vinyl"`
|
||||
IsCd bool `json:"is_cd"`
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type Querier interface {
|
||||
// users.sql
|
||||
GetUsernameByID(ctx context.Context, id pgtype.UUID) (string, error)
|
||||
checkUserExistsByEmail(ctx context.Context, userMail string) (pgtype.UUID, error)
|
||||
}
|
||||
|
||||
var _ Querier = (*Queries)(nil)
|
||||
@@ -1,35 +0,0 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.30.0
|
||||
// source: users.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const getUsernameByID = `-- name: GetUsernameByID :one
|
||||
SELECT user_name FROM users WHERE id = $1
|
||||
`
|
||||
|
||||
// users.sql
|
||||
func (q *Queries) GetUsernameByID(ctx context.Context, id pgtype.UUID) (string, error) {
|
||||
row := q.db.QueryRow(ctx, getUsernameByID, id)
|
||||
var user_name string
|
||||
err := row.Scan(&user_name)
|
||||
return user_name, err
|
||||
}
|
||||
|
||||
const checkUserExistsByEmail = `-- name: checkUserExistsByEmail :one
|
||||
SELECT id FROM users WHERE user_mail = $1
|
||||
`
|
||||
|
||||
func (q *Queries) checkUserExistsByEmail(ctx context.Context, userMail string) (pgtype.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, checkUserExistsByEmail, userMail)
|
||||
var id pgtype.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
Reference in New Issue
Block a user