路由分拆
This commit is contained in:
72
routers/images.go
Normal file
72
routers/images.go
Normal file
@@ -0,0 +1,72 @@
|
||||
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))
|
||||
}
|
Reference in New Issue
Block a user