Copilot commented on code in PR #75:
URL: 
https://github.com/apache/incubator-seata-go-samples/pull/75#discussion_r2383910065


##########
integrate_test/tcc/insert_on_update/main.go:
##########
@@ -0,0 +1,153 @@
+/*
+ * 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 main
+
+import (
+       "context"
+       "database/sql"
+       "errors"
+       "log"
+
+       "gorm.io/driver/mysql"
+       "gorm.io/gorm"
+       "gorm.io/gorm/clause"
+
+       "seata.apache.org/seata-go/pkg/client"
+       "seata.apache.org/seata-go/pkg/rm/tcc"
+       "seata.apache.org/seata-go/pkg/tm"
+)
+
+type OrderTblModel struct {
+       Id            int64  `gorm:"column:id;primaryKey"`
+       UserId        string `gorm:"column:user_id"`
+       CommodityCode string `gorm:"column:commodity_code"`
+       Count         int64  `gorm:"column:count"`
+       Money         int64  `gorm:"column:money"`
+       Descs         string `gorm:"column:descs"`
+}
+
+var gormDB *gorm.DB
+
+type TCCInsertOnUpdateService struct{}
+
+func (t *TCCInsertOnUpdateService) Prepare(ctx context.Context, params 
interface{}) (bool, error) {
+       order := params.(OrderTblModel)
+
+       // Insert on update operation using GORM
+       err := 
gormDB.WithContext(ctx).Table("order_tbl").Clauses(clause.OnConflict{
+               Columns:   []clause.Column{{Name: "id"}}, // by primary key
+               DoUpdates: clause.Assignments(map[string]interface{}{"descs": 
order.Descs}),
+       }).Create(&order).Error
+
+       if err != nil {
+               return false, err
+       }
+       log.Printf("[Prepare] insert on update order %+v", order)
+       return true, nil
+}
+
+func (t *TCCInsertOnUpdateService) Commit(ctx context.Context, 
businessActionContext *tm.BusinessActionContext) (bool, error) {
+       log.Printf("[Commit] confirm insert on update order")
+       return true, nil
+}
+
+func (t *TCCInsertOnUpdateService) Rollback(ctx context.Context, 
businessActionContext *tm.BusinessActionContext) (bool, error) {
+       log.Printf("[Rollback] cancel insert on update")
+
+       // Delete the record that was inserted/updated in Prepare phase
+       // Using the same ID that was used in the test
+       if err := gormDB.WithContext(ctx).
+               Table("order_tbl").
+               Where("id = ?", 1). // The test uses ID=1

Review Comment:
   Hard-coded ID value '1' in the Rollback method creates tight coupling with 
test data. Consider extracting the ID from the BusinessActionContext or making 
it configurable to improve maintainability and flexibility.



##########
integrate_test/tcc/insert/main.go:
##########
@@ -0,0 +1,174 @@
+/*
+ * 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 main
+
+import (
+       "context"
+       "database/sql"
+       "errors"
+       "log"
+
+       "gorm.io/driver/mysql"
+       "gorm.io/gorm"
+
+       "seata.apache.org/seata-go/pkg/client"
+       "seata.apache.org/seata-go/pkg/rm/tcc"
+       "seata.apache.org/seata-go/pkg/tm"
+)
+
+type OrderTblModel struct {
+       Id            int64  `gorm:"column:id;primaryKey"`
+       UserId        string `gorm:"column:user_id"`
+       CommodityCode string `gorm:"column:commodity_code"`
+       Count         int64  `gorm:"column:count"`
+       Money         int64  `gorm:"column:money"`
+       Descs         string `gorm:"column:descs"`
+}
+
+var gormDB *gorm.DB
+
+type OrderTCCService struct{}
+
+func (o *OrderTCCService) GetActionName() string {
+       return "OrderTCCService"
+}
+
+func (o *OrderTCCService) Prepare(ctx context.Context, params interface{}) 
(bool, error) {
+       order := params.(OrderTblModel)
+       if err := 
gormDB.WithContext(ctx).Table("order_tbl").Create(&order).Error; err != nil {
+               return false, err
+       }
+       log.Printf("[Prepare] insert order %+v", order)
+       return true, nil
+}
+
+func (o *OrderTCCService) Commit(ctx context.Context, bac 
*tm.BusinessActionContext) (bool, error) {
+       log.Printf("[Commit] confirm order")
+       return true, nil
+}
+
+func (o *OrderTCCService) Rollback(ctx context.Context, bac 
*tm.BusinessActionContext) (bool, error) {
+       if err := gormDB.WithContext(ctx).
+               Table("order_tbl").
+               Where("user_id = ? AND commodity_code = ?", "U10001", "C10001").

Review Comment:
   Hard-coded values 'U10001' and 'C10001' in the Rollback method don't match 
the actual test data which uses 'NO-100003' and 'C100001'. This could cause the 
rollback operation to fail silently as it won't find matching records to delete.
   ```suggestion
                Where("user_id = ? AND commodity_code = ?", "NO-100003", 
"C100001").
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to