119 lines
3 KiB
Go
119 lines
3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"arimelody.me/arimelody.me/api/v1/music"
|
||
|
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
_ "github.com/lib/pq"
|
||
|
"github.com/jmoiron/sqlx"
|
||
|
)
|
||
|
|
||
|
var schema =
|
||
|
`CREATE TABLE IF NOT EXISTS Artists (
|
||
|
id SERIAL primary key,
|
||
|
name text,
|
||
|
website text
|
||
|
);
|
||
|
|
||
|
CREATE TABLE IF NOT EXISTS Albums (
|
||
|
id varchar(64) primary key,
|
||
|
title text not null,
|
||
|
release_date date not null,
|
||
|
artwork text,
|
||
|
buyname text,
|
||
|
buylink text,
|
||
|
description text,
|
||
|
lyrics text
|
||
|
);
|
||
|
|
||
|
CREATE TABLE IF NOT EXISTS AlbumLinks (
|
||
|
id SERIAL primary key,
|
||
|
album varchar(64) references Albums(id),
|
||
|
name text,
|
||
|
url text
|
||
|
);
|
||
|
|
||
|
CREATE TABLE IF NOT EXISTS AlbumCredits (
|
||
|
id SERIAL primary key,
|
||
|
album varchar(64) references Albums(id),
|
||
|
artist int references Artists(id),
|
||
|
role text
|
||
|
);`
|
||
|
|
||
|
func PushArtist(db *sqlx.DB, artist music.Artist) {
|
||
|
query := "SELECT count(*) FROM Artists WHERE name=$1"
|
||
|
var count int
|
||
|
err := db.Get(&count, query, artist.Name)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error while pushing artist [%s] to the database: %v\n", artist.Name, err)
|
||
|
}
|
||
|
|
||
|
query = "INSERT INTO artists (name, website) VALUES ($1, $2)"
|
||
|
if count != 0 {
|
||
|
query = "UPDATE artists SET website=$2 WHERE name=$1"
|
||
|
}
|
||
|
|
||
|
fmt.Printf("saving artist [%s] to the database...", artist.Name)
|
||
|
_, err = db.Exec(query,
|
||
|
&artist.Name,
|
||
|
&artist.Website,
|
||
|
)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error while pushing artist [%s] to the database: %v\n", artist.Name, err)
|
||
|
}
|
||
|
fmt.Printf("done!\n")
|
||
|
|
||
|
// defer db.Close()
|
||
|
}
|
||
|
|
||
|
func PushAlbum(db *sqlx.DB, album music.Album) {
|
||
|
query := "SELECT count(*) FROM Albums WHERE id=$1"
|
||
|
var count int
|
||
|
err := db.Get(&count, query, album.Id)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err)
|
||
|
}
|
||
|
|
||
|
query = "INSERT INTO Albums (id, title, release_date, artwork, buyname, buylink, description, lyrics) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)"
|
||
|
if count != 0 {
|
||
|
query = "UPDATE Albums SET title=$2, release_date=$3, artwork=$4, buyname=$5, buylink=$6, description=$7, lyrics=$8 WHERE id=$1"
|
||
|
}
|
||
|
|
||
|
fmt.Printf("saving album [%s] to the database...", album.Id)
|
||
|
_, err = db.Exec(query,
|
||
|
&album.Id,
|
||
|
&album.Title,
|
||
|
album.ReleaseDate.Format("2-Jan-2006"),
|
||
|
&album.Artwork,
|
||
|
&album.Buyname,
|
||
|
&album.Buylink,
|
||
|
&album.Description,
|
||
|
&album.Lyrics,
|
||
|
)
|
||
|
if err != nil {
|
||
|
fmt.Printf("error while pushing album [%s] to the database: %v\n", album.Id, err)
|
||
|
}
|
||
|
fmt.Printf("done!\n")
|
||
|
|
||
|
// defer db.Close()
|
||
|
}
|
||
|
|
||
|
func InitDatabase() *sqlx.DB {
|
||
|
db, err := sqlx.Connect("postgres", "user=arimimi dbname=arimelody password=fuckingpassword sslmode=disable")
|
||
|
if err != nil {
|
||
|
fmt.Fprintf(os.Stderr, "unable to create database connection pool: %v\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
db.SetConnMaxLifetime(time.Minute * 3)
|
||
|
db.SetMaxOpenConns(10)
|
||
|
db.SetMaxIdleConns(10)
|
||
|
|
||
|
db.MustExec(schema)
|
||
|
|
||
|
return db
|
||
|
}
|