add document service and acitons
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package document
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mediarr/database"
|
||||
"mediarr/ident"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
db database.DatabaseClient
|
||||
}
|
||||
|
||||
func NewService(db database.DatabaseClient) *Service {
|
||||
return &Service{db: db}
|
||||
}
|
||||
|
||||
func (s *Service) withTx(ctx context.Context, fn func(*database.Queries) error) error {
|
||||
tx, err := s.db.WriteDB.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
_ = tx.Rollback()
|
||||
}()
|
||||
|
||||
if err := fn(s.db.WriteQueries.WithTx(tx)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
func (s *Service) getDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
||||
document, err := s.db.Queries.GetDocument(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentNotFound, id)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *Service) getActiveDocument(ctx context.Context, id ident.UUID) (*database.Document, error) {
|
||||
document, err := s.getDocument(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if document.DeletedAt.Valid {
|
||||
return nil, fmt.Errorf("%w: %s", ErrDocumentDeleted, id)
|
||||
}
|
||||
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func normalizeName(name string) (string, string, error) {
|
||||
normalized := strings.TrimSpace(name)
|
||||
if normalized == "" || len(normalized) > 255 || strings.Contains(normalized, "/") || strings.ContainsRune(normalized, 0) {
|
||||
return "", "", fmt.Errorf("%w: %q", ErrInvalidName, name)
|
||||
}
|
||||
|
||||
return normalized, strings.ToLower(normalized), nil
|
||||
}
|
||||
|
||||
func normalizeVisibility(visibility string) (string, error) {
|
||||
if visibility == "" {
|
||||
return VisibilityPrivate, nil
|
||||
}
|
||||
|
||||
switch visibility {
|
||||
case VisibilityPrivate, VisibilityCluster, VisibilityFederated, VisibilityPublic:
|
||||
return visibility, nil
|
||||
default:
|
||||
return "", fmt.Errorf("%w: %q", ErrInvalidVisibility, visibility)
|
||||
}
|
||||
}
|
||||
|
||||
func metadataValue(metadata string) string {
|
||||
if metadata == "" {
|
||||
return "{}"
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func aclInheritValue(value *bool) int64 {
|
||||
if value == nil || *value {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func isContainerKind(kind string) bool {
|
||||
return kind == KindLibrary || kind == KindFolder
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
return err != nil && strings.Contains(strings.ToLower(err.Error()), "unique constraint failed")
|
||||
}
|
||||
|
||||
func documentFromDB(document database.Document) *Document {
|
||||
visibility := ""
|
||||
if document.Visibility.Valid {
|
||||
visibility = document.Visibility.String
|
||||
}
|
||||
|
||||
var position *string
|
||||
if document.Position.Valid {
|
||||
value := document.Position.String
|
||||
position = &value
|
||||
}
|
||||
|
||||
var deletedAt *int64
|
||||
if document.DeletedAt.Valid {
|
||||
value := document.DeletedAt.Int64
|
||||
deletedAt = &value
|
||||
}
|
||||
|
||||
return &Document{
|
||||
ID: document.ID,
|
||||
Kind: document.Kind,
|
||||
OwnerID: document.OwnerID,
|
||||
CreatedBy: document.CreatedBy,
|
||||
ParentID: document.ParentID,
|
||||
Name: document.Name,
|
||||
NameKey: document.NameKey,
|
||||
Path: document.Path,
|
||||
Depth: document.Depth,
|
||||
Position: position,
|
||||
HeadVersionID: document.HeadVersionID,
|
||||
Metadata: document.Metadata,
|
||||
PolicyID: document.PolicyID,
|
||||
Visibility: visibility,
|
||||
ACLInherit: document.AclInherit != 0,
|
||||
Rev: document.Rev,
|
||||
CreatedAt: document.CreatedAt,
|
||||
UpdatedAt: document.UpdatedAt,
|
||||
DeletedAt: deletedAt,
|
||||
DeletedBy: document.DeletedBy,
|
||||
TrashBatch: document.TrashBatch,
|
||||
}
|
||||
}
|
||||
|
||||
func nowMillis() int64 {
|
||||
return time.Now().UnixMilli()
|
||||
}
|
||||
Reference in New Issue
Block a user