March 2019
Beginner to intermediate
324 pages
7h 17m
English
We expect a URL with a parameter here as well, such as /user/:id/orders. The :id parameter represents the ID of the user whose orders we are trying to retrieve. The code will look like this:
func (h *Handler) GetOrders(c *gin.Context) { if h.db == nil { return } // get id parameter p := c.Param("id") // convert the string 'p' to integer 'id' id, err := strconv.Atoi(p) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } // call the database layer method to get orders from id orders, err := h.db.GetCustomerOrdersByID(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, orders)}
Read now
Unlock full access