add document service and acitons

This commit is contained in:
venture
2026-11-14 11:37:27 +01:00
parent 9d4f827d38
commit 03da7d9fff
13 changed files with 2177 additions and 5 deletions
+118
View File
@@ -0,0 +1,118 @@
package document
import (
"context"
"database/sql"
"encoding/hex"
"errors"
"fmt"
"mediarr/database"
"mediarr/ident"
)
func (s *Service) CreateLibrary(ctx context.Context, params CreateLibraryParams) (*Document, error) {
name, nameKey, err := normalizeName(params.Name)
if err != nil {
return nil, err
}
visibility, err := normalizeVisibility(params.Visibility)
if err != nil {
return nil, err
}
if _, err := s.db.Queries.GetStoragePolicy(ctx, params.PolicyID); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, fmt.Errorf("%w: %s", ErrPolicyNotFound, params.PolicyID)
}
return nil, err
}
id, err := ident.NewV7()
if err != nil {
return nil, err
}
now := nowMillis()
created, err := s.db.WriteQueries.CreateDocument(ctx, database.CreateDocumentParams{
ID: id,
Kind: KindLibrary,
OwnerID: params.OwnerID,
CreatedBy: params.CreatedBy,
ParentID: nil,
Name: name,
NameKey: nameKey,
Path: hex.EncodeToString(id[:]) + "/",
Depth: 0,
Metadata: metadataValue(params.Metadata),
PolicyID: &params.PolicyID,
Visibility: sql.NullString{String: visibility, Valid: true},
AclInherit: aclInheritValue(params.ACLInherit),
Rev: 1,
CreatedAt: now,
UpdatedAt: now,
})
if err != nil {
if isUniqueViolation(err) {
return nil, fmt.Errorf("%w: %s", ErrNameTaken, name)
}
return nil, err
}
return documentFromDB(created), nil
}
func (s *Service) CreateFolder(ctx context.Context, params CreateFolderParams) (*Document, error) {
name, nameKey, err := normalizeName(params.Name)
if err != nil {
return nil, err
}
parent, err := s.getDocument(ctx, params.ParentID)
if err != nil {
if errors.Is(err, ErrDocumentNotFound) {
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, params.ParentID)
}
return nil, err
}
if parent.DeletedAt.Valid {
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, params.ParentID)
}
if !isContainerKind(parent.Kind) {
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
}
id, err := ident.NewV7()
if err != nil {
return nil, err
}
now := nowMillis()
created, err := s.db.WriteQueries.CreateDocument(ctx, database.CreateDocumentParams{
ID: id,
Kind: KindFolder,
OwnerID: params.OwnerID,
CreatedBy: params.CreatedBy,
ParentID: &params.ParentID,
Name: name,
NameKey: nameKey,
Path: parent.Path + hex.EncodeToString(id[:]) + "/",
Depth: parent.Depth + 1,
Metadata: metadataValue(params.Metadata),
AclInherit: aclInheritValue(params.ACLInherit),
Rev: 1,
CreatedAt: now,
UpdatedAt: now,
})
if err != nil {
if isUniqueViolation(err) {
return nil, fmt.Errorf("%w: %s", ErrNameTaken, name)
}
return nil, err
}
return documentFromDB(created), nil
}