Skip to content

Commit cae2988

Browse files
committed
get connection obj and insert if not present
1 parent 99a5da3 commit cae2988

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

code/go/0chain.net/blobbercore/allocation/allocationchange.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,34 @@ func GetAllocationChanges(ctx context.Context, connectionID, allocationID, clien
162162
return nil, err
163163
}
164164

165+
func GetConnectionObj(ctx context.Context, connectionID, allocationID, clientID string) (*AllocationChangeCollector, error) {
166+
cc := &AllocationChangeCollector{}
167+
db := datastore.GetStore().GetTransaction(ctx)
168+
err := db.Where("id = ? and allocation_id = ? and client_id = ?",
169+
connectionID,
170+
allocationID,
171+
clientID,
172+
).Take(cc).Error
173+
174+
if err == nil {
175+
return cc, nil
176+
}
177+
178+
if err == gorm.ErrRecordNotFound {
179+
cc.ID = connectionID
180+
cc.AllocationID = allocationID
181+
cc.ClientID = clientID
182+
cc.Status = NewConnection
183+
err = cc.Save(ctx)
184+
if err != nil {
185+
return nil, err
186+
}
187+
return cc, nil
188+
}
189+
190+
return nil, err
191+
}
192+
165193
func (cc *AllocationChangeCollector) AddChange(allocationChange *AllocationChange, changeProcessor AllocationChangeProcessor) {
166194
cc.AllocationChanges = append(cc.AllocationChanges, changeProcessor)
167195
allocationChange.Input, _ = changeProcessor.Marshal()

code/go/0chain.net/blobbercore/handler/object_operation_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ func (fsh *StorageHandler) WriteFile(ctx context.Context, r *http.Request) (*all
12971297
}
12981298
// Update/Save the change
12991299
if res.UpdateChange {
1300-
dbConnectionObj, err := allocation.GetAllocationChanges(ctx, connectionID, allocationID, clientID)
1300+
dbConnectionObj, err := allocation.GetConnectionObj(ctx, connectionID, allocationID, clientID)
13011301
if err != nil {
13021302
return nil, err
13031303
}

0 commit comments

Comments
 (0)