mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-09 21:20:49 -06:00
Split rename and reparent
This commit is contained in:
@@ -27,10 +27,13 @@ var (
|
||||
ErrInsufficientPermissions = errors.New("insufficient permissions")
|
||||
ErrCannotGrantOwnerPermission = errors.New("cannot grant owner permission")
|
||||
ErrResourceNotCollection = errors.New("cannot add member to non-collection resource")
|
||||
ErrCannotReparentRootResource = errors.New("cannot reparent root resource")
|
||||
ErrCannotReparentToRoot = errors.New("cannot reparent resource to root")
|
||||
)
|
||||
|
||||
type ResourceInfo interface {
|
||||
ID() uuid.UUID
|
||||
ParentID() *uuid.UUID
|
||||
Name() string
|
||||
Size() int64
|
||||
Permission() int32
|
||||
@@ -47,6 +50,9 @@ type Resource interface {
|
||||
ReadDir(ctx context.Context) ([]Resource, error)
|
||||
CreateMemberResource(ctx context.Context, id uuid.UUID, name string, dir bool) (Resource, error)
|
||||
DeleteRecursive(ctx context.Context, hardDelete bool) error
|
||||
UpdateName(ctx context.Context, name string) error
|
||||
UpdateParent(ctx context.Context, parent uuid.UUID) error
|
||||
UpdatePermissions(ctx context.Context, userID int32, permission int32) error
|
||||
}
|
||||
|
||||
type resource struct {
|
||||
@@ -208,6 +214,17 @@ func (r resource) DeleteRecursive(ctx context.Context, hardDelete bool) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (r resource) UpdateName(ctx context.Context, name string) error {
|
||||
return r.db.Queries().UpdateResourceName(ctx, sql.UpdateResourceNameParams{ID: r.id, Name: name})
|
||||
}
|
||||
|
||||
func (r resource) UpdateParent(ctx context.Context, parent uuid.UUID) error {
|
||||
if r.parentID == nil {
|
||||
return ErrCannotReparentRootResource
|
||||
}
|
||||
return r.db.Queries().UpdateResourceParent(ctx, sql.UpdateResourceParentParams{ID: r.id, Parent: parent})
|
||||
}
|
||||
|
||||
func (r resource) UpdatePermissions(ctx context.Context, userID int32, permission int32) error {
|
||||
if r.permission < PermissionAdmin {
|
||||
return ErrInsufficientPermissions
|
||||
|
||||
@@ -16,7 +16,6 @@ type Silo interface {
|
||||
Owner() int32
|
||||
StorageName() string
|
||||
ResourceByPath(ctx context.Context, path string, userID int32) (Resource, error)
|
||||
Move(ctx context.Context, id uuid.UUID, parent uuid.UUID, name string) error
|
||||
}
|
||||
|
||||
type silo struct {
|
||||
@@ -53,10 +52,6 @@ func (s *silo) StorageName() string {
|
||||
return s.storage.Name()
|
||||
}
|
||||
|
||||
func (s *silo) Move(ctx context.Context, id uuid.UUID, parent uuid.UUID, name string) error {
|
||||
return s.db.Queries().Rename(ctx, sql.RenameParams{ID: id, Parent: parent, Name: name})
|
||||
}
|
||||
|
||||
func (s *silo) ResourceByPath(ctx context.Context, path string, userID int32) (Resource, error) {
|
||||
path = strings.Trim(path, "/")
|
||||
segments := strings.Split(path, "/")
|
||||
|
||||
@@ -158,8 +158,15 @@ func (a adapter) Rename(ctx context.Context, oldName, newName string) error {
|
||||
return fs.ErrNotExist
|
||||
}
|
||||
|
||||
if err = a.silo.Move(ctx, src.ID(), parent.ID(), newName); err != nil {
|
||||
return err
|
||||
if src.ParentID() != nil && *src.ParentID() != parent.ID() {
|
||||
if err = src.UpdateParent(ctx, parent.ID()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if src.Name() != newName && newName != "" && newName != "/" {
|
||||
if err = src.UpdateName(ctx, newName); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -186,26 +186,6 @@ func (q *Queries) ReadDir(ctx context.Context, arg ReadDirParams) ([]ReadDirRow,
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const rename = `-- name: Rename :exec
|
||||
UPDATE resources
|
||||
SET
|
||||
name = CASE WHEN ($1::text = '') THEN name ELSE $1::text END,
|
||||
parent = $2::uuid,
|
||||
modified = NOW()
|
||||
WHERE id = $3::uuid
|
||||
`
|
||||
|
||||
type RenameParams struct {
|
||||
Name string
|
||||
Parent uuid.UUID
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) Rename(ctx context.Context, arg RenameParams) error {
|
||||
_, err := q.db.Exec(ctx, rename, arg.Name, arg.Parent, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const resourceById = `-- name: ResourceById :one
|
||||
SELECT id, parent, name, dir, created, modified, deleted, size, etag from resources WHERE id = $1
|
||||
`
|
||||
@@ -320,3 +300,39 @@ func (q *Queries) UpdateResourceModified(ctx context.Context, id uuid.UUID) erro
|
||||
_, err := q.db.Exec(ctx, updateResourceModified, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateResourceName = `-- name: UpdateResourceName :exec
|
||||
UPDATE resources
|
||||
SET
|
||||
name = CASE WHEN ($1::text = '') THEN name ELSE $1::text END,
|
||||
modified = NOW()
|
||||
WHERE id = $2::uuid
|
||||
`
|
||||
|
||||
type UpdateResourceNameParams struct {
|
||||
Name string
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateResourceName(ctx context.Context, arg UpdateResourceNameParams) error {
|
||||
_, err := q.db.Exec(ctx, updateResourceName, arg.Name, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
const updateResourceParent = `-- name: UpdateResourceParent :exec
|
||||
UPDATE resources
|
||||
SET
|
||||
parent = $1::uuid,
|
||||
modified = NOW()
|
||||
WHERE id = $2::uuid
|
||||
`
|
||||
|
||||
type UpdateResourceParentParams struct {
|
||||
Parent uuid.UUID
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) UpdateResourceParent(ctx context.Context, arg UpdateResourceParentParams) error {
|
||||
_, err := q.db.Exec(ctx, updateResourceParent, arg.Parent, arg.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -22,11 +22,17 @@ SET
|
||||
modified = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: Rename :exec
|
||||
-- name: UpdateResourceParent :exec
|
||||
UPDATE resources
|
||||
SET
|
||||
parent = @parent::uuid,
|
||||
modified = NOW()
|
||||
WHERE id = @id::uuid;
|
||||
|
||||
-- name: UpdateResourceName :exec
|
||||
UPDATE resources
|
||||
SET
|
||||
name = CASE WHEN (@name::text = '') THEN name ELSE @name::text END,
|
||||
parent = @parent::uuid,
|
||||
modified = NOW()
|
||||
WHERE id = @id::uuid;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user