diff --git a/api/contents.go b/api/contents.go index b839ca3..259046c 100644 --- a/api/contents.go +++ b/api/contents.go @@ -113,29 +113,34 @@ func ConfigureContentsRouter(e *echo.Group, dldm *core.DeltaDM) { } } - for _, cnt := range content { - // Check for bad data - if cnt.CommP == "" || cnt.PayloadCID == "" || cnt.PaddedSize == 0 || cnt.Size == 0 { - results.Fail = append(results.Fail, cnt.CommP) - continue - } + dldm.DB.Transaction(func(tx *gorm.DB) error { + for _, cnt := range content { + // Check for bad data + if cnt.CommP == "" || cnt.PayloadCID == "" || cnt.PaddedSize == 0 || cnt.Size == 0 { + results.Fail = append(results.Fail, cnt.CommP) + continue + } - cnt.DatasetID = dataset.ID + cnt.DatasetID = dataset.ID - err := dldm.DB.Create(&cnt).Error - if err != nil { - results.Fail = append(results.Fail, cnt.CommP) - continue - } + err := tx.Create(&cnt).Error + if err != nil { + results.Fail = append(results.Fail, cnt.CommP) + continue + } - results.Success = append(results.Success, cnt.CommP) - } + results.Success = append(results.Success, cnt.CommP) + } + return nil + }) return c.JSON(http.StatusOK, results) }) contents.POST("", func(c echo.Context) error { var content []ContentCollection + var datasets []db.Dataset + results := struct { Success []string `json:"success"` Fail []string `json:"fail"` @@ -148,47 +153,56 @@ func ConfigureContentsRouter(e *echo.Group, dldm *core.DeltaDM) { return err } - cache := make(map[string]uint) + if err := dldm.DB.Model(&db.Dataset{}).Find(&datasets).Error; err != nil { + return err + } + + dldm.DB.Transaction(func(tx *gorm.DB) error { + for _, cnt := range content { + // Check for bad data + if cnt.CommP == "" || cnt.PayloadCID == "" || cnt.PaddedSize == 0 || cnt.Size == 0 || + cnt.Collection == "" || cnt.ContentLocation == "" { + log.Debugf("Missing required parameters for commP: %s", cnt.CommP) + results.Fail = append(results.Fail, cnt.CommP) + continue + } - for _, cnt := range content { - var dataset db.Dataset - // Check for bad data - if cnt.CommP == "" || cnt.PayloadCID == "" || cnt.PaddedSize == 0 || cnt.Size == 0 || - cnt.Collection == "" || cnt.ContentLocation == "" { - log.Debugf("Missing required parameters for commP: %s", cnt.CommP) - results.Fail = append(results.Fail, cnt.CommP) - continue - } + // Check if dataset exists + var collection *db.Dataset + for _, dataset := range datasets { + if dataset.Name == cnt.Collection { + collection = &dataset + break + } + } - // Check if dataset exists - if cache[cnt.Collection] == 0 { - if dldm.DB.Model(&db.Dataset{}).Where("name = ?", cnt.Collection).First(&dataset).Error != nil { - log.Debugf("Collection not found for: %s", cnt.Collection) + if collection == nil { + log.Debugf("Collection %s not found for Commp: %s", cnt.Collection, cnt.CommP) results.Fail = append(results.Fail, cnt.CommP) continue } - } - cache[cnt.Collection] = dataset.ID + dbc := db.Content{ + CommP: cnt.CommP, + PayloadCID: cnt.PayloadCID, + PaddedSize: cnt.PaddedSize, + Size: cnt.Size, + DatasetID: collection.ID, + ContentLocation: cnt.ContentLocation, + } - dbc := db.Content{ - CommP: cnt.CommP, - PayloadCID: cnt.PayloadCID, - PaddedSize: cnt.PaddedSize, - Size: cnt.Size, - DatasetID: dataset.ID, - ContentLocation: cnt.ContentLocation, - } + err := tx.Create(&dbc).Error + if err != nil { + log.Debugf("Could not create DB record: %s", err.Error()) + results.Fail = append(results.Fail, cnt.CommP) + continue + } - err := dldm.DB.Create(&dbc).Error - if err != nil { - log.Debugf("Could not create DB record: %s", err.Error()) - results.Fail = append(results.Fail, cnt.CommP) - continue + results.Success = append(results.Success, cnt.CommP) } - results.Success = append(results.Success, cnt.CommP) - } + return nil + }) return c.JSON(http.StatusOK, results) })