From ca5c67cb4b099d41eed8e3cd8c2d02214ce7982d Mon Sep 17 00:00:00 2001 From: Quaternions Date: Tue, 10 Dec 2024 21:38:12 -0800 Subject: [PATCH] check rows affected to see if anything happened --- pkg/datastore/gormstore/submissions.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/pkg/datastore/gormstore/submissions.go b/pkg/datastore/gormstore/submissions.go index 233ba66..4a32751 100644 --- a/pkg/datastore/gormstore/submissions.go +++ b/pkg/datastore/gormstore/submissions.go @@ -10,6 +10,10 @@ import ( "gorm.io/gorm/clause" ) +var ( + ErrorStatus = errors.New("Status is not in allowed statuses") +) + type Submissions struct { db *gorm.DB } @@ -68,15 +72,20 @@ func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, status // returns the updated value func (env *Submissions) IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) (model.Submission, error) { var submission model.Submission - if err := env.db.Model(&submission). + result := env.db.Model(&submission). Clauses(clause.Returning{}). Where("id = ?", id). Where("status_id IN ?",statuses). - Updates(values.Map()).Error; err != nil { - if err == gorm.ErrRecordNotFound { + Updates(values.Map()) + if result.Error != nil { + if result.Error == gorm.ErrRecordNotFound { return submission, datastore.ErrNotExist } - return submission, err + return submission, result.Error + } + + if result.RowsAffected == 0 { + return submission, ErrorStatus } return submission, nil