From faaeb28a8abc59f380cbcb31a8b2db698224ff5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Mon, 19 Jun 2023 15:09:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=81=A2=E5=A4=8D=E7=9B=B4=E6=8E=A5=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/ListView.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/models/ListView.go b/models/ListView.go index e11ebf3..7fb28e8 100644 --- a/models/ListView.go +++ b/models/ListView.go @@ -12,6 +12,7 @@ type ListView struct { Total int64 `json:"total"` Next bool `json:"next"` List []interface{} `json:"list"` + Type string `json:"-"` } // 轉換爲JSON並返回 @@ -27,6 +28,59 @@ func (listview *ListView) ToJSON() []byte { return b } +//func (listview *ListView) Readx(r *http.Request, model interface{}, list interface{}) error { +// listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1) +// listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10) +// db := configs.ORMDB() +// db.Model(&model).Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&list) +// db.Model(&model).Count(&listview.Total) +// listview.Next = listview.Page*listview.PageSize < int(listview.Total) +// listview.List = list.([]interface{}) +// return nil +//} +// +//func (listview *ListView) Read(r *http.Request) error { +// listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1) +// listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10) +// +// db := configs.ORMDB() +// +// // 按照 user_id 篩選 +// if user_id := utils.ParamInt(r.URL.Query().Get("user_id"), 0); user_id > 0 { +// db = db.Where("user_id = ?", user_id) +// } +// +// // 按照 star 篩選 +// if star := utils.ParamInt(r.URL.Query().Get("star"), 0); star > 0 { +// db = db.Where("stars LIKE ?", "%"+strconv.Itoa(star)+"%") +// } +// +// // 按照 name 模糊搜索 +// if name := r.URL.Query().Get("name"); name != "" { +// db = db.Where("name LIKE ?", "%"+name+"%") +// } +// +// // 按照 type 篩選 +// if model_type := r.URL.Query().Get("type"); model_type != "" { +// db = db.Where("type = ?", model_type) +// } +// +// // 按照 status 篩選 +// if status := r.URL.Query().Get("status"); status != "" { +// db = db.Where("status = ?", status) +// } +// +// // 按照 tag 篩選 +// if tag := r.URL.Query().Get("tag"); tag != "" { +// db = db.Where("tags LIKE ?", "%"+tag+"%") +// } +// +// db.Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&listview.List) +// db.Model(listview.List).Count(&listview.Total) +// +// return nil +//} + // 直接輸出JSON給瀏覽器 func (listview *ListView) WriteJSON(w http.ResponseWriter) { w.Header().Set("Content-Type", "application/json; charset=utf-8")