我有一个News结构,我想在descending date order. 但他以通常的方式通过 id 向我显示它们结构:type News struct { Id int `json:"id" gorm:"primary_key, AUTO_INCREMENT"` ... CreatedAt time.Time `json:"created_at"`}功能:func GetAllNews(q *models.News, pagination *models.Pagination) (*[]models.News, error) { var news []models.News offset := (pagination.Page - 1) * pagination.Limit queryBuider := config.DB.Limit(pagination.Limit).Offset(offset).Order(pagination.Sort) result := queryBuider.Model(&models.News{}).Where(q).Order("Id DESC").Find(&news) if result.Error != nil { msg := result.Error return nil, msg } return &news, nil}func GetAllNews_by_page(c *gin.Context) { pagination := GeneratePaginationFromRequest(c) var news models.News prodLists, err := GetAllNews(&news, &pagination) if err != nil { c.JSON(http.StatusBadRequest, err.Error()) return } c.JSON(http.StatusOK, gin.H{ "data": prodLists, })}
1 回答
- 1 回答
- 0 关注
- 138 浏览
添加回答
举报
0/150
提交
取消