Compare commits
No commits in common. "828d611f4d0716f3a8f42d3086e334edb653f71d" and "761babfa1af4c4a2f6694e0dfceba404627335f0" have entirely different histories.
828d611f4d
...
761babfa1a
@ -2,13 +2,11 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgconn"
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -50,30 +48,3 @@ func transaction(handler func(context.Context, pgx.Tx) (statusCode int, err erro
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle database error
|
|
||||||
func handleDBError(errIn error) (statusCode int, err error) {
|
|
||||||
if errIn == nil {
|
|
||||||
statusCode = http.StatusOK
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if errors.Is(errIn, pgx.ErrNoRows) {
|
|
||||||
err = fmt.Errorf("not found")
|
|
||||||
statusCode = http.StatusNotFound
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var pgErr *pgconn.PgError
|
|
||||||
if errors.As(errIn, &pgErr) {
|
|
||||||
switch pgErr.Code {
|
|
||||||
case "22P02", "22007": // Invalid data format
|
|
||||||
err = fmt.Errorf("%s", pgErr.Message)
|
|
||||||
statusCode = http.StatusBadRequest
|
|
||||||
return
|
|
||||||
case "23505": // Unique constraint violation
|
|
||||||
err = fmt.Errorf("already exists")
|
|
||||||
statusCode = http.StatusConflict
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return http.StatusInternalServerError, errIn
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
@ -9,7 +8,7 @@ import (
|
|||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
IsAdmin bool `json:"isAdmin"`
|
IsAdmin bool `json:"is_admin"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type MIME struct {
|
type MIME struct {
|
||||||
@ -17,95 +16,50 @@ type MIME struct {
|
|||||||
Extension string `json:"extension"`
|
Extension string `json:"extension"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type Category struct {
|
||||||
CategoryCore struct {
|
ID string `json:"id"`
|
||||||
ID string `json:"id"`
|
Name string `json:"name"`
|
||||||
Name string `json:"name"`
|
Color pgtype.Text `json:"color"`
|
||||||
Color pgtype.Text `json:"color"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
Creator User `json:"creator"`
|
||||||
CategoryItem struct {
|
|
||||||
CategoryCore
|
|
||||||
}
|
|
||||||
CategoryFull struct {
|
|
||||||
CategoryCore
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
Creator User `json:"creator"`
|
|
||||||
Notes pgtype.Text `json:"notes"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
FileCore struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Name pgtype.Text `json:"name"`
|
|
||||||
MIME MIME `json:"mime"`
|
|
||||||
}
|
|
||||||
FileItem struct {
|
|
||||||
FileCore
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
Creator User `json:"creator"`
|
|
||||||
}
|
|
||||||
FileFull struct {
|
|
||||||
FileCore
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
Creator User `json:"creator"`
|
|
||||||
Notes pgtype.Text `json:"notes"`
|
|
||||||
Metadata *json.RawMessage `json:"metadata"`
|
|
||||||
Tags []TagCore `json:"tags"`
|
|
||||||
Viewed int `json:"viewed"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
TagCore struct {
|
|
||||||
ID string `json:"id"`
|
|
||||||
Name string `json:"name"`
|
|
||||||
Color pgtype.Text `json:"color"`
|
|
||||||
}
|
|
||||||
TagItem struct {
|
|
||||||
TagCore
|
|
||||||
Category CategoryCore `json:"category"`
|
|
||||||
}
|
|
||||||
TagFull struct {
|
|
||||||
TagCore
|
|
||||||
Category CategoryCore `json:"category"`
|
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
|
||||||
Creator User `json:"creator"`
|
|
||||||
Notes pgtype.Text `json:"notes"`
|
|
||||||
UsedIncl int `json:"usedIncl"`
|
|
||||||
UsedExcl int `json:"usedExcl"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type Autotag struct {
|
|
||||||
TriggerTag TagCore `json:"triggerTag"`
|
|
||||||
AddTag TagCore `json:"addTag"`
|
|
||||||
IsActive bool `json:"isActive"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type (
|
type File struct {
|
||||||
PoolCore struct {
|
ID string `json:"id"`
|
||||||
ID string `json:"id"`
|
Name pgtype.Text `json:"name"`
|
||||||
Name string `json:"name"`
|
MIME MIME `json:"mime"`
|
||||||
}
|
CreatedAt time.Time `json:"created_at"`
|
||||||
PoolItem struct {
|
Creator User `json:"creator"`
|
||||||
PoolCore
|
}
|
||||||
}
|
|
||||||
PoolFull struct {
|
type Tag struct {
|
||||||
PoolCore
|
ID string `json:"id"`
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
Name string `json:"name"`
|
||||||
Creator User `json:"creator"`
|
Color pgtype.Text `json:"color"`
|
||||||
Notes pgtype.Text `json:"notes"`
|
Category Category `json:"category"`
|
||||||
Viewed int `json:"viewed"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
}
|
Creator User `json:"creator"`
|
||||||
)
|
}
|
||||||
|
|
||||||
|
type Autotag struct {
|
||||||
|
TriggerTag Tag `json:"trigger_tag"`
|
||||||
|
AddTag Tag `json:"add_tag"`
|
||||||
|
IsActive bool `json:"is_active"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Pool struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
CreatedAt time.Time `json:"created_at"`
|
||||||
|
Creator User `json:"creator"`
|
||||||
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
UserAgent string `json:"userAgent"`
|
UserAgent string `json:"user_agent"`
|
||||||
StartedAt time.Time `json:"startedAt"`
|
StartedAt time.Time `json:"started_at"`
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
ExpiresAt time.Time `json:"expires_at"`
|
||||||
LastActivity time.Time `json:"lastActivity"`
|
LastActivity time.Time `json:"last_activity"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pagination struct {
|
type Pagination struct {
|
||||||
|
|||||||
@ -80,20 +80,6 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA public;
|
|||||||
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
|
COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
|
||||||
|
|
||||||
|
|
||||||
--
|
|
||||||
-- Name: uuid_extract_timestamp(uuid); Type: FUNCTION; Schema: public; Owner: -
|
|
||||||
--
|
|
||||||
|
|
||||||
CREATE FUNCTION public.uuid_extract_timestamp(uuid_val uuid) RETURNS timestamp with time zone
|
|
||||||
LANGUAGE sql IMMUTABLE
|
|
||||||
AS $$
|
|
||||||
SELECT to_timestamp(
|
|
||||||
('x' || LEFT(REPLACE(uuid_val::TEXT, '-', ''), 12))::BIT(48)::BIGINT
|
|
||||||
/ 1000.0
|
|
||||||
);
|
|
||||||
$$;
|
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
-- Name: uuid_v7(timestamp with time zone); Type: FUNCTION; Schema: public; Owner: -
|
-- Name: uuid_v7(timestamp with time zone); Type: FUNCTION; Schema: public; Owner: -
|
||||||
--
|
--
|
||||||
@ -304,7 +290,7 @@ CREATE TABLE data.files (
|
|||||||
mime_id smallint NOT NULL,
|
mime_id smallint NOT NULL,
|
||||||
datetime timestamp with time zone DEFAULT clock_timestamp() NOT NULL,
|
datetime timestamp with time zone DEFAULT clock_timestamp() NOT NULL,
|
||||||
notes text,
|
notes text,
|
||||||
metadata jsonb,
|
metadata jsonb NOT NULL,
|
||||||
creator_id smallint NOT NULL
|
creator_id smallint NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user