add document service and acitons
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
func (s *Service) Rename(ctx context.Context, documentID ident.UUID, name string) (*Document, error) {
|
||||
if _, err := s.getActiveDocument(ctx, documentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
normalized, nameKey, err := normalizeName(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updated, err := s.db.WriteQueries.UpdateDocumentName(ctx, database.UpdateDocumentNameParams{
|
||||
Name: normalized,
|
||||
NameKey: nameKey,
|
||||
UpdatedAt: nowMillis(),
|
||||
ID: documentID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNameTaken, normalized)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(updated), nil
|
||||
}
|
||||
|
||||
func (s *Service) Move(ctx context.Context, documentID, newParentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if document.ParentID == nil {
|
||||
return nil, ErrCannotMoveRoot
|
||||
}
|
||||
if *document.ParentID == newParentID {
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
if document.ID == newParentID {
|
||||
return nil, ErrInvalidMove
|
||||
}
|
||||
|
||||
parent, err := s.getDocument(ctx, newParentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrDocumentNotFound) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, newParentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if parent.DeletedAt.Valid {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, newParentID)
|
||||
}
|
||||
if !isContainerKind(parent.Kind) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidParent, parent.Kind)
|
||||
}
|
||||
if strings.HasPrefix(parent.Path, document.Path) {
|
||||
return nil, ErrInvalidMove
|
||||
}
|
||||
|
||||
newPath := parent.Path + hex.EncodeToString(document.ID[:]) + "/"
|
||||
newDepth := parent.Depth + 1
|
||||
depthDelta := newDepth - document.Depth
|
||||
now := nowMillis()
|
||||
|
||||
var moved database.Document
|
||||
|
||||
err = s.withTx(ctx, func(q *database.Queries) error {
|
||||
updated, err := q.MoveDocument(ctx, database.MoveDocumentParams{
|
||||
ParentID: &newParentID,
|
||||
Path: newPath,
|
||||
Depth: newDepth,
|
||||
UpdatedAt: now,
|
||||
ID: documentID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
moved = updated
|
||||
|
||||
descendants, err := q.ListDocumentDescendants(ctx, database.ListDocumentDescendantsParams{
|
||||
Prefix: document.Path,
|
||||
RootID: documentID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, descendant := range descendants {
|
||||
relativePath := strings.TrimPrefix(descendant.Path, document.Path)
|
||||
if err := q.UpdateDocumentLocation(ctx, database.UpdateDocumentLocationParams{
|
||||
Path: newPath + relativePath,
|
||||
Depth: descendant.Depth + depthDelta,
|
||||
UpdatedAt: now,
|
||||
ID: descendant.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNameTaken, document.Name)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentFromDB(moved), nil
|
||||
}
|
||||
|
||||
func (s *Service) Trash(ctx context.Context, documentID, deletedBy ident.UUID) (*Document, error) {
|
||||
document, err := s.getActiveDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
batch, err := ident.NewV7()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := nowMillis()
|
||||
|
||||
deleted, err := s.db.WriteQueries.TrashDocumentSubtree(ctx, database.TrashDocumentSubtreeParams{
|
||||
DeletedAt: sql.NullInt64{
|
||||
Int64: now,
|
||||
Valid: true,
|
||||
},
|
||||
DeletedBy: &deletedBy,
|
||||
TrashBatch: batch.Bytes(),
|
||||
UpdatedAt: now,
|
||||
Prefix: document.Path,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if deleted == 0 {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
}
|
||||
|
||||
func (s *Service) Restore(ctx context.Context, documentID ident.UUID) (*Document, error) {
|
||||
document, err := s.getDocument(ctx, documentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !document.DeletedAt.Valid {
|
||||
return documentFromDB(*document), nil
|
||||
}
|
||||
if len(document.TrashBatch) == 0 {
|
||||
return nil, fmt.Errorf("%w: %s", ErrInvalidTrashBatch, documentID)
|
||||
}
|
||||
|
||||
for parentID := document.ParentID; parentID != nil; {
|
||||
parent, err := s.getDocument(ctx, *parentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrDocumentNotFound) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentNotFound, *parentID)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if parent.DeletedAt.Valid && !bytes.Equal(parent.TrashBatch, document.TrashBatch) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrParentDeleted, *parentID)
|
||||
}
|
||||
parentID = parent.ParentID
|
||||
}
|
||||
|
||||
restored, err := s.db.WriteQueries.RestoreDocumentBatch(ctx, database.RestoreDocumentBatchParams{
|
||||
UpdatedAt: nowMillis(),
|
||||
TrashBatch: document.TrashBatch,
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrNameTaken, document.Name)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if restored == 0 {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, documentID)
|
||||
}
|
||||
|
||||
return s.Get(ctx, documentID)
|
||||
}
|
||||
Reference in New Issue
Block a user