feat: update database queries for multiple folder management
- Replace single folder queries with multiple folder operations - Add AddUserEbookFolder, GetUserEbookFolders, DeleteUserEbookFolder queries - Regenerate Go code with sqlc - Update models to include UserEbookFolders struct
This commit is contained in:
@@ -47,6 +47,13 @@ type ReadingProgress struct {
|
|||||||
LastReadAt pgtype.Timestamptz `db:"last_read_at" json:"last_read_at"`
|
LastReadAt pgtype.Timestamptz `db:"last_read_at" json:"last_read_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type UserEbookFolders struct {
|
||||||
|
ID pgtype.UUID `db:"id" json:"id"`
|
||||||
|
UserID pgtype.UUID `db:"user_id" json:"user_id"`
|
||||||
|
FolderPath string `db:"folder_path" json:"folder_path"`
|
||||||
|
CreatedAt pgtype.Timestamptz `db:"created_at" json:"created_at"`
|
||||||
|
}
|
||||||
|
|
||||||
type Users struct {
|
type Users struct {
|
||||||
ID pgtype.UUID `db:"id" json:"id"`
|
ID pgtype.UUID `db:"id" json:"id"`
|
||||||
Email string `db:"email" json:"email"`
|
Email string `db:"email" json:"email"`
|
||||||
|
|||||||
@@ -11,13 +11,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Querier interface {
|
type Querier interface {
|
||||||
|
AddUserEbookFolder(ctx context.Context, arg AddUserEbookFolderParams) (UserEbookFolders, error)
|
||||||
CreateEbook(ctx context.Context, arg CreateEbookParams) (Ebooks, error)
|
CreateEbook(ctx context.Context, arg CreateEbookParams) (Ebooks, error)
|
||||||
CreateEbookRating(ctx context.Context, arg CreateEbookRatingParams) (EbookRatings, error)
|
CreateEbookRating(ctx context.Context, arg CreateEbookRatingParams) (EbookRatings, error)
|
||||||
CreateUser(ctx context.Context, arg CreateUserParams) (Users, error)
|
CreateUser(ctx context.Context, arg CreateUserParams) (Users, error)
|
||||||
DeleteEbook(ctx context.Context, id pgtype.UUID) error
|
DeleteEbook(ctx context.Context, id pgtype.UUID) error
|
||||||
DeleteEbookRating(ctx context.Context, arg DeleteEbookRatingParams) error
|
DeleteEbookRating(ctx context.Context, arg DeleteEbookRatingParams) error
|
||||||
DeleteReadingProgress(ctx context.Context, arg DeleteReadingProgressParams) error
|
DeleteReadingProgress(ctx context.Context, arg DeleteReadingProgressParams) error
|
||||||
|
DeleteUserEbookFolder(ctx context.Context, arg DeleteUserEbookFolderParams) error
|
||||||
GetEbook(ctx context.Context, id pgtype.UUID) (Ebooks, error)
|
GetEbook(ctx context.Context, id pgtype.UUID) (Ebooks, error)
|
||||||
|
GetEbookByFilePath(ctx context.Context, filePath string) (Ebooks, error)
|
||||||
GetEbookRating(ctx context.Context, arg GetEbookRatingParams) (EbookRatings, error)
|
GetEbookRating(ctx context.Context, arg GetEbookRatingParams) (EbookRatings, error)
|
||||||
GetEbookRatings(ctx context.Context, ebookID pgtype.UUID) ([]GetEbookRatingsRow, error)
|
GetEbookRatings(ctx context.Context, ebookID pgtype.UUID) ([]GetEbookRatingsRow, error)
|
||||||
GetReadingProgress(ctx context.Context, arg GetReadingProgressParams) (ReadingProgress, error)
|
GetReadingProgress(ctx context.Context, arg GetReadingProgressParams) (ReadingProgress, error)
|
||||||
@@ -25,6 +28,7 @@ type Querier interface {
|
|||||||
GetUserByEmail(ctx context.Context, email string) (Users, error)
|
GetUserByEmail(ctx context.Context, email string) (Users, error)
|
||||||
GetUserByEmailOrUsername(ctx context.Context, email string) (Users, error)
|
GetUserByEmailOrUsername(ctx context.Context, email string) (Users, error)
|
||||||
GetUserByUsername(ctx context.Context, username string) (Users, error)
|
GetUserByUsername(ctx context.Context, username string) (Users, error)
|
||||||
|
GetUserEbookFolders(ctx context.Context, userID pgtype.UUID) ([]UserEbookFolders, error)
|
||||||
ListEbooks(ctx context.Context, arg ListEbooksParams) ([]Ebooks, error)
|
ListEbooks(ctx context.Context, arg ListEbooksParams) ([]Ebooks, error)
|
||||||
ListUsers(ctx context.Context) ([]ListUsersRow, error)
|
ListUsers(ctx context.Context) ([]ListUsersRow, error)
|
||||||
UpdateEbook(ctx context.Context, arg UpdateEbookParams) (Ebooks, error)
|
UpdateEbook(ctx context.Context, arg UpdateEbookParams) (Ebooks, error)
|
||||||
|
|||||||
@@ -11,6 +11,27 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const AddUserEbookFolder = `-- name: AddUserEbookFolder :one
|
||||||
|
INSERT INTO user_ebook_folders (user_id, folder_path) VALUES ($1, $2) RETURNING id, user_id, folder_path, created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
type AddUserEbookFolderParams struct {
|
||||||
|
UserID pgtype.UUID `db:"user_id" json:"user_id"`
|
||||||
|
FolderPath string `db:"folder_path" json:"folder_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) AddUserEbookFolder(ctx context.Context, arg AddUserEbookFolderParams) (UserEbookFolders, error) {
|
||||||
|
row := q.db.QueryRow(ctx, AddUserEbookFolder, arg.UserID, arg.FolderPath)
|
||||||
|
var i UserEbookFolders
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.FolderPath,
|
||||||
|
&i.CreatedAt,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const CreateEbook = `-- name: CreateEbook :one
|
const CreateEbook = `-- name: CreateEbook :one
|
||||||
INSERT INTO ebooks (title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, series, series_number, tags, asin, date_published, publisher, contributors)
|
INSERT INTO ebooks (title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, series, series_number, tags, asin, date_published, publisher, contributors)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
||||||
@@ -177,6 +198,20 @@ func (q *Queries) DeleteReadingProgress(ctx context.Context, arg DeleteReadingPr
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DeleteUserEbookFolder = `-- name: DeleteUserEbookFolder :exec
|
||||||
|
DELETE FROM user_ebook_folders WHERE user_id = $1 AND folder_path = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type DeleteUserEbookFolderParams struct {
|
||||||
|
UserID pgtype.UUID `db:"user_id" json:"user_id"`
|
||||||
|
FolderPath string `db:"folder_path" json:"folder_path"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) DeleteUserEbookFolder(ctx context.Context, arg DeleteUserEbookFolderParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, DeleteUserEbookFolder, arg.UserID, arg.FolderPath)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
const GetEbook = `-- name: GetEbook :one
|
const GetEbook = `-- name: GetEbook :one
|
||||||
SELECT id, title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, created_at, updated_at, series, series_number, tags, asin, date_published, publisher, contributors FROM ebooks WHERE id = $1
|
SELECT id, title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, created_at, updated_at, series, series_number, tags, asin, date_published, publisher, contributors FROM ebooks WHERE id = $1
|
||||||
`
|
`
|
||||||
@@ -207,6 +242,36 @@ func (q *Queries) GetEbook(ctx context.Context, id pgtype.UUID) (Ebooks, error)
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetEbookByFilePath = `-- name: GetEbookByFilePath :one
|
||||||
|
SELECT id, title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, created_at, updated_at, series, series_number, tags, asin, date_published, publisher, contributors FROM ebooks WHERE file_path = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetEbookByFilePath(ctx context.Context, filePath string) (Ebooks, error) {
|
||||||
|
row := q.db.QueryRow(ctx, GetEbookByFilePath, filePath)
|
||||||
|
var i Ebooks
|
||||||
|
err := row.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.Title,
|
||||||
|
&i.Author,
|
||||||
|
&i.Isbn,
|
||||||
|
&i.Description,
|
||||||
|
&i.FilePath,
|
||||||
|
&i.FileSize,
|
||||||
|
&i.MimeType,
|
||||||
|
&i.CoverImagePath,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.Series,
|
||||||
|
&i.SeriesNumber,
|
||||||
|
&i.Tags,
|
||||||
|
&i.Asin,
|
||||||
|
&i.DatePublished,
|
||||||
|
&i.Publisher,
|
||||||
|
&i.Contributors,
|
||||||
|
)
|
||||||
|
return i, err
|
||||||
|
}
|
||||||
|
|
||||||
const GetEbookRating = `-- name: GetEbookRating :one
|
const GetEbookRating = `-- name: GetEbookRating :one
|
||||||
SELECT id, ebook_id, user_id, rating, created_at, updated_at FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2
|
SELECT id, ebook_id, user_id, rating, created_at, updated_at FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2
|
||||||
`
|
`
|
||||||
@@ -383,6 +448,35 @@ func (q *Queries) GetUserByUsername(ctx context.Context, username string) (Users
|
|||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetUserEbookFolders = `-- name: GetUserEbookFolders :many
|
||||||
|
SELECT id, user_id, folder_path, created_at FROM user_ebook_folders WHERE user_id = $1 ORDER BY created_at
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetUserEbookFolders(ctx context.Context, userID pgtype.UUID) ([]UserEbookFolders, error) {
|
||||||
|
rows, err := q.db.Query(ctx, GetUserEbookFolders, userID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
items := []UserEbookFolders{}
|
||||||
|
for rows.Next() {
|
||||||
|
var i UserEbookFolders
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.UserID,
|
||||||
|
&i.FolderPath,
|
||||||
|
&i.CreatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const ListEbooks = `-- name: ListEbooks :many
|
const ListEbooks = `-- name: ListEbooks :many
|
||||||
SELECT id, title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, created_at, updated_at, series, series_number, tags, asin, date_published, publisher, contributors FROM ebooks ORDER BY created_at DESC LIMIT $1 OFFSET $2
|
SELECT id, title, author, isbn, description, file_path, file_size, mime_type, cover_image_path, created_at, updated_at, series, series_number, tags, asin, date_published, publisher, contributors FROM ebooks ORDER BY created_at DESC LIMIT $1 OFFSET $2
|
||||||
`
|
`
|
||||||
|
|||||||
@@ -97,3 +97,15 @@ RETURNING *;
|
|||||||
|
|
||||||
-- name: DeleteEbookRating :exec
|
-- name: DeleteEbookRating :exec
|
||||||
DELETE FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2;
|
DELETE FROM ebook_ratings WHERE ebook_id = $1 AND user_id = $2;
|
||||||
|
|
||||||
|
-- name: AddUserEbookFolder :one
|
||||||
|
INSERT INTO user_ebook_folders (user_id, folder_path) VALUES ($1, $2) RETURNING *;
|
||||||
|
|
||||||
|
-- name: GetUserEbookFolders :many
|
||||||
|
SELECT * FROM user_ebook_folders WHERE user_id = $1 ORDER BY created_at;
|
||||||
|
|
||||||
|
-- name: DeleteUserEbookFolder :exec
|
||||||
|
DELETE FROM user_ebook_folders WHERE user_id = $1 AND folder_path = $2;
|
||||||
|
|
||||||
|
-- name: GetEbookByFilePath :one
|
||||||
|
SELECT * FROM ebooks WHERE file_path = $1;
|
||||||
Reference in New Issue
Block a user