56 lines
1.6 KiB
Go
56 lines
1.6 KiB
Go
package routers
|
|
|
|
import (
|
|
"main/models"
|
|
"main/utils"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
// 獲取會話列表
|
|
func SessionsGet(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.QuerySessions(listview.Page, listview.PageSize)
|
|
listview.Total = models.CountSessions()
|
|
listview.Next = listview.Page*listview.PageSize < listview.Total
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Write(listview.ToJSON())
|
|
}
|
|
|
|
// 創建會話
|
|
func SessionsPost(w http.ResponseWriter, r *http.Request) {
|
|
var session models.Session
|
|
session.Create()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Write(utils.ToJSON(session))
|
|
}
|
|
|
|
// 獲取會話
|
|
func SessionsItemGet(w http.ResponseWriter, r *http.Request) {
|
|
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
|
session.Get()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Write(utils.ToJSON(session))
|
|
}
|
|
|
|
// 更新會話
|
|
func SessionsItemPatch(w http.ResponseWriter, r *http.Request) {
|
|
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
|
session.Get()
|
|
session.Update()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Write(utils.ToJSON(session))
|
|
}
|
|
|
|
// 刪除會話
|
|
func SessionsItemDelete(w http.ResponseWriter, r *http.Request) {
|
|
session := models.Session{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
|
|
session.Get()
|
|
session.Delete()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.Write(utils.ToJSON(session))
|
|
}
|