feat: primary images (#576)

* add support for primary images

* fix locked loading state issue

* add action to auto-set images

Former-commit-id: 318b8be192
This commit is contained in:
Hayden 2023-10-06 21:51:08 -05:00 committed by GitHub
parent ce16b37b97
commit b20c88e256
34 changed files with 649 additions and 207 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/google/uuid"
"github.com/hay-kot/homebox/backend/internal/core/services/reporting/eventbus"
"github.com/hay-kot/homebox/backend/internal/data/ent"
"github.com/hay-kot/homebox/backend/internal/data/ent/attachment"
"github.com/hay-kot/homebox/backend/internal/data/ent/group"
"github.com/hay-kot/homebox/backend/internal/data/ent/item"
"github.com/hay-kot/homebox/backend/internal/data/ent/itemfield"
@ -125,6 +126,8 @@ type (
// Edges
Location *LocationSummary `json:"location,omitempty" extensions:"x-nullable,x-omitempty"`
Labels []LabelSummary `json:"labels"`
ImageID *uuid.UUID `json:"imageId,omitempty"`
}
ItemOut struct {
@ -174,6 +177,16 @@ func mapItemSummary(item *ent.Item) ItemSummary {
labels = mapEach(item.Edges.Label, mapLabelSummary)
}
var imageID *uuid.UUID
if item.Edges.Attachments != nil {
for _, a := range item.Edges.Attachments {
if a.Primary && a.Edges.Document != nil {
imageID = &a.ID
break
}
}
}
return ItemSummary{
ID: item.ID,
Name: item.Name,
@ -191,6 +204,7 @@ func mapItemSummary(item *ent.Item) ItemSummary {
// Warranty
Insured: item.Insured,
ImageID: imageID,
}
}
@ -407,7 +421,13 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
qb = qb.
WithLabel().
WithLocation()
WithLocation().
WithAttachments(func(aq *ent.AttachmentQuery) {
aq.Where(
attachment.Primary(true),
).
WithDocument()
})
if q.Page != -1 || q.PageSize != -1 {
qb = qb.
@ -533,13 +553,13 @@ func (e *ItemsRepository) Create(ctx context.Context, gid uuid.UUID, data ItemCr
}
func (e *ItemsRepository) Delete(ctx context.Context, id uuid.UUID) error {
err := e.db.Item.DeleteOneID(id).Exec(ctx)
if err != nil {
return err
}
err := e.db.Item.DeleteOneID(id).Exec(ctx)
if err != nil {
return err
}
e.publishMutationEvent(id)
return nil
e.publishMutationEvent(id)
return nil
}
func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID) error {
@ -549,12 +569,11 @@ func (e *ItemsRepository) DeleteByGroup(ctx context.Context, gid, id uuid.UUID)
item.ID(id),
item.HasGroupWith(group.ID(gid)),
).Exec(ctx)
if err != nil {
return err
}
if err != nil {
return err
}
e.publishMutationEvent(gid)
e.publishMutationEvent(gid)
return err
}
@ -670,7 +689,7 @@ func (e *ItemsRepository) UpdateByGroup(ctx context.Context, GID uuid.UUID, data
}
}
e.publishMutationEvent(GID)
e.publishMutationEvent(GID)
return e.GetOne(ctx, data.ID)
}
@ -709,7 +728,7 @@ func (e *ItemsRepository) Patch(ctx context.Context, GID, ID uuid.UUID, data Ite
q.SetQuantity(*data.Quantity)
}
e.publishMutationEvent(GID)
e.publishMutationEvent(GID)
return q.Exec(ctx)
}
@ -822,3 +841,51 @@ func (e *ItemsRepository) ZeroOutTimeFields(ctx context.Context, GID uuid.UUID)
return updated, nil
}
func (e *ItemsRepository) SetPrimaryPhotos(ctx context.Context, GID uuid.UUID) (int, error) {
// All items where there is no primary photo
itemIDs, err := e.db.Item.Query().
Where(
item.HasGroupWith(group.ID(GID)),
item.HasAttachmentsWith(
attachment.Not(
attachment.And(
attachment.Primary(true),
attachment.TypeEQ(attachment.TypePhoto),
),
),
),
).
IDs(ctx)
if err != nil {
return -1, err
}
updated := 0
for _, id := range itemIDs {
// Find the first photo attachment
a, err := e.db.Attachment.Query().
Where(
attachment.HasItemWith(item.ID(id)),
attachment.TypeEQ(attachment.TypePhoto),
attachment.Primary(false),
).
First(ctx)
if err != nil {
return updated, err
}
// Set it as primary
_, err = e.db.Attachment.UpdateOne(a).
SetPrimary(true).
Save(ctx)
if err != nil {
return updated, err
}
updated++
}
return updated, nil
}