Files
ai/routers/images.go
2023-04-28 16:53:23 +08:00

73 lines
1.9 KiB
Go

package routers
import (
"encoding/json"
"io/ioutil"
"log"
"main/models"
"main/utils"
"net/http"
"github.com/gorilla/mux"
)
func ImagesGet(w http.ResponseWriter, r *http.Request) {
var listview models.ListView
listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1)
listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 10)
listview.List = models.QueryImages(listview.Page, listview.PageSize)
listview.Total = models.CountImages()
listview.Next = listview.Page*listview.PageSize < listview.Total
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(listview.ToJSON())
}
func ImagesPost(w http.ResponseWriter, r *http.Request) {
var image models.Image
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
defer r.Body.Close()
if err = json.Unmarshal(body, &image); err != nil {
log.Println(err)
return
}
image.Create()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(image))
}
func ImagesItemGet(w http.ResponseWriter, r *http.Request) {
image := models.Image{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
image.Get()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(image))
}
func ImagesItemPatch(w http.ResponseWriter, r *http.Request) {
image := models.Image{}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println(err)
return
}
defer r.Body.Close()
if err = json.Unmarshal(body, &image); err != nil {
log.Println(err)
return
}
image.ID = utils.ParamInt(mux.Vars(r)["id"], 0)
image.Update()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(image))
}
func ImagesItemDelete(w http.ResponseWriter, r *http.Request) {
image := models.Image{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
image.Delete()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(image))
}