move queries to db folder, change database to better accommodate musicbrainz api data
This commit is contained in:
@@ -8,29 +8,43 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
|
||||
CREATE TABLE IF NOT EXISTS albums (
|
||||
id UUID PRIMARY KEY,
|
||||
musicbrainz_id UUID UNIQUE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
artist VARCHAR(255) NOT NULL,
|
||||
extra_info VARCHAR(255),
|
||||
artist_id VARCHAR(255) NOT NULL,
|
||||
release_date DATE,
|
||||
album_art_url VARCHAR(255)
|
||||
FOREIGN KEY (artist_id) REFERENCES artists(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS artists (
|
||||
id UUID PRIMARY KEY,
|
||||
musicbrainz_id UUID UNIQUE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
genre VARCHAR(255),
|
||||
bio TEXT,
|
||||
artist_image_url VARCHAR(255)
|
||||
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_albums (
|
||||
user_id UUID,
|
||||
user_id UUID PRIMARY KEY,
|
||||
album_id UUID,
|
||||
date_added TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
album_owned BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
album_want BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
album_condition VARCHAR(50) CHECK (album_condition IN ('Mint', 'Near Mint', 'Very Good Plus', 'Very Good', 'Good Plus', 'Good', 'Fair', 'Poor')),
|
||||
is_vinyl BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_cd BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
PRIMARY KEY (user_id, album_id),
|
||||
FOREIGN KEY (user_id) REFERENCES Users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (album_id) REFERENCES Albums(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
id UUID PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS artist_tags (
|
||||
artist_id UUID PRIMARY KEY,
|
||||
tag_id UUID,
|
||||
FOREIGN KEY (artist_id) REFERENCES Artists(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (tag_id) REFERENCES Tags(id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -0,0 +1,21 @@
|
||||
-- albums.sql
|
||||
-- name: GetAlbumByID :one
|
||||
SELECT * FROM albums WHERE id = $1;
|
||||
|
||||
-- name: GetAllAlbums :many
|
||||
SELECT * FROM albums;
|
||||
|
||||
-- name: CreateAlbum :one
|
||||
INSERT INTO albums (id, name, artist, release_date, album_art_url) VALUES ($1, $2, $3, $4, $5) RETURNING id;
|
||||
|
||||
-- name: UpdateAlbum :exec
|
||||
UPDATE albums SET name = $2, artist = $3, release_date = $4, album_art_url = $5 WHERE id = $1;
|
||||
|
||||
-- name: DeleteAlbum :exec
|
||||
DELETE FROM albums WHERE id = $1;
|
||||
|
||||
-- name: SearchAlbums :many
|
||||
SELECT * FROM albums WHERE name ILIKE '%' || $1 || '%' OR artist ILIKE '%' || $1 || '%';
|
||||
|
||||
-- name: GetAlbumsByArtist :many
|
||||
SELECT * FROM albums WHERE artist = $1;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- artists.sql
|
||||
-- name: GetArtistByID :one
|
||||
SELECT * FROM artists WHERE id = $1;
|
||||
|
||||
-- name: GetAllArtists :many
|
||||
SELECT * FROM artists;
|
||||
|
||||
-- name: CreateArtist :one
|
||||
INSERT INTO artists (id, name, genre, bio, artist_image_url) VALUES ($1, $2, $3, $4, $5) RETURNING id;
|
||||
|
||||
-- name: UpdateArtist :exec
|
||||
UPDATE artists SET name = $2, genre = $3, bio = $4, artist_image_url = $5 WHERE id = $1;
|
||||
|
||||
-- name: DeleteArtist :exec
|
||||
DELETE FROM artists WHERE id = $1;
|
||||
|
||||
-- name: SearchArtists :many
|
||||
SELECT * FROM artists WHERE name ILIKE '%' || $1 || '%' OR genre ILIKE '%' || $1 || '%';
|
||||
@@ -0,0 +1,28 @@
|
||||
-- user_albums.sql
|
||||
-- name: GetUserAlbums :many
|
||||
SELECT ua.*, a.name AS album_name, a.artist AS album_artist, a.release_date, a.album_art_url
|
||||
FROM user_albums ua
|
||||
JOIN albums a ON ua.album_id = a.id
|
||||
WHERE ua.user_id = $1;
|
||||
|
||||
-- name: GetUserAlbum :one
|
||||
SELECT ua.*, a.name AS album_name, a.artist AS album_artist, a.release_date, a.album_art_url
|
||||
FROM user_albums ua
|
||||
JOIN albums a ON ua.album_id = a.id
|
||||
WHERE ua.user_id = $1 AND ua.album_id = $2;
|
||||
|
||||
-- name: AddUserAlbum :exec
|
||||
INSERT INTO user_albums (user_id, album_id, album_owned, album_want,
|
||||
is_vinyl, is_cd) VALUES ($1, $2, $3, $4, $5, $6);
|
||||
|
||||
-- name: RemoveUserAlbum :exec
|
||||
DELETE FROM user_albums WHERE user_id = $1 AND album_id = $2;
|
||||
|
||||
-- name: UpdateUserAlbumStatus :exec
|
||||
INSERT INTO user_albums (user_id, album_id, album_owned, album_want,
|
||||
is_vinyl, is_cd) VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (user_id, album_id) DO UPDATE SET
|
||||
album_owned = EXCLUDED.album_owned,
|
||||
album_want = EXCLUDED.album_want,
|
||||
is_vinyl = EXCLUDED.is_vinyl,
|
||||
is_cd = EXCLUDED.is_cd;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- users.sql
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (id, user_name, user_mail, password) VALUES ($1, $2, $3, $4) RETURNING id;
|
||||
|
||||
-- name: UpdateUser :exec
|
||||
UPDATE users SET user_name = $2, user_mail = $3, password = $4 WHERE id = $1;
|
||||
|
||||
-- name: GetUsernameByID :one
|
||||
SELECT user_name FROM users WHERE id = $1;
|
||||
|
||||
-- name: GetUserByID :one
|
||||
SELECT id, user_name, user_mail, created_at FROM users WHERE id = $1;
|
||||
|
||||
-- name: GetUserAuthByEmail :one
|
||||
SELECT id, user_name, user_mail, password
|
||||
FROM users
|
||||
WHERE user_mail = $1;
|
||||
Reference in New Issue
Block a user