dsomm80482 2018-10-30 14:08
浏览 46

如何在golang中对更新进行通用功能查询

Suppose there is a struct and I'm saving that struct into the database mongodb. example:-

type Booking struct {
  Id                          int                 `json:"_id,omitempty" bson:"_id,omitempty"`
  Uid                         int                 `json:"uid,omitempty" bson:"uid,omitempty"`
  IndustryId                  int                 `json:"industry_id,omitempty" bson:"industry_id,omitempty"`
  PricingParameter []BookingParams `json:"pricing_parameter,omitempty" bson:"pricing_parameter,omitempty"`
  AreaParameter    BookingParams   `json:"area_parameter,omitempty" bson:"area_parameter,omitempty"`
  PartialCleaning  []BookingParams `json:"partial_cleaning,omitempty" bson:"partial_cleaning,omitempty"`
  Extras           []BookingParams `json:"extras" bson:"extras"`
}

type BookingParams struct {
  Id                  int    `json:"id" bson:"id,omitempty"`
  Name                string `json:"name,omitempty" bson:"name,omitempty"`
  Quantity            int    `json:"quantity,omitempty" bson:"quantity,omitempty"`
}

This booking struct is saving in the database. Suppose there are 10 bookings having the extras and pricing_parameters field in them when I will update the any of these two fields value then my five booking will be update by using a single function. Now I'm using the below function for this:-

func (MerchantDb *MerchantDatabase) UpdateBulkBookingsDb(Id int, updator map[string]interface{}) (err error) {
   condition := bson.M{
    "status":    bson.M{"$in": []int{0, 1, 6, 7, 8}},
    "extras.id": Id}
  fmt.Println(bson.M{updator["field"].(string): updator["value"]})
  fmt.Println(condition)
  _, err = MerchantDb.DbUpdateAll(config.BookingsCollection, condition, bson.M{"$set": bson.M{updator["field"].(string): updator["value"]}})
  fmt.Println(err)
  return err
} 

Controller.go

func SaveExtra(c *gin.Context) {
  var extraData models.Extras
  var err error
  dataErr := json.NewDecoder(c.Request.Body).Decode(&extraData)
  if dataErr != nil {
    GetErrResponse(c, config.DataFormatErrMsg, dataErr)
    return
  }

  if extraData.Id != 0 {
    savedExtra, _ := merchantDb.GetExtraById(extraData.Id)
    err = merchantDb.UpdateExtraById(extraData.Id, extraData)
    updator := make(map[string]interface{})
    if savedExtra.TimeSA != extraData.TimeSA || savedExtra.TimeML != extraData.TimeML {
        updator["field"] = "is_length_updated"
        updator["value"] = 1
        err = merchantDb.UpdateBulkBookingsDb(extraData.Id, updator)
    }
    if savedExtra.PriceSA != extraData.PriceSA || savedExtra.PriceML != extraData.PriceML {
        updator["field"] = "is_price_updated"
        updator["value"] = 1
        err = merchantDb.UpdateBulkBookingsDb(extraData.Id, updator)
    }
    SuccessMsg = "Extra updated successfully."
  }
}

How will I use the single function for this. Any help please.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。