// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"
	"time"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/omit"
	"github.com/google/uuid"
	enums "source.gleipnir.technology/Gleipnir/nidus-sync/db/enums"
)

// NoteImageDatum is an object representing the database table.
type NoteImageDatum struct {
	Created          time.Time           `db:"created" `
	NoteImageVersion int32               `db:"note_image_version,pk" `
	NoteImageUUID    uuid.UUID           `db:"note_image_uuid,pk" `
	Type             enums.Audiodatatype `db:"type_,pk" `

	R noteImageDatumR `db:"-" `
}

// NoteImageDatumSlice is an alias for a slice of pointers to NoteImageDatum.
// This should almost always be used instead of []*NoteImageDatum.
type NoteImageDatumSlice []*NoteImageDatum

// NoteImageData contains methods to work with the note_image_data table
var NoteImageData = psql.NewTablex[*NoteImageDatum, NoteImageDatumSlice, *NoteImageDatumSetter]("", "note_image_data", buildNoteImageDatumColumns("note_image_data"))

// NoteImageDataQuery is a query on the note_image_data table
type NoteImageDataQuery = *psql.ViewQuery[*NoteImageDatum, NoteImageDatumSlice]

// noteImageDatumR is where relationships are stored.
type noteImageDatumR struct {
	NoteImage *NoteImage // note_image_data.note_image_data_note_image_version_note_image_uuid_fkey
}

func buildNoteImageDatumColumns(alias string) noteImageDatumColumns {
	return noteImageDatumColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"created", "note_image_version", "note_image_uuid", "type_",
		).WithParent("note_image_data"),
		tableAlias:       alias,
		Created:          psql.Quote(alias, "created"),
		NoteImageVersion: psql.Quote(alias, "note_image_version"),
		NoteImageUUID:    psql.Quote(alias, "note_image_uuid"),
		Type:             psql.Quote(alias, "type_"),
	}
}

type noteImageDatumColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	Created          psql.Expression
	NoteImageVersion psql.Expression
	NoteImageUUID    psql.Expression
	Type             psql.Expression
}

func (c noteImageDatumColumns) Alias() string {
	return c.tableAlias
}

func (noteImageDatumColumns) AliasedAs(alias string) noteImageDatumColumns {
	return buildNoteImageDatumColumns(alias)
}

// NoteImageDatumSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type NoteImageDatumSetter struct {
	Created          omit.Val[time.Time]           `db:"created" `
	NoteImageVersion omit.Val[int32]               `db:"note_image_version,pk" `
	NoteImageUUID    omit.Val[uuid.UUID]           `db:"note_image_uuid,pk" `
	Type             omit.Val[enums.Audiodatatype] `db:"type_,pk" `
}

func (s NoteImageDatumSetter) SetColumns() []string {
	vals := make([]string, 0, 4)
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.NoteImageVersion.IsValue() {
		vals = append(vals, "note_image_version")
	}
	if s.NoteImageUUID.IsValue() {
		vals = append(vals, "note_image_uuid")
	}
	if s.Type.IsValue() {
		vals = append(vals, "type_")
	}
	return vals
}

func (s NoteImageDatumSetter) Overwrite(t *NoteImageDatum) {
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.NoteImageVersion.IsValue() {
		t.NoteImageVersion = s.NoteImageVersion.MustGet()
	}
	if s.NoteImageUUID.IsValue() {
		t.NoteImageUUID = s.NoteImageUUID.MustGet()
	}
	if s.Type.IsValue() {
		t.Type = s.Type.MustGet()
	}
}

func (s *NoteImageDatumSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return NoteImageData.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		vals := make([]bob.Expression, 4)
		if s.Created.IsValue() {
			vals[0] = psql.Arg(s.Created.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

		if s.NoteImageVersion.IsValue() {
			vals[1] = psql.Arg(s.NoteImageVersion.MustGet())
		} else {
			vals[1] = psql.Raw("DEFAULT")
		}

		if s.NoteImageUUID.IsValue() {
			vals[2] = psql.Arg(s.NoteImageUUID.MustGet())
		} else {
			vals[2] = psql.Raw("DEFAULT")
		}

		if s.Type.IsValue() {
			vals[3] = psql.Arg(s.Type.MustGet())
		} else {
			vals[3] = psql.Raw("DEFAULT")
		}

		return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
	}))
}

func (s NoteImageDatumSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions()...)
}

func (s NoteImageDatumSetter) Expressions(prefix ...string) []bob.Expression {
	exprs := make([]bob.Expression, 0, 4)

	if s.Created.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "created")...),
			psql.Arg(s.Created),
		}})
	}

	if s.NoteImageVersion.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "note_image_version")...),
			psql.Arg(s.NoteImageVersion),
		}})
	}

	if s.NoteImageUUID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "note_image_uuid")...),
			psql.Arg(s.NoteImageUUID),
		}})
	}

	if s.Type.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "type_")...),
			psql.Arg(s.Type),
		}})
	}

	return exprs
}

// FindNoteImageDatum retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindNoteImageDatum(ctx context.Context, exec bob.Executor, NoteImageVersionPK int32, NoteImageUUIDPK uuid.UUID, TypePK enums.Audiodatatype, cols ...string) (*NoteImageDatum, error) {
	if len(cols) == 0 {
		return NoteImageData.Query(
			sm.Where(NoteImageData.Columns.NoteImageVersion.EQ(psql.Arg(NoteImageVersionPK))),
			sm.Where(NoteImageData.Columns.NoteImageUUID.EQ(psql.Arg(NoteImageUUIDPK))),
			sm.Where(NoteImageData.Columns.Type.EQ(psql.Arg(TypePK))),
		).One(ctx, exec)
	}

	return NoteImageData.Query(
		sm.Where(NoteImageData.Columns.NoteImageVersion.EQ(psql.Arg(NoteImageVersionPK))),
		sm.Where(NoteImageData.Columns.NoteImageUUID.EQ(psql.Arg(NoteImageUUIDPK))),
		sm.Where(NoteImageData.Columns.Type.EQ(psql.Arg(TypePK))),
		sm.Columns(NoteImageData.Columns.Only(cols...)),
	).One(ctx, exec)
}

// NoteImageDatumExists checks the presence of a single record by primary key
func NoteImageDatumExists(ctx context.Context, exec bob.Executor, NoteImageVersionPK int32, NoteImageUUIDPK uuid.UUID, TypePK enums.Audiodatatype) (bool, error) {
	return NoteImageData.Query(
		sm.Where(NoteImageData.Columns.NoteImageVersion.EQ(psql.Arg(NoteImageVersionPK))),
		sm.Where(NoteImageData.Columns.NoteImageUUID.EQ(psql.Arg(NoteImageUUIDPK))),
		sm.Where(NoteImageData.Columns.Type.EQ(psql.Arg(TypePK))),
	).Exists(ctx, exec)
}

// AfterQueryHook is called after NoteImageDatum is retrieved from the database
func (o *NoteImageDatum) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = NoteImageData.AfterSelectHooks.RunHooks(ctx, exec, NoteImageDatumSlice{o})
	case bob.QueryTypeInsert:
		ctx, err = NoteImageData.AfterInsertHooks.RunHooks(ctx, exec, NoteImageDatumSlice{o})
	case bob.QueryTypeUpdate:
		ctx, err = NoteImageData.AfterUpdateHooks.RunHooks(ctx, exec, NoteImageDatumSlice{o})
	case bob.QueryTypeDelete:
		ctx, err = NoteImageData.AfterDeleteHooks.RunHooks(ctx, exec, NoteImageDatumSlice{o})
	}

	return err
}

// primaryKeyVals returns the primary key values of the NoteImageDatum
func (o *NoteImageDatum) primaryKeyVals() bob.Expression {
	return psql.ArgGroup(
		o.NoteImageVersion,
		o.NoteImageUUID,
		o.Type,
	)
}

func (o *NoteImageDatum) pkEQ() dialect.Expression {
	return psql.Group(psql.Quote("note_image_data", "note_image_version"), psql.Quote("note_image_data", "note_image_uuid"), psql.Quote("note_image_data", "type_")).EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the NoteImageDatum
func (o *NoteImageDatum) Update(ctx context.Context, exec bob.Executor, s *NoteImageDatumSetter) error {
	v, err := NoteImageData.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
	if err != nil {
		return err
	}

	o.R = v.R
	*o = *v

	return nil
}

// Delete deletes a single NoteImageDatum record with an executor
func (o *NoteImageDatum) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := NoteImageData.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

// Reload refreshes the NoteImageDatum using the executor
func (o *NoteImageDatum) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := NoteImageData.Query(
		sm.Where(NoteImageData.Columns.NoteImageVersion.EQ(psql.Arg(o.NoteImageVersion))),
		sm.Where(NoteImageData.Columns.NoteImageUUID.EQ(psql.Arg(o.NoteImageUUID))),
		sm.Where(NoteImageData.Columns.Type.EQ(psql.Arg(o.Type))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

// AfterQueryHook is called after NoteImageDatumSlice is retrieved from the database
func (o NoteImageDatumSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = NoteImageData.AfterSelectHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeInsert:
		ctx, err = NoteImageData.AfterInsertHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeUpdate:
		ctx, err = NoteImageData.AfterUpdateHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeDelete:
		ctx, err = NoteImageData.AfterDeleteHooks.RunHooks(ctx, exec, o)
	}

	return err
}

func (o NoteImageDatumSlice) pkIN() dialect.Expression {
	if len(o) == 0 {
		return psql.Raw("NULL")
	}

	return psql.Group(psql.Quote("note_image_data", "note_image_version"), psql.Quote("note_image_data", "note_image_uuid"), psql.Quote("note_image_data", "type_")).In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o NoteImageDatumSlice) copyMatchingRows(from ...*NoteImageDatum) {
	for i, old := range o {
		for _, new := range from {
			if new.NoteImageVersion != old.NoteImageVersion {
				continue
			}
			if new.NoteImageUUID != old.NoteImageUUID {
				continue
			}
			if new.Type != old.Type {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o NoteImageDatumSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return NoteImageData.BeforeUpdateHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *NoteImageDatum:
				o.copyMatchingRows(retrieved)
			case []*NoteImageDatum:
				o.copyMatchingRows(retrieved...)
			case NoteImageDatumSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a NoteImageDatum or a slice of NoteImageDatum
				// then run the AfterUpdateHooks on the slice
				_, err = NoteImageData.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o NoteImageDatumSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
	return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return NoteImageData.BeforeDeleteHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *NoteImageDatum:
				o.copyMatchingRows(retrieved)
			case []*NoteImageDatum:
				o.copyMatchingRows(retrieved...)
			case NoteImageDatumSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a NoteImageDatum or a slice of NoteImageDatum
				// then run the AfterDeleteHooks on the slice
				_, err = NoteImageData.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

func (o NoteImageDatumSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals NoteImageDatumSetter) error {
	if len(o) == 0 {
		return nil
	}

	_, err := NoteImageData.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
	return err
}

func (o NoteImageDatumSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	_, err := NoteImageData.Delete(o.DeleteMod()).Exec(ctx, exec)
	return err
}

func (o NoteImageDatumSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	o2, err := NoteImageData.Query(sm.Where(o.pkIN())).All(ctx, exec)
	if err != nil {
		return err
	}

	o.copyMatchingRows(o2...)

	return nil
}

// NoteImage starts a query for related objects on note_image
func (o *NoteImageDatum) NoteImage(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	return NoteImages.Query(append(mods,
		sm.Where(NoteImages.Columns.Version.EQ(psql.Arg(o.NoteImageVersion))), sm.Where(NoteImages.Columns.UUID.EQ(psql.Arg(o.NoteImageUUID))),
	)...)
}

func (os NoteImageDatumSlice) NoteImage(mods ...bob.Mod[*dialect.SelectQuery]) NoteImagesQuery {
	pkNoteImageVersion := make(pgtypes.Array[int32], 0, len(os))

	pkNoteImageUUID := make(pgtypes.Array[uuid.UUID], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkNoteImageVersion = append(pkNoteImageVersion, o.NoteImageVersion)
		pkNoteImageUUID = append(pkNoteImageUUID, o.NoteImageUUID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkNoteImageVersion), "integer[]")),
		psql.F("unnest", psql.Cast(psql.Arg(pkNoteImageUUID), "uuid[]")),
	))

	return NoteImages.Query(append(mods,
		sm.Where(psql.Group(NoteImages.Columns.Version, NoteImages.Columns.UUID).OP("IN", PKArgExpr)),
	)...)
}

func attachNoteImageDatumNoteImage0(ctx context.Context, exec bob.Executor, count int, noteImageDatum0 *NoteImageDatum, noteImage1 *NoteImage) (*NoteImageDatum, error) {
	setter := &NoteImageDatumSetter{
		NoteImageVersion: omit.From(noteImage1.Version),
		NoteImageUUID:    omit.From(noteImage1.UUID),
	}

	err := noteImageDatum0.Update(ctx, exec, setter)
	if err != nil {
		return nil, fmt.Errorf("attachNoteImageDatumNoteImage0: %w", err)
	}

	return noteImageDatum0, nil
}

func (noteImageDatum0 *NoteImageDatum) InsertNoteImage(ctx context.Context, exec bob.Executor, related *NoteImageSetter) error {
	var err error

	noteImage1, err := NoteImages.Insert(related).One(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}

	_, err = attachNoteImageDatumNoteImage0(ctx, exec, 1, noteImageDatum0, noteImage1)
	if err != nil {
		return err
	}

	noteImageDatum0.R.NoteImage = noteImage1

	noteImage1.R.NoteImageData = append(noteImage1.R.NoteImageData, noteImageDatum0)

	return nil
}

func (noteImageDatum0 *NoteImageDatum) AttachNoteImage(ctx context.Context, exec bob.Executor, noteImage1 *NoteImage) error {
	var err error

	_, err = attachNoteImageDatumNoteImage0(ctx, exec, 1, noteImageDatum0, noteImage1)
	if err != nil {
		return err
	}

	noteImageDatum0.R.NoteImage = noteImage1

	noteImage1.R.NoteImageData = append(noteImage1.R.NoteImageData, noteImageDatum0)

	return nil
}

type noteImageDatumWhere[Q psql.Filterable] struct {
	Created          psql.WhereMod[Q, time.Time]
	NoteImageVersion psql.WhereMod[Q, int32]
	NoteImageUUID    psql.WhereMod[Q, uuid.UUID]
	Type             psql.WhereMod[Q, enums.Audiodatatype]
}

func (noteImageDatumWhere[Q]) AliasedAs(alias string) noteImageDatumWhere[Q] {
	return buildNoteImageDatumWhere[Q](buildNoteImageDatumColumns(alias))
}

func buildNoteImageDatumWhere[Q psql.Filterable](cols noteImageDatumColumns) noteImageDatumWhere[Q] {
	return noteImageDatumWhere[Q]{
		Created:          psql.Where[Q, time.Time](cols.Created),
		NoteImageVersion: psql.Where[Q, int32](cols.NoteImageVersion),
		NoteImageUUID:    psql.Where[Q, uuid.UUID](cols.NoteImageUUID),
		Type:             psql.Where[Q, enums.Audiodatatype](cols.Type),
	}
}

func (o *NoteImageDatum) Preload(name string, retrieved any) error {
	if o == nil {
		return nil
	}

	switch name {
	case "NoteImage":
		rel, ok := retrieved.(*NoteImage)
		if !ok {
			return fmt.Errorf("noteImageDatum cannot load %T as %q", retrieved, name)
		}

		o.R.NoteImage = rel

		if rel != nil {
			rel.R.NoteImageData = NoteImageDatumSlice{o}
		}
		return nil
	default:
		return fmt.Errorf("noteImageDatum has no relationship %q", name)
	}
}

type noteImageDatumPreloader struct {
	NoteImage func(...psql.PreloadOption) psql.Preloader
}

func buildNoteImageDatumPreloader() noteImageDatumPreloader {
	return noteImageDatumPreloader{
		NoteImage: func(opts ...psql.PreloadOption) psql.Preloader {
			return psql.Preload[*NoteImage, NoteImageSlice](psql.PreloadRel{
				Name: "NoteImage",
				Sides: []psql.PreloadSide{
					{
						From:        NoteImageData,
						To:          NoteImages,
						FromColumns: []string{"note_image_version", "note_image_uuid"},
						ToColumns:   []string{"version", "uuid"},
					},
				},
			}, NoteImages.Columns.Names(), opts...)
		},
	}
}

type noteImageDatumThenLoader[Q orm.Loadable] struct {
	NoteImage func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildNoteImageDatumThenLoader[Q orm.Loadable]() noteImageDatumThenLoader[Q] {
	type NoteImageLoadInterface interface {
		LoadNoteImage(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return noteImageDatumThenLoader[Q]{
		NoteImage: thenLoadBuilder[Q](
			"NoteImage",
			func(ctx context.Context, exec bob.Executor, retrieved NoteImageLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNoteImage(ctx, exec, mods...)
			},
		),
	}
}

// LoadNoteImage loads the noteImageDatum's NoteImage into the .R struct
func (o *NoteImageDatum) LoadNoteImage(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.NoteImage = nil

	related, err := o.NoteImage(mods...).One(ctx, exec)
	if err != nil {
		return err
	}

	related.R.NoteImageData = NoteImageDatumSlice{o}

	o.R.NoteImage = related
	return nil
}

// LoadNoteImage loads the noteImageDatum's NoteImage into the .R struct
func (os NoteImageDatumSlice) LoadNoteImage(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	noteImages, err := os.NoteImage(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range noteImages {

			if !(o.NoteImageVersion == rel.Version) {
				continue
			}

			if !(o.NoteImageUUID == rel.UUID) {
				continue
			}

			rel.R.NoteImageData = append(rel.R.NoteImageData, o)

			o.R.NoteImage = rel
			break
		}
	}

	return nil
}
