Files
2026-11-15 09:04:18 +01:00

68 lines
1.5 KiB
Go

package document
import (
"context"
"mediarr/ident"
"mediarr/permission"
)
func (s *Service) Get(ctx context.Context, actorID, id ident.UUID) (*Document, error) {
document, err := s.getDocument(ctx, id)
if err != nil {
return nil, err
}
if err := s.can(ctx, actorID, id, permission.Read); err != nil {
return nil, err
}
return documentFromDB(*document), nil
}
func (s *Service) ListChildren(ctx context.Context, actorID, parentID ident.UUID) ([]Document, error) {
if _, err := s.getActiveDocument(ctx, parentID); err != nil {
return nil, err
}
if err := s.can(ctx, actorID, parentID, permission.Read); err != nil {
return nil, err
}
rows, err := s.db.Queries.ListDocumentChildren(ctx, &parentID)
if err != nil {
return nil, err
}
documents := make([]Document, 0, len(rows))
for _, row := range rows {
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
if err != nil {
return nil, err
}
if allowed {
documents = append(documents, *documentFromDB(row))
}
}
return documents, nil
}
func (s *Service) ListLibraries(ctx context.Context, actorID ident.UUID) ([]Document, error) {
rows, err := s.db.Queries.ListLibraries(ctx)
if err != nil {
return nil, err
}
documents := make([]Document, 0, len(rows))
for _, row := range rows {
allowed, err := s.allowed(ctx, actorID, row.ID, permission.Read)
if err != nil {
return nil, err
}
if allowed {
documents = append(documents, *documentFromDB(row))
}
}
return documents, nil
}