users
This commit is contained in:
7
main.go
7
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)
|
output := blackfriday.Markdown(input, blackfriday.HtmlRenderer(0, "", ""), blackfriday.EXTENSION_TABLES|blackfriday.EXTENSION_FENCED_CODE|blackfriday.EXTENSION_AUTOLINK)
|
||||||
w.Write(output)
|
w.Write(output)
|
||||||
})
|
})
|
||||||
|
|
||||||
r.HandleFunc("/api/models", routers.ModelsGet).Methods("GET")
|
r.HandleFunc("/api/models", routers.ModelsGet).Methods("GET")
|
||||||
r.HandleFunc("/api/models", routers.ModelsPost).Methods("POST")
|
r.HandleFunc("/api/models", routers.ModelsPost).Methods("POST")
|
||||||
r.HandleFunc("/api/models/{id}", routers.ModelItemGet).Methods("GET")
|
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.TasksItemPatch).Methods("PATCH")
|
||||||
r.HandleFunc("/api/tasks/{id}", routers.TasksItemDelete).Methods("DELETE")
|
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")
|
r.HandleFunc("/api/params/model", routers.ParamsModelsGet).Methods("GET")
|
||||||
|
|
||||||
log.Println("Web Server is running on http://localhost:8080")
|
log.Println("Web Server is running on http://localhost:8080")
|
||||||
|
@@ -4,8 +4,11 @@ import (
|
|||||||
"main/models"
|
"main/models"
|
||||||
"main/utils"
|
"main/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gorilla/mux"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 獲取用戶列表
|
||||||
func UsersGet(w http.ResponseWriter, r *http.Request) {
|
func UsersGet(w http.ResponseWriter, r *http.Request) {
|
||||||
var listview models.ListView
|
var listview models.ListView
|
||||||
listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1)
|
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.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||||
w.Write(listview.ToJSON())
|
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))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user