klesh commented on code in PR #6146: URL: https://github.com/apache/incubator-devlake/pull/6146#discussion_r1338294533
########## backend/helpers/srvhelper/model_service_helper.go: ########## @@ -0,0 +1,215 @@ +/* +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You under the Apache License, Version 2.0 +(the "License"); you may not use this file except in compliance with +the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package srvhelper + +import ( + "fmt" + "time" + + "github.com/apache/incubator-devlake/core/context" + "github.com/apache/incubator-devlake/core/dal" + "github.com/apache/incubator-devlake/core/errors" + "github.com/apache/incubator-devlake/core/log" + "github.com/apache/incubator-devlake/core/models" + "github.com/apache/incubator-devlake/helpers/dbhelper" + "github.com/go-playground/validator/v10" +) + +type CustomValidator interface { + CustomValidate(entitiy interface{}, validate *validator.Validate) errors.Error +} + +type ModelSrvHelper[M dal.Tabler] struct { + basicRes context.BasicRes + log log.Logger + db dal.Dal + validator *validator.Validate + modelName string + pk []dal.ColumnMeta + pkWhere string + pkCount int + searchColumns []string +} + +func NewModelSrvHelper[M dal.Tabler](basicRes context.BasicRes) *ModelSrvHelper[M] { + m := new(M) + modelName := fmt.Sprintf("%T", m) + db := basicRes.GetDal() + if db == nil { + db = basicRes.GetDal() + } + pk := errors.Must1(dal.GetPrimarykeyColumns(db, *m)) + pkWhere := "" + for _, col := range pk { + if pkWhere != "" { + pkWhere += " AND " + } + pkWhere += fmt.Sprintf("%s = ? ", col.Name()) + } + return &ModelSrvHelper[M]{ + basicRes: basicRes, + log: basicRes.GetLogger().Nested(fmt.Sprintf("%s_dal", modelName)), + db: db, + validator: validator.New(), + modelName: modelName, + pk: pk, + pkWhere: pkWhere, + pkCount: len(pk), + } +} + +func (srv *ModelSrvHelper[M]) NewTx(tx dal.Transaction) *ModelSrvHelper[M] { + helper := new(ModelSrvHelper[M]) + *helper = *srv + helper.db = tx + return helper +} + +func (srv *ModelSrvHelper[M]) ValidateModel(model *M) errors.Error { + // the model can validate itself + if customValidator, ok := (interface{}(model)).(CustomValidator); ok { + return customValidator.CustomValidate(model, srv.validator) + } + // basic validator + if e := srv.validator.Struct(model); e != nil { + return errors.BadInput.Wrap(e, "validation faild") + } + return nil +} + +// Create validates given model and insert it into database if validation passed +func (srv *ModelSrvHelper[M]) Create(model *M) errors.Error { + println("create model") + err := srv.ValidateModel(model) + if err != nil { + return err + } + err = srv.db.Create(model) + if err != nil { + if srv.db.IsDuplicationError(err) { + return errors.Conflict.Wrap(err, fmt.Sprintf("%s already exists", srv.modelName)) + } + return err + } + return err +} + +// Update validates given model and update it into database if validation passed +func (srv *ModelSrvHelper[M]) Update(model *M) errors.Error { + err := srv.ValidateModel(model) + if err != nil { + if srv.db.IsDuplicationError(err) { Review Comment: Yes, absolutely. One may update the model's unique index (e.g. name) causing conflict with other rows. it is not unusal. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@devlake.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org