mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 13:23:14 +01:00
fix: use tx for duplicate (#1059)
This commit is contained in:
@@ -319,8 +319,13 @@ func (e *ItemsRepository) publishMutationEvent(gid uuid.UUID) {
|
||||
}
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) {
|
||||
q := e.db.Item.Query().Where(where...)
|
||||
func (e *ItemsRepository) getOneTx(ctx context.Context, tx *ent.Tx, where ...predicate.Item) (ItemOut, error) {
|
||||
var q *ent.ItemQuery
|
||||
if tx != nil {
|
||||
q = tx.Item.Query().Where(where...)
|
||||
} else {
|
||||
q = e.db.Item.Query().Where(where...)
|
||||
}
|
||||
|
||||
return mapItemOutErr(q.
|
||||
WithFields().
|
||||
@@ -333,6 +338,10 @@ func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (
|
||||
)
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) getOne(ctx context.Context, where ...predicate.Item) (ItemOut, error) {
|
||||
return e.getOneTx(ctx, nil, where...)
|
||||
}
|
||||
|
||||
// GetOne returns a single item by ID. If the item does not exist, an error is returned.
|
||||
// See also: GetOneByGroup to ensure that the item belongs to a specific group.
|
||||
func (e *ItemsRepository) GetOne(ctx context.Context, id uuid.UUID) (ItemOut, error) {
|
||||
@@ -577,12 +586,21 @@ func (e *ItemsRepository) GetAllZeroAssetID(ctx context.Context, gid uuid.UUID)
|
||||
return mapItemsSummaryErr(q.All(ctx))
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, gid uuid.UUID) (AssetID, error) {
|
||||
q := e.db.Item.Query().Where(
|
||||
func (e *ItemsRepository) GetHighestAssetIDTx(ctx context.Context, tx *ent.Tx, gid uuid.UUID) (AssetID, error) {
|
||||
var q *ent.ItemQuery
|
||||
if tx != nil {
|
||||
q = tx.Item.Query().Where(
|
||||
item.HasGroupWith(group.ID(gid)),
|
||||
).Order(
|
||||
ent.Desc(item.FieldAssetID),
|
||||
).Limit(1)
|
||||
} else {
|
||||
q = e.db.Item.Query().Where(
|
||||
item.HasGroupWith(group.ID(gid)),
|
||||
).Order(
|
||||
ent.Desc(item.FieldAssetID),
|
||||
).Limit(1)
|
||||
}
|
||||
|
||||
result, err := q.First(ctx)
|
||||
if err != nil {
|
||||
@@ -595,6 +613,10 @@ func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, gid uuid.UUID)
|
||||
return AssetID(result.AssetID), nil
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) GetHighestAssetID(ctx context.Context, gid uuid.UUID) (AssetID, error) {
|
||||
return e.GetHighestAssetIDTx(ctx, nil, gid)
|
||||
}
|
||||
|
||||
func (e *ItemsRepository) SetAssetID(ctx context.Context, gid uuid.UUID, id uuid.UUID, assetID AssetID) error {
|
||||
q := e.db.Item.Update().Where(
|
||||
item.HasGroupWith(group.ID(gid)),
|
||||
@@ -1166,12 +1188,12 @@ func (e *ItemsRepository) Duplicate(ctx context.Context, gid, id uuid.UUID, opti
|
||||
}()
|
||||
|
||||
// Get the original item with all its data
|
||||
originalItem, err := e.getOne(ctx, item.ID(id), item.HasGroupWith(group.ID(gid)))
|
||||
originalItem, err := e.getOneTx(ctx, tx, item.ID(id), item.HasGroupWith(group.ID(gid)))
|
||||
if err != nil {
|
||||
return ItemOut{}, err
|
||||
}
|
||||
|
||||
nextAssetID, err := e.GetHighestAssetID(ctx, gid)
|
||||
nextAssetID, err := e.GetHighestAssetIDTx(ctx, tx, gid)
|
||||
if err != nil {
|
||||
return ItemOut{}, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user