diff --git a/backend/internal/data/repo/repo_item_attachments.go b/backend/internal/data/repo/repo_item_attachments.go index c301b60b..9eba2225 100644 --- a/backend/internal/data/repo/repo_item_attachments.go +++ b/backend/internal/data/repo/repo_item_attachments.go @@ -91,9 +91,26 @@ func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemC SetItemID(itemID). SetTitle(doc.Title) - // Autoset primary to true if this is the first attachment - // that is of type photo - if typ == attachment.TypePhoto { + if typ == attachment.TypePhoto && primary { + bldr = bldr.SetPrimary(true) + err := r.db.Attachment.Update(). + Where( + attachment.HasItemWith(item.ID(itemID)), + attachment.IDNEQ(bldrId), + ). + SetPrimary(false). + Exec(ctx) + if err != nil { + log.Err(err).Msg("failed to remove primary from other attachments") + err := tx.Rollback() + if err != nil { + return nil, err + } + return nil, err + } + } else if typ == attachment.TypePhoto { + // Autoset primary to true if this is the first attachment + // that is of type photo cnt, err := tx.Attachment.Query(). Where( attachment.HasItemWith(item.ID(itemID)), @@ -112,23 +129,6 @@ func (r *AttachmentRepo) Create(ctx context.Context, itemID uuid.UUID, doc ItemC if cnt == 0 { bldr = bldr.SetPrimary(true) } - } else if typ == attachment.TypePhoto && primary { - bldr = bldr.SetPrimary(true) - err := r.db.Attachment.Update(). - Where( - attachment.HasItemWith(item.ID(itemID)), - attachment.IDNEQ(bldrId), - ). - SetPrimary(false). - Exec(ctx) - if err != nil { - log.Err(err).Msg("failed to remove primary from other attachments") - err := tx.Rollback() - if err != nil { - return nil, err - } - return nil, err - } } // Get the group ID for the item the attachment is being created for diff --git a/frontend/components/Item/CreateModal.vue b/frontend/components/Item/CreateModal.vue index 2c62a531..191e3919 100644 --- a/frontend/components/Item/CreateModal.vue +++ b/frontend/components/Item/CreateModal.vue @@ -95,8 +95,7 @@
Rotate photo
- - +{{ photo.photoName }}
@@ -135,8 +134,8 @@ import MdiPackageVariantClosed from "~icons/mdi/package-variant-closed"; import MdiDelete from "~icons/mdi/delete"; import MdiRotateClockwise from "~icons/mdi/rotate-clockwise"; - // import MdiStarOutline from "~icons/mdi/star-outline"; - // import MdiStar from "~icons/mdi/star"; + import MdiStarOutline from "~icons/mdi/star-outline"; + import MdiStar from "~icons/mdi/star"; import { AttachmentTypes } from "~~/lib/api/types/non-generated"; import { useDialog, useDialogHotkey } from "~/components/ui/dialog-provider"; import LabelSelector from "~/components/Label/Selector.vue"; @@ -196,16 +195,12 @@ form.photos.splice(index, 1); } - // TODO: actually set the primary when adding item + function setPrimary(index: number) { + const primary = form.photos.findIndex(p => p.primary); - // function setPrimary(index: number) { - // const primary = form.photos.findIndex(p => p.primary); - - // if (primary !== -1) form.photos[primary].primary = false; - // if (primary !== index) form.photos[index].primary = true; - - // toast.error("Currently this does not do anything, the first photo will always be primary"); - // } + if (primary !== -1) form.photos[primary].primary = false; + if (primary !== index) form.photos[index].primary = true; + } function previewImage(event: Event) { const input = event.target as HTMLInputElement; @@ -285,7 +280,8 @@ data.id, photo.file, photo.photoName, - AttachmentTypes.Photo + AttachmentTypes.Photo, + photo.primary ); if (attachError) { diff --git a/frontend/lib/api/classes/items.ts b/frontend/lib/api/classes/items.ts index a19ebe6b..0778b04f 100644 --- a/frontend/lib/api/classes/items.ts +++ b/frontend/lib/api/classes/items.ts @@ -32,13 +32,14 @@ export type ItemsQuery = { }; export class AttachmentsAPI extends BaseAPI { - add(id: string, file: File | Blob, filename: string, type: AttachmentTypes | null = null) { + add(id: string, file: File | Blob, filename: string, type: AttachmentTypes | null = null, primary: boolean = false) { const formData = new FormData(); formData.append("file", file); if (type) { formData.append("type", type); } formData.append("name", filename); + formData.append("primary", primary.toString()); return this.http.post