klesh commented on code in PR #6146:
URL: 
https://github.com/apache/incubator-devlake/pull/6146#discussion_r1338300146


##########
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) {
+                       return errors.Conflict.Wrap(err, fmt.Sprintf("%s 
already exists", srv.modelName))
+               }
+               return err
+       }
+       return srv.db.Update(model)
+}
+
+// CreateOrUpdate validates given model and insert or update it into database 
if validation passed
+func (srv *ModelSrvHelper[M]) CreateOrUpdate(model *M) errors.Error {
+       err := srv.ValidateModel(model)
+       if err != nil {
+               return err
+       }
+       return srv.db.CreateOrUpdate(model)
+}
+
+// DeleteModel deletes given model from database
+func (srv *ModelSrvHelper[M]) DeleteModel(model *M) errors.Error {
+       return srv.db.Delete(model)
+}
+
+// FindByPk returns model with given primary key from database
+func (srv *ModelSrvHelper[M]) FindByPk(pk ...interface{}) (*M, errors.Error) {
+       if len(pk) != srv.pkCount {
+               return nil, errors.BadInput.New("invalid primary key")
+       }
+       model := new(M)
+       err := srv.db.First(model, dal.Where(srv.pkWhere, pk...))
+       if err != nil {
+               if srv.db.IsErrorNotFound(err) {
+                       return nil, errors.NotFound.Wrap(err, fmt.Sprintf("%s 
not found", srv.modelName))
+               }
+               return nil, err
+       }
+       return model, nil
+}
+
+// GetAll returns all models from database
+func (srv *ModelSrvHelper[M]) GetAll() ([]*M, errors.Error) {
+       array := make([]*M, 0)
+       return array, srv.db.All(&array)
+}
+
+func (srv *ModelSrvHelper[M]) GetPage(pagination *Pagination, query 
...dal.Clause) ([]*M, int64, errors.Error) {
+       query = append(query, dal.From(new(M)))
+       // process keyword
+       searchTerm := pagination.SearchTerm
+       if searchTerm != "" && len(srv.searchColumns) > 0 {

Review Comment:
   Are you sure? would you like to take another closer look? The `searchTerm` 
is prefixed and suffixed with the `%` and then passed down to `gorm` as a SQL 
parameters, it should be safe from SQL injection IMO.



-- 
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

Reply via email to