memos/store/resource.go

296 lines
6.4 KiB
Go
Raw Normal View History

package store
2022-02-03 10:32:03 +03:00
import (
2022-08-07 05:17:12 +03:00
"context"
2022-05-21 19:59:22 +03:00
"database/sql"
2022-02-03 10:32:03 +03:00
"fmt"
"strings"
)
type Resource struct {
2022-05-19 13:32:04 +03:00
ID int
// Standard fields
CreatorID int
CreatedTs int64
UpdatedTs int64
// Domain specific fields
Filename string
Blob []byte
InternalPath string
ExternalLink string
Type string
Size int64
PublicID string
LinkedMemoAmount int
2022-05-19 13:32:04 +03:00
}
type FindResource struct {
GetBlob bool
ID *int
CreatorID *int
Filename *string
MemoID *int
PublicID *string
Limit *int
Offset *int
2022-05-19 13:32:04 +03:00
}
type UpdateResource struct {
ID int
UpdatedTs *int64
Filename *string
PublicID *string
}
2022-10-01 05:37:02 +03:00
type DeleteResource struct {
ID int
2022-02-03 10:32:03 +03:00
}
func (s *Store) CreateResourceV1(ctx context.Context, create *Resource) (*Resource, error) {
2023-05-25 19:38:27 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
if err := tx.QueryRowContext(ctx, `
INSERT INTO resource (
filename,
blob,
external_link,
type,
size,
creator_id,
internal_path,
public_id
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id, created_ts, updated_ts
`,
create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.PublicID,
).Scan(&create.ID, &create.CreatedTs, &create.UpdatedTs); err != nil {
2023-05-25 19:38:27 +03:00
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
2023-05-25 19:38:27 +03:00
}
resource := create
2023-05-25 19:38:27 +03:00
return resource, nil
}
func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resource, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
resources, err := listResources(ctx, tx, find)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
2022-05-19 13:32:04 +03:00
}
return resources, nil
}
func (s *Store) GetResource(ctx context.Context, find *FindResource) (*Resource, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
resources, err := listResources(ctx, tx, find)
2022-02-04 13:54:24 +03:00
if err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, err
2022-02-04 13:54:24 +03:00
}
if len(resources) == 0 {
return nil, nil
2023-05-25 19:38:27 +03:00
}
2022-10-01 05:37:02 +03:00
return resources[0], nil
2022-02-04 13:54:24 +03:00
}
func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error) {
2022-08-07 05:17:12 +03:00
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, FormatError(err)
}
defer tx.Rollback()
set, args := []string{}, []any{}
if v := update.UpdatedTs; v != nil {
set, args = append(set, "updated_ts = ?"), append(args, *v)
}
if v := update.Filename; v != nil {
set, args = append(set, "filename = ?"), append(args, *v)
}
if v := update.PublicID; v != nil {
set, args = append(set, "public_id = ?"), append(args, *v)
}
args = append(args, update.ID)
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts", "internal_path", "public_id"}
query := `
UPDATE resource
SET ` + strings.Join(set, ", ") + `
WHERE id = ?
RETURNING ` + strings.Join(fields, ", ")
resource := Resource{}
dests := []any{
&resource.ID,
&resource.Filename,
&resource.ExternalLink,
&resource.Type,
&resource.Size,
&resource.CreatorID,
&resource.CreatedTs,
&resource.UpdatedTs,
&resource.InternalPath,
&resource.PublicID,
}
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
2022-02-03 10:32:03 +03:00
return nil, FormatError(err)
}
if err := tx.Commit(); err != nil {
return nil, err
}
return &resource, nil
}
func (s *Store) DeleteResourceV1(ctx context.Context, delete *DeleteResource) error {
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return FormatError(err)
}
defer tx.Rollback()
if _, err := tx.ExecContext(ctx, `
DELETE FROM resource
WHERE id = ?
`, delete.ID); err != nil {
return err
}
if err := tx.Commit(); err != nil {
// Prevent linter warning.
return err
}
return nil
2022-02-03 10:32:03 +03:00
}
func listResources(ctx context.Context, tx *sql.Tx, find *FindResource) ([]*Resource, error) {
where, args := []string{"1 = 1"}, []any{}
2022-02-06 06:26:53 +03:00
2022-05-02 21:05:43 +03:00
if v := find.ID; v != nil {
where, args = append(where, "resource.id = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2022-05-02 21:05:43 +03:00
if v := find.CreatorID; v != nil {
where, args = append(where, "resource.creator_id = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
if v := find.Filename; v != nil {
where, args = append(where, "resource.filename = ?"), append(args, *v)
2022-02-03 10:32:03 +03:00
}
2022-09-30 17:58:59 +03:00
if v := find.MemoID; v != nil {
where, args = append(where, "resource.id in (SELECT resource_id FROM memo_resource WHERE memo_id = ?)"), append(args, *v)
2022-09-30 17:58:59 +03:00
}
if v := find.PublicID; v != nil {
where, args = append(where, "resource.public_id = ?"), append(args, *v)
}
fields := []string{"resource.id", "resource.filename", "resource.external_link", "resource.type", "resource.size", "resource.creator_id", "resource.created_ts", "resource.updated_ts", "internal_path", "public_id"}
if find.GetBlob {
fields = append(fields, "resource.blob")
}
query := fmt.Sprintf(`
2022-02-03 10:32:03 +03:00
SELECT
COUNT(DISTINCT memo_resource.memo_id) AS linked_memo_amount,
%s
2022-02-03 10:32:03 +03:00
FROM resource
LEFT JOIN memo_resource ON resource.id = memo_resource.resource_id
WHERE %s
GROUP BY resource.id
ORDER BY resource.id DESC
`, strings.Join(fields, ", "), strings.Join(where, " AND "))
if find.Limit != nil {
query = fmt.Sprintf("%s LIMIT %d", query, *find.Limit)
if find.Offset != nil {
query = fmt.Sprintf("%s OFFSET %d", query, *find.Offset)
}
}
2022-08-07 05:17:12 +03:00
rows, err := tx.QueryContext(ctx, query, args...)
2022-02-03 10:32:03 +03:00
if err != nil {
return nil, FormatError(err)
}
defer rows.Close()
list := make([]*Resource, 0)
for rows.Next() {
resource := Resource{}
dests := []any{
&resource.LinkedMemoAmount,
&resource.ID,
&resource.Filename,
&resource.ExternalLink,
&resource.Type,
&resource.Size,
&resource.CreatorID,
&resource.CreatedTs,
&resource.UpdatedTs,
&resource.InternalPath,
&resource.PublicID,
}
if find.GetBlob {
dests = append(dests, &resource.Blob)
}
if err := rows.Scan(dests...); err != nil {
2022-02-03 10:32:03 +03:00
return nil, FormatError(err)
}
list = append(list, &resource)
}
if err := rows.Err(); err != nil {
2022-02-03 10:32:03 +03:00
return nil, FormatError(err)
}
return list, nil
2022-11-06 07:21:58 +03:00
}
func vacuumResource(ctx context.Context, tx *sql.Tx) error {
stmt := `
DELETE FROM
resource
WHERE
creator_id NOT IN (
SELECT
id
FROM
user
)`
_, err := tx.ExecContext(ctx, stmt)
if err != nil {
return FormatError(err)
}
2022-02-03 10:32:03 +03:00
return nil
}