From 433c7b4085e03d80dff25f1230977c1650328207 Mon Sep 17 00:00:00 2001 From: zardzul Date: Sat, 14 Mar 2026 18:18:10 +0100 Subject: [PATCH] change database, add queries --- api/queries/albums.sql | 17 ++++++++++++++++- api/queries/artists.sql | 9 +++++++++ api/queries/user_albums.sql | 19 +++++++++++++++++++ api/queries/users.sql | 19 +++++++++++++++++-- db/db.sql | 5 ++++- 5 files changed, 65 insertions(+), 4 deletions(-) diff --git a/api/queries/albums.sql b/api/queries/albums.sql index da59b3e..0cbdba6 100644 --- a/api/queries/albums.sql +++ b/api/queries/albums.sql @@ -3,4 +3,19 @@ SELECT * FROM albums WHERE id = $1; -- name: GetAllAlbums :many -SELECT * FROM albums; \ No newline at end of file +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; \ No newline at end of file diff --git a/api/queries/artists.sql b/api/queries/artists.sql index 589258d..70489a0 100644 --- a/api/queries/artists.sql +++ b/api/queries/artists.sql @@ -7,3 +7,12 @@ 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 || '%'; \ No newline at end of file diff --git a/api/queries/user_albums.sql b/api/queries/user_albums.sql index fe6b506..159756a 100644 --- a/api/queries/user_albums.sql +++ b/api/queries/user_albums.sql @@ -1,4 +1,23 @@ -- 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) diff --git a/api/queries/users.sql b/api/queries/users.sql index 74776b4..bb8a562 100644 --- a/api/queries/users.sql +++ b/api/queries/users.sql @@ -1,9 +1,24 @@ -- 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: CheckUserExistsByEmail :one SELECT id FROM users WHERE user_mail = $1; --- name: CreateUser :one -INSERT INTO users (id, user_name, user_mail, password) VALUES ($1, $2, $3, $4) RETURNING id; +-- name: GetUserByID :one +SELECT id, user_name, user_mail, created_at FROM users WHERE id = $1; + +-- name: LoginUser :one +SELECT id, user_name, user_mail FROM users WHERE user_mail = $1 AND password = $2; + +-- name: UpdateUserSession :exec +UPDATE users SET session_token = $2, session_expiry = $3 WHERE id = $1; + +-- name: logoutUser :exec +-- This is a placeholder for logout functionality, which typically involves token invalidation or session management rather \ No newline at end of file diff --git a/db/db.sql b/db/db.sql index 0cc4390..aa22a38 100644 --- a/db/db.sql +++ b/db/db.sql @@ -3,7 +3,9 @@ CREATE TABLE IF NOT EXISTS users ( user_name VARCHAR(255) NOT NULL, user_mail VARCHAR(255) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + session_token VARCHAR(255), + session_expiry TIMESTAMP ); CREATE TABLE IF NOT EXISTS albums ( @@ -25,6 +27,7 @@ CREATE TABLE IF NOT EXISTS artists ( CREATE TABLE IF NOT EXISTS user_albums ( user_id UUID, 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, is_vinyl BOOLEAN NOT NULL DEFAULT FALSE,