check rows affected to see if anything happened

This commit is contained in:
Quaternions 2024-12-10 21:38:12 -08:00
parent e3360a6c6b
commit ca5c67cb4b

View File

@ -10,6 +10,10 @@ import (
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
var (
ErrorStatus = errors.New("Status is not in allowed statuses")
)
type Submissions struct { type Submissions struct {
db *gorm.DB db *gorm.DB
} }
@ -68,15 +72,20 @@ func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, status
// returns the updated value // returns the updated value
func (env *Submissions) IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) (model.Submission, error) { func (env *Submissions) IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) (model.Submission, error) {
var submission model.Submission var submission model.Submission
if err := env.db.Model(&submission). result := env.db.Model(&submission).
Clauses(clause.Returning{}). Clauses(clause.Returning{}).
Where("id = ?", id). Where("id = ?", id).
Where("status_id IN ?",statuses). Where("status_id IN ?",statuses).
Updates(values.Map()).Error; err != nil { Updates(values.Map())
if err == gorm.ErrRecordNotFound { if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return submission, datastore.ErrNotExist return submission, datastore.ErrNotExist
} }
return submission, err return submission, result.Error
}
if result.RowsAffected == 0 {
return submission, ErrorStatus
} }
return submission, nil return submission, nil