2022-09-12 22:47:27 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-09-24 19:33:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/ent"
|
|
|
|
"github.com/hay-kot/homebox/backend/ent/attachment"
|
2022-09-12 22:47:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AttachmentRepo is a repository for Attachments table that links Items to Documents
|
|
|
|
// While also specifying the type of the attachment. This _ONLY_ provides basic Create Update
|
|
|
|
// And Delete operations. For accessing the actual documents, use the Items repository since it
|
|
|
|
// provides the attachments with the documents.
|
|
|
|
type AttachmentRepo struct {
|
|
|
|
db *ent.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AttachmentRepo) Create(ctx context.Context, itemId, docId uuid.UUID, typ attachment.Type) (*ent.Attachment, error) {
|
|
|
|
return r.db.Attachment.Create().
|
|
|
|
SetType(typ).
|
|
|
|
SetDocumentID(docId).
|
|
|
|
SetItemID(itemId).
|
|
|
|
Save(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AttachmentRepo) Get(ctx context.Context, id uuid.UUID) (*ent.Attachment, error) {
|
|
|
|
return r.db.Attachment.
|
|
|
|
Query().
|
|
|
|
Where(attachment.ID(id)).
|
|
|
|
WithItem().
|
|
|
|
WithDocument().
|
|
|
|
Only(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AttachmentRepo) Update(ctx context.Context, itemId uuid.UUID, typ attachment.Type) (*ent.Attachment, error) {
|
2022-09-24 19:33:38 +00:00
|
|
|
itm, err := r.db.Attachment.UpdateOneID(itemId).
|
2022-09-12 22:47:27 +00:00
|
|
|
SetType(typ).
|
|
|
|
Save(ctx)
|
2022-09-24 19:33:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return r.Get(ctx, itm.ID)
|
2022-09-12 22:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *AttachmentRepo) Delete(ctx context.Context, id uuid.UUID) error {
|
|
|
|
return r.db.Attachment.DeleteOneID(id).Exec(ctx)
|
|
|
|
}
|