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