|
|
|
@ -94,7 +94,7 @@ func (s *Store) CreateResource(ctx context.Context, create *api.ResourceCreate)
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
resourceRaw, err := s.createResourceImpl(ctx, tx, create)
|
|
|
|
|
resourceRaw, err := createResourceImpl(ctx, tx, create)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -115,7 +115,7 @@ func (s *Store) FindResourceList(ctx context.Context, find *api.ResourceFind) ([
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
resourceRawList, err := s.findResourceListImpl(ctx, tx, find)
|
|
|
|
|
resourceRawList, err := findResourceListImpl(ctx, tx, find)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -135,7 +135,7 @@ func (s *Store) FindResource(ctx context.Context, find *api.ResourceFind) (*api.
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
list, err := s.findResourceListImpl(ctx, tx, find)
|
|
|
|
|
list, err := findResourceListImpl(ctx, tx, find)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -178,7 +178,7 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a
|
|
|
|
|
}
|
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
|
|
resourceRaw, err := s.patchResourceImpl(ctx, tx, patch)
|
|
|
|
|
resourceRaw, err := patchResourceImpl(ctx, tx, patch)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
@ -192,16 +192,10 @@ func (s *Store) PatchResource(ctx context.Context, patch *api.ResourcePatch) (*a
|
|
|
|
|
return resource, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
|
|
|
|
fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id"}
|
|
|
|
|
values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID}
|
|
|
|
|
placeholders := []string{"?", "?", "?", "?", "?", "?"}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
fields = append(fields, "internal_path", "public_id")
|
|
|
|
|
values = append(values, create.InternalPath, create.PublicID)
|
|
|
|
|
placeholders = append(placeholders, "?", "?")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.ResourceCreate) (*resourceRaw, error) {
|
|
|
|
|
fields := []string{"filename", "blob", "external_link", "type", "size", "creator_id", "internal_path", "public_id"}
|
|
|
|
|
values := []any{create.Filename, create.Blob, create.ExternalLink, create.Type, create.Size, create.CreatorID, create.InternalPath, create.PublicID}
|
|
|
|
|
placeholders := []string{"?", "?", "?", "?", "?", "?", "?", "?"}
|
|
|
|
|
query := `
|
|
|
|
|
INSERT INTO resource (
|
|
|
|
|
` + strings.Join(fields, ",") + `
|
|
|
|
@ -218,9 +212,8 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.
|
|
|
|
|
&resourceRaw.Type,
|
|
|
|
|
&resourceRaw.Size,
|
|
|
|
|
&resourceRaw.CreatorID,
|
|
|
|
|
}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID)
|
|
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
|
&resourceRaw.PublicID,
|
|
|
|
|
}
|
|
|
|
|
dests = append(dests, []any{&resourceRaw.CreatedTs, &resourceRaw.UpdatedTs}...)
|
|
|
|
|
if err := tx.QueryRowContext(ctx, query, values...).Scan(dests...); err != nil {
|
|
|
|
@ -230,7 +223,7 @@ func (s *Store) createResourceImpl(ctx context.Context, tx *sql.Tx, create *api.
|
|
|
|
|
return &resourceRaw, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
|
|
|
|
|
func patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.ResourcePatch) (*resourceRaw, error) {
|
|
|
|
|
set, args := []string{}, []any{}
|
|
|
|
|
|
|
|
|
|
if v := patch.UpdatedTs; v != nil {
|
|
|
|
@ -244,12 +237,7 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args = append(args, patch.ID)
|
|
|
|
|
|
|
|
|
|
fields := []string{"id", "filename", "external_link", "type", "size", "creator_id", "created_ts", "updated_ts"}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
fields = append(fields, "internal_path", "public_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, ", ") + `
|
|
|
|
@ -265,9 +253,8 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re
|
|
|
|
|
&resourceRaw.CreatorID,
|
|
|
|
|
&resourceRaw.CreatedTs,
|
|
|
|
|
&resourceRaw.UpdatedTs,
|
|
|
|
|
}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID)
|
|
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
|
&resourceRaw.PublicID,
|
|
|
|
|
}
|
|
|
|
|
if err := tx.QueryRowContext(ctx, query, args...).Scan(dests...); err != nil {
|
|
|
|
|
return nil, FormatError(err)
|
|
|
|
@ -276,7 +263,7 @@ func (s *Store) patchResourceImpl(ctx context.Context, tx *sql.Tx, patch *api.Re
|
|
|
|
|
return &resourceRaw, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
|
|
|
|
func findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.ResourceFind) ([]*resourceRaw, error) {
|
|
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
|
|
|
|
|
|
if v := find.ID; v != nil {
|
|
|
|
@ -295,13 +282,10 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.
|
|
|
|
|
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"}
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
fields = append(fields, "internal_path", "public_id")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
query := fmt.Sprintf(`
|
|
|
|
|
SELECT
|
|
|
|
@ -339,13 +323,12 @@ func (s *Store) findResourceListImpl(ctx context.Context, tx *sql.Tx, find *api.
|
|
|
|
|
&resourceRaw.CreatorID,
|
|
|
|
|
&resourceRaw.CreatedTs,
|
|
|
|
|
&resourceRaw.UpdatedTs,
|
|
|
|
|
&resourceRaw.InternalPath,
|
|
|
|
|
&resourceRaw.PublicID,
|
|
|
|
|
}
|
|
|
|
|
if find.GetBlob {
|
|
|
|
|
dests = append(dests, &resourceRaw.Blob)
|
|
|
|
|
}
|
|
|
|
|
if s.profile.IsDev() {
|
|
|
|
|
dests = append(dests, &resourceRaw.InternalPath, &resourceRaw.PublicID)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Scan(dests...); err != nil {
|
|
|
|
|
return nil, FormatError(err)
|
|
|
|
|
}
|
|
|
|
|