2022-09-12 22:47:27 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-27 23:52:13 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2022-09-12 22:47:27 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/document"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
|
2022-09-27 23:52:13 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/pkgs/pathlib"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidDocExtension = errors.New("invalid document extension")
|
2022-09-12 22:47:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DocumentRepository struct {
|
2022-09-27 23:52:13 +00:00
|
|
|
db *ent.Client
|
|
|
|
dir string
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
type (
|
|
|
|
DocumentCreate struct {
|
|
|
|
Title string `json:"title"`
|
|
|
|
Content io.Reader `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentOut struct {
|
|
|
|
ID uuid.UUID `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func mapDocumentOut(doc *ent.Document) DocumentOut {
|
|
|
|
return DocumentOut{
|
|
|
|
ID: doc.ID,
|
|
|
|
Title: doc.Title,
|
|
|
|
Path: doc.Path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
mapDocumentOutErr = mapTErrFunc(mapDocumentOut)
|
|
|
|
mapDocumentOutEachErr = mapTEachErrFunc(mapDocumentOut)
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *DocumentRepository) path(gid uuid.UUID, ext string) string {
|
|
|
|
return pathlib.Safe(filepath.Join(r.dir, gid.String(), "documents", uuid.NewString()+ext))
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *DocumentRepository) GetAll(ctx context.Context, gid uuid.UUID) ([]DocumentOut, error) {
|
|
|
|
return mapDocumentOutEachErr(r.db.Document.
|
|
|
|
Query().
|
2022-09-12 22:47:27 +00:00
|
|
|
Where(document.HasGroupWith(group.ID(gid))).
|
2022-09-27 23:52:13 +00:00
|
|
|
All(ctx),
|
|
|
|
)
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *DocumentRepository) Get(ctx context.Context, id uuid.UUID) (DocumentOut, error) {
|
|
|
|
return mapDocumentOutErr(r.db.Document.Get(ctx, id))
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 23:52:13 +00:00
|
|
|
func (r *DocumentRepository) Create(ctx context.Context, gid uuid.UUID, doc DocumentCreate) (DocumentOut, error) {
|
|
|
|
ext := filepath.Ext(doc.Title)
|
|
|
|
if ext == "" {
|
|
|
|
return DocumentOut{}, ErrInvalidDocExtension
|
|
|
|
}
|
|
|
|
|
|
|
|
path := r.path(gid, ext)
|
|
|
|
|
|
|
|
parent := filepath.Dir(path)
|
|
|
|
err := os.MkdirAll(parent, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return DocumentOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return DocumentOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(f, doc.Content)
|
|
|
|
if err != nil {
|
|
|
|
return DocumentOut{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapDocumentOutErr(r.db.Document.Create().
|
|
|
|
SetGroupID(gid).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetTitle(doc.Title).
|
2022-09-27 23:52:13 +00:00
|
|
|
SetPath(path).
|
|
|
|
Save(ctx),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DocumentRepository) Rename(ctx context.Context, id uuid.UUID, title string) (DocumentOut, error) {
|
|
|
|
return mapDocumentOutErr(r.db.Document.UpdateOneID(id).
|
|
|
|
SetTitle(title).
|
|
|
|
Save(ctx))
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *DocumentRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
doc, err := r.db.Document.Get(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(doc.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-09-12 22:47:27 +00:00
|
|
|
return r.db.Document.DeleteOneID(id).Exec(ctx)
|
|
|
|
}
|