diff --git a/Cargo.lock b/Cargo.lock index 92c337d..c0cf7ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1379,9 +1379,9 @@ dependencies = [ [[package]] name = "rbx_asset" -version = "0.4.7" +version = "0.4.8" source = "sparse+https://git.itzana.me/api/packages/strafesnet/cargo/" -checksum = "6e55b4f6f15966cbb943bce20bcad32c2481f8d4d92a947749007b5bc517e1e1" +checksum = "8c067713a3201d0d2de3445ebecc8001952d914319863a9c7bc8f9212fdb1a17" dependencies = [ "bytes", "chrono", diff --git a/pkg/datastore/gormstore/mapfixes.go b/pkg/datastore/gormstore/mapfixes.go index 50f4a05..ee4f0ad 100644 --- a/pkg/datastore/gormstore/mapfixes.go +++ b/pkg/datastore/gormstore/mapfixes.go @@ -55,11 +55,16 @@ func (env *Mapfixes) Update(ctx context.Context, id int64, values datastore.Opti // the update can only occur if the status matches one of the provided values. func (env *Mapfixes) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.MapfixStatus, values datastore.OptionalMap) error { - if err := env.db.Model(&model.Mapfix{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()).Error; err != nil { - if err == gorm.ErrRecordNotFound { + result := env.db.Model(&model.Mapfix{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()) + if result.Error != nil { + if result.Error == gorm.ErrRecordNotFound { return datastore.ErrNotExist } - return err + return result.Error + } + + if result.RowsAffected == 0 { + return datastore.ErroNoRowsAffected } return nil diff --git a/pkg/datastore/gormstore/submissions.go b/pkg/datastore/gormstore/submissions.go index 5791819..9f4d149 100644 --- a/pkg/datastore/gormstore/submissions.go +++ b/pkg/datastore/gormstore/submissions.go @@ -55,11 +55,16 @@ 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. func (env *Submissions) IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.SubmissionStatus, values datastore.OptionalMap) error { - if err := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()).Error; err != nil { - if err == gorm.ErrRecordNotFound { + result := env.db.Model(&model.Submission{}).Where("id = ?", id).Where("status_id IN ?", statuses).Updates(values.Map()) + if result.Error != nil { + if result.Error == gorm.ErrRecordNotFound { return datastore.ErrNotExist } - return err + return result.Error + } + + if result.RowsAffected == 0 { + return datastore.ErroNoRowsAffected } return nil diff --git a/pkg/service/mapfixes.go b/pkg/service/mapfixes.go index 2e95d2f..9bddba8 100644 --- a/pkg/service/mapfixes.go +++ b/pkg/service/mapfixes.go @@ -35,7 +35,7 @@ var( var ( ErrCreationPhaseMapfixesLimit = errors.New("Active mapfixes limited to 20") - ErrActiveMapfixSameTargetAssetID = errors.New("There is an active mapfix with the same TargetAssetID") + ErrActiveMapfixSameTargetAssetID = errors.New("There is an active mapfix for this map already") ErrAcceptOwnMapfix = fmt.Errorf("%w: You cannot accept your own mapfix as the submitter", ErrPermissionDenied) ErrCreateMapfixRateLimit = errors.New("You must not create more than 5 mapfixes every 10 minutes") ) @@ -77,6 +77,24 @@ func (svc *Service) CreateMapfix(ctx context.Context, request *api.MapfixTrigger } } + // Check if a mapfix targetting the same map exists in creation phase + { + filter := datastore.Optional() + filter.Add("submitter", int64(userId)) + filter.Add("target_asset_id", request.TargetAssetID) + filter.Add("status_id", CreationPhaseMapfixStatuses) + active_mapfixes, err := svc.DB.Mapfixes().List(ctx, filter, model.Page{ + Number: 1, + Size: 1, + },datastore.ListSortDisabled) + if err != nil { + return nil, err + } + if len(active_mapfixes) != 0{ + return nil, ErrActiveMapfixSameTargetAssetID + } + } + // Check if TargetAssetID actually exists { _, err := svc.Maps.Get(ctx, &maps.IdMessage{