read itzaname code and simplify wacky string stuff
This commit is contained in:
parent
712ff32552
commit
f3b41c6b25
@ -3,8 +3,6 @@ package gormstore
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
||||||
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
||||||
@ -55,13 +53,7 @@ func (env *Submissions) Update(ctx context.Context, id int64, values datastore.O
|
|||||||
|
|
||||||
// the update can only occur if the status matches one of the provided values.
|
// the update can only occur if the status matches one of the provided values.
|
||||||
func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) error {
|
func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.Status, values datastore.OptionalMap) error {
|
||||||
tx := env.db.Model(&model.Submission{}).Where("id = ?", id)
|
if err := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status IN ?",statuses).Updates(values.Map()).Error; err != nil {
|
||||||
expr := make([]string, 0,len(statuses))
|
|
||||||
for i, status := range statuses{
|
|
||||||
expr[i] = fmt.Sprintf("status = %d", int32(status))
|
|
||||||
}
|
|
||||||
tx = tx.Where(strings.Join(expr, " OR "))
|
|
||||||
if err := tx.Updates(values.Map()).Error; err != nil {
|
|
||||||
if err == gorm.ErrRecordNotFound {
|
if err == gorm.ErrRecordNotFound {
|
||||||
return datastore.ErrNotExist
|
return datastore.ErrNotExist
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user