This commit is contained in:
2023-05-11 07:54:37 +08:00
parent 2ab29d1e02
commit 057e2c8222
2 changed files with 44 additions and 0 deletions

View File

@@ -46,6 +46,7 @@ func main() {
output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(0, "", ""), blackfriday.EXTENSION_TABLES|blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_AUTOLINK)
w.Write(output)
})
r.HandleFunc("/api/models", routers.ModelsGet).Methods("GET")
r.HandleFunc("/api/models", routers.ModelsPost).Methods("POST")
r.HandleFunc("/api/models/{id}", routers.ModelItemGet).Methods("GET")
@@ -64,6 +65,12 @@ func main() {
r.HandleFunc("/api/tasks/{id}", routers.TasksItemPatch).Methods("PATCH")
r.HandleFunc("/api/tasks/{id}", routers.TasksItemDelete).Methods("DELETE")
r.HandleFunc("/api/users", routers.UsersGet).Methods("GET")
r.HandleFunc("/api/users", routers.UsersPost).Methods("POST")
r.HandleFunc("/api/users/{id}", routers.UsersItemGet).Methods("GET")
r.HandleFunc("/api/users/{id}", routers.UsersItemPatch).Methods("PATCH")
r.HandleFunc("/api/users/{id}", routers.UsersItemDelete).Methods("DELETE")
r.HandleFunc("/api/params/model", routers.ParamsModelsGet).Methods("GET")
log.Println("Web Server is running on http://localhost:8080")

View File

@@ -4,8 +4,11 @@ import (
"main/models"
"main/utils"
"net/http"
"github.com/gorilla/mux"
)
// 獲取用戶列表
func UsersGet(w http.ResponseWriter, r *http.Request) {
var listview models.ListView
listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1)
@@ -16,3 +19,37 @@ func UsersGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(listview.ToJSON())
}
// 創建用戶
func UsersPost(w http.ResponseWriter, r *http.Request) {
var user models.User
user.Create()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(user))
}
// 獲取用戶
func UsersItemGet(w http.ResponseWriter, r *http.Request) {
user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
user.Get()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(user))
}
// 更新用戶
func UsersItemPatch(w http.ResponseWriter, r *http.Request) {
user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
user.Get()
user.Update()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(user))
}
// 刪除用戶
func UsersItemDelete(w http.ResponseWriter, r *http.Request) {
user := models.User{ID: utils.ParamInt(mux.Vars(r)["id"], 0)}
user.Get()
user.Delete()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(utils.ToJSON(user))
}