- Updated DbHandler to accept configuration parameters instead of using environment variables directly. - Moved database queries from handler.go to a dedicated queries package for better organization and separation of concerns. - Implemented error handling in query functions to provide more informative error messages. - Refactored image handling to utilize the new queries package for database interactions. - Cleaned up unused code and comments across various files. - Improved overall code readability and maintainability.
95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package queries
|
|
|
|
import (
|
|
"backend/typedefs"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
)
|
|
|
|
// STORAGE
|
|
|
|
func StorageNewId() (string, error) {
|
|
var newId string
|
|
err := Db.Dbpool.QueryRow(Db.Ctx, "INSERT INTO public.storageindex(filename)VALUES (null) RETURNING id;").Scan(&newId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if newId == "" {
|
|
return "", fmt.Errorf("failed to create new id")
|
|
}
|
|
return newId, nil
|
|
|
|
}
|
|
|
|
func StorageGet_min() ([]struct {
|
|
Id string
|
|
Ext string
|
|
}, error) {
|
|
var items_db []struct {
|
|
Id string
|
|
Ext string
|
|
}
|
|
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &items_db, "SELECT id, ext FROM public.storageindex")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get items: %w", err)
|
|
}
|
|
return items_db, nil
|
|
}
|
|
|
|
func StorageUpdate(id, filename, ext string, from_admin bool, expires_at *time.Time) error {
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "UPDATE public.storageindex set filename= $2, ext = $3, from_admin = $4, expires_at = $5 where id = $1", id, filename, ext, from_admin, expires_at)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update storage index: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StorageDelete(id string) error {
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.storageindex WHERE id = $1", id)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete storage index: %w", err)
|
|
}
|
|
return nil
|
|
|
|
}
|
|
|
|
func StorageDeleteUnused() error {
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.storageindex WHERE filename IS NULL")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete storage index: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StorageCheckToken(token string) error {
|
|
var tk []string
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &tk, "SELECT token FROM public.temp_tokens WHERE token = $1 and elapse > $2", token, time.Now())
|
|
if err != nil {
|
|
return fmt.Errorf("failed to check token: %w", err)
|
|
}
|
|
if tk == nil {
|
|
return fmt.Errorf("invalid or expired token")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StorageCleanExpiredTokens(token string) error {
|
|
_, err := Db.Dbpool.Exec(Db.Ctx, "DELETE FROM public.temp_tokens WHERE elapse < $1", time.Now())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func StorageGetExpiredFiles() ([]typedefs.Storageindex, error) {
|
|
|
|
var items_db []typedefs.Storageindex
|
|
err := pgxscan.Select(Db.Ctx, Db.Dbpool, &items_db, "SELECT * FROM public.storageindex WHERE expires_at is not null and expires_at < NOW()")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get expired items: %w", err)
|
|
}
|
|
return items_db, nil
|
|
}
|