从 GORM Raw() 查询中检索数据

Retrieving data from GORM Raw() Query

我正在尝试通过基于项目的学习来学习 Golang。我摆在自己面前的问题是模拟客户将产品添加到他们的购物车。目前,我有这样的 Cart.go 模型..

type Cart struct {
     ID         string    
     Customer   Customer  
     CustomerID string    
     Product    Product   
     ProductID  string    
     CreatedAt  time.Time 
     UpdatedAt  time.Time
}

在 Cart 模型上,我有一个函数定义为

func (c *Cart) GetAllItemsInCart(db *gorm.DB, customer_id string) (*gorm.DB, error) {
    items := db.Raw("SELECT id, product_id FROM carts WHERE customer_id = ?", customer_id).Scan(&c)

    return items, nil
}

这个函数在控制器中调用

func (server *Server) GetAllCartItems(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)

    id := vars["id"]

    cart := models.Cart{}

    items, err := cart.GetAllItemsInCart(server.DB, id)

    if err != nil {
        responses.ERROR(w, http.StatusInternalServerError, err)
        return
    }

    responses.JSON(w, http.StatusOK, items)
}

据我了解,目前 Scan() 将扫描结构的值,或者在这种情况下将找到的数据加载到指定的结构中。但是,我看到的回复是

 {
    "Value": null,
    "Error": null,
    "RowsAffected": 2
 }

这给了我 50% 的希望,因为“RowsAffected”: 2 是正确的。但是,有效载荷响应显然不是我要找的。任何指导将不胜感激,谢谢。

Scan 将值放入指向您为其提供的变量 (via &c) 的指针中,return 是一个数据库事务对象。您正在调用该交易对象 items,但事实并非如此。物品(即您购物车中的物品)在 c *Cart 中,而不是在 return 由 Scan 编辑的东西中。

你的方法通过填充它来修改 c,它不需要 return 任何东西,除非你想 return Scan 可能 return.

而不是这个...

    items, err := cart.GetAllItemsInCart(server.DB, id)

    // ... 

    responses.JSON(w, http.StatusOK, items)

你应该这样做:

    err := cart.GetAllItemsInCart(server.DB, id)

    // ...

    responses.JSON(w, http.StatusOK, cart)

您需要解决一些问题:

  • 您需要使用 Scan(c) 而不是 Scan(&c),因为 c 已经是一个指针。
  • 您应该始终检查错误。在您的 GetAllItemsInCart 方法中,您没有通过或检查错误。从技术上讲,您确实传递了它(在 items 对象内),但您没有在任何地方检查它。
  • 不需要将*gorm.DB指针向上传递。

如果你想保留你已经开始的代码结构,它可能看起来像这样:

func (c *Cart) GetAllItemsInCart(db *gorm.DB, customer_id string) error {
    return db.Raw("SELECT id, product_id FROM carts WHERE customer_id = ?", customer_id).Scan(c).Error
}

// controller
func (server *Server) GetAllCartItems(w http.ResponseWriter, r *http.Request) {
    vars := mux.Vars(r)

    id := vars["id"]

    cart := models.Cart{}

    err := cart.GetAllItemsInCart(server.DB, id)
    if err != nil {
        responses.ERROR(w, http.StatusInternalServerError, err)
        return
    }

    responses.JSON(w, http.StatusOK, cart)
}