memos/store/resource.go

108 lines
1.9 KiB
Go
Raw Normal View History

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
)
type Resource struct {
2023-08-04 21:55:07 +08:00
ID int32
2022-05-19 18:32:04 +08:00
// Standard fields
2023-08-04 21:55:07 +08:00
CreatorID int32
2022-05-19 18:32:04 +08:00
CreatedTs int64
UpdatedTs int64
// Domain specific fields
2023-09-16 00:11:07 +08:00
Filename string
Blob []byte
InternalPath string
ExternalLink string
Type string
Size int64
MemoID *int32
2022-05-19 18:32:04 +08:00
}
type FindResource struct {
2023-09-16 11:48:53 +08:00
GetBlob bool
ID *int32
CreatorID *int32
Filename *string
MemoID *int32
HasRelatedMemo bool
Limit *int
Offset *int
2022-05-19 18:32:04 +08:00
}
type UpdateResource struct {
2023-08-04 21:55:07 +08:00
ID int32
UpdatedTs *int64
Filename *string
InternalPath *string
MemoID *int32
UnbindMemo bool
Blob []byte
}
2022-10-01 10:37:02 +08:00
type DeleteResource struct {
ID int32
MemoID *int32
2022-02-03 15:32:03 +08:00
}
func (s *Store) CreateResource(ctx context.Context, create *Resource) (*Resource, error) {
return s.driver.CreateResource(ctx, create)
2023-05-26 00:38:27 +08:00
}
func (s *Store) ListResources(ctx context.Context, find *FindResource) ([]*Resource, error) {
return s.driver.ListResources(ctx, find)
2022-11-06 12:21:58 +08:00
}
func (s *Store) GetResource(ctx context.Context, find *FindResource) (*Resource, error) {
resources, err := s.ListResources(ctx, find)
if err != nil {
return nil, err
}
if len(resources) == 0 {
return nil, nil
}
return resources[0], nil
}
func (s *Store) UpdateResource(ctx context.Context, update *UpdateResource) (*Resource, error) {
return s.driver.UpdateResource(ctx, update)
}
func (s *Store) DeleteResource(ctx context.Context, delete *DeleteResource) error {
err := s.driver.DeleteResource(ctx, delete)
if err != nil {
return err
}
if err := s.Vacuum(ctx); err != nil {
// Prevent linter warning.
return err
}
return 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-02-03 15:32:03 +08:00
return nil
}