From 057e2c8222e9b7d75603821bdc23041826f8a3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A1=9C=E8=8F=AF?= Date: Thu, 11 May 2023 07:54:37 +0800 Subject: [PATCH] users --- main.go | 7 +++++++ routers/users.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/main.go b/main.go index 3a86ace..a9d6330 100644 --- a/main.go +++ b/main.go @@ -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") diff --git a/routers/users.go b/routers/users.go index 4b3da70..a07db74 100644 --- a/routers/users.go +++ b/routers/users.go @@ -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)) +}