Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(obligation): fix FirstOrCreate in obligation creation #35

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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