Skip to content

Commit

Permalink
fix(obligation): fix FirstOrCreate in obligation creation
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Mishra <[email protected]>
  • Loading branch information
GMishx committed Jan 11, 2024
1 parent 55dbde0 commit 0f04b95
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ func CreateLicense(c *gin.Context) {
Risk: input.Risk,
}

result := db.DB.FirstOrCreate(&license)
result := db.DB.
Where(&models.LicenseDB{Shortname: license.Shortname}).
FirstOrCreate(&license)
if result.RowsAffected == 0 {

er := models.LicenseError{
Status: http.StatusConflict,
Message: "can not create license with same shortname",
Expand Down
14 changes: 8 additions & 6 deletions pkg/api/obligations.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,17 @@ func CreateObligation(c *gin.Context) {
Active: input.Active,
}

result := db.DB.FirstOrCreate(&obligation)
result := db.DB.
Where(&models.Obligation{Topic: obligation.Topic}).
Or(&models.Obligation{Md5: obligation.Md5}).
FirstOrCreate(&obligation)

fmt.Print(obligation)
if result.RowsAffected == 0 {

er := models.LicenseError{
Status: http.StatusConflict,
Message: "can not create obligation with same MD5",
Error: fmt.Sprintf("Error: Obligation with MD5 '%s' already exists", obligation.Md5),
Status: http.StatusConflict,
Message: "can not create obligation with same MD5",
Error: fmt.Sprintf("Error: Obligation with topic '%s' or MD5 '%s' already exists",
obligation.Topic, obligation.Md5),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
Expand Down

0 comments on commit 0f04b95

Please sign in to comment.