条件查询
This commit is contained in:
46
bin/main.go
46
bin/main.go
@@ -49,6 +49,7 @@ type Images struct {
|
|||||||
Page int `json:"page"`
|
Page int `json:"page"`
|
||||||
PageSize int `json:"page_size"`
|
PageSize int `json:"page_size"`
|
||||||
Total int `json:"total"`
|
Total int `json:"total"`
|
||||||
|
Next bool `json:"next"`
|
||||||
List []Image `json:"list"`
|
List []Image `json:"list"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,30 +69,40 @@ func main() {
|
|||||||
// 排序
|
// 排序
|
||||||
// 分页
|
// 分页
|
||||||
|
|
||||||
// 按标签查询
|
// 获取查询条件(忽略空值), 超级简洁写法
|
||||||
var tags string
|
QueryConditions := func(key string) (list []string) {
|
||||||
if list := strings.Split(r.URL.Query().Get("tags"), ","); len(list) > 0 {
|
for _, item := range strings.Split(r.URL.Query().Get(key), ",") {
|
||||||
for _, tag := range list {
|
if item != "" {
|
||||||
tags += fmt.Sprintf("'%s',", tag)
|
list = append(list, fmt.Sprintf("'%s'", item))
|
||||||
}
|
}
|
||||||
tags = tags[:len(tags)-1]
|
}
|
||||||
fmt.Println(tags)
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
// 按分类查询
|
// 拼接查询条件, 超级简洁写法
|
||||||
var categories string
|
conditions := ""
|
||||||
if list := strings.Split(r.URL.Query().Get("categories"), ","); len(list) > 0 {
|
if authors := QueryConditions("authors"); len(authors) > 0 {
|
||||||
for _, category := range list {
|
conditions += fmt.Sprintf(" AND author IN (%s)", strings.Join(authors, ","))
|
||||||
categories += fmt.Sprintf("'%s',", category)
|
|
||||||
}
|
}
|
||||||
categories = categories[:len(categories)-1]
|
if tags := QueryConditions("tags"); len(tags) > 0 {
|
||||||
fmt.Println(categories)
|
conditions += fmt.Sprintf(" AND tag IN (%s)", strings.Join(tags, ","))
|
||||||
|
}
|
||||||
|
if categories := QueryConditions("categories"); len(categories) > 0 {
|
||||||
|
conditions += fmt.Sprintf(" AND categorie IN (%s)", strings.Join(categories, ","))
|
||||||
|
}
|
||||||
|
if sets := QueryConditions("sets"); len(sets) > 0 {
|
||||||
|
conditions += fmt.Sprintf(" AND sets IN (%s)", strings.Join(sets, ","))
|
||||||
|
}
|
||||||
|
if conditions != "" {
|
||||||
|
conditions = strings.Replace(conditions, " AND", "", 1) // 去掉第一个 AND
|
||||||
|
conditions = " WHERE" + conditions // 拼接 WHERE
|
||||||
|
fmt.Println(conditions) // 打印查询条件
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取图片列表
|
// 获取图片列表
|
||||||
var images Images
|
var images Images
|
||||||
images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
|
images.Page, images.PageSize = stringToInt(r.URL.Query().Get("page"), 1), stringToInt(r.URL.Query().Get("pageSize"), 10)
|
||||||
rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
|
rows, err := mysqlConnection.Database.Query("SELECT id, content, update_time, create_time FROM web_images"+conditions+" LIMIT ?, ?", (images.Page-1)*images.PageSize, images.PageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("获取图片列表失败", err)
|
log.Println("获取图片列表失败", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
@@ -110,13 +121,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取总数
|
// 获取总数
|
||||||
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images").Scan(&images.Total)
|
err = mysqlConnection.Database.QueryRow("SELECT COUNT(*) FROM web_images" + conditions).Scan(&images.Total)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("获取图片总数失败", err)
|
log.Println("获取图片总数失败", err)
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 是否有下一页
|
||||||
|
images.Next = images.Total > images.Page*images.PageSize
|
||||||
|
|
||||||
// 将对象转换为有缩进的JSON输出
|
// 将对象转换为有缩进的JSON输出
|
||||||
data, err := json.MarshalIndent(images, "", " ")
|
data, err := json.MarshalIndent(images, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user