feat: order get requests
This commit is contained in:
@@ -231,3 +231,53 @@ func AddItemToOrder(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
ctx.JSON(http.StatusOK, order_item_price)
|
||||
}
|
||||
}
|
||||
|
||||
func GetOrderItems(pool *pgxpool.Pool) gin.HandlerFunc {
|
||||
return func(ctx *gin.Context) {
|
||||
conn, conn_err := pool.Acquire(ctx)
|
||||
if conn_err != nil {
|
||||
log.Printf("GetOrderItems(): ERROR: Failed to connect... %s", conn_err.Error())
|
||||
ctx.AbortWithStatus(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer conn.Release()
|
||||
|
||||
order_id := ctx.Query("order_id")
|
||||
|
||||
if order_id == "" {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderItems(): ERROR: order id not supplied")
|
||||
return
|
||||
}
|
||||
|
||||
_, parse_err := strconv.ParseInt(order_id, 10, 64)
|
||||
|
||||
if parse_err != nil {
|
||||
ctx.String(http.StatusBadRequest, "GetOrderItem(): ERROR... order id not an integer")
|
||||
return
|
||||
}
|
||||
|
||||
rows, query_err := conn.Query(context.Background(), queries.GET_ORDER_ITEMS, order_id)
|
||||
if query_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderItems(): ERROR... failed to query database.")
|
||||
log.Printf("GetOrderItems(): ERROR... Failed to query database... %s", query_err.Error())
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []dto.OrderItemPriceResponse
|
||||
|
||||
for rows.Next() {
|
||||
var item dto.OrderItemPriceResponse
|
||||
scan_err := rows.Scan(&item.ItemId, &item.OrderId, &item.ItemName, &item.Quantity, &item.Made, &item.CreatedAt, &item.TotalPrice, &item.UnitPrice)
|
||||
if scan_err != nil {
|
||||
ctx.String(http.StatusInternalServerError, "GetOrderItems(): ERROR... Failed to scan data...")
|
||||
log.Printf("GetOrderItems(): ERROR... failed to scan data... %s", scan_err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, items)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user