1 Star 0 Fork 0

王旭/banking

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
custormerHandlers.go 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
package app
import (
"encoding/json"
"net/http"
"gitee.com/waterwx_admin/banking/service"
"github.com/gorilla/mux"
)
// 客户结构体
// type Customer struct {
// Name string `json:"full_name" xml:"name"`
// City string `json:"city" xml:"city"`
// Zipcode string `json:"zip_code" xml:"zipcode"`
// }
// 客户处理结构体
type CustomerHandlers struct {
service service.CustomerService
}
// http://localhost:8000/customers?status=active
// http://localhost:8000/customers?status=inactive
// http://localhost:8000/customers
func (ch *CustomerHandlers)getAllCustomers(w http.ResponseWriter, r *http.Request) {
status := r.URL.Query().Get("status")
customers, err := ch.service.GetAllCustomers(status)
if err != nil {
writeResponse(w, err.Code, err.AsMessage())
} else {
writeResponse(w, http.StatusOK, customers)
}
}
func (ch *CustomerHandlers) getCustomer(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["customer_id"]
customer, err := ch.service.GetCustomer(id)
if err != nil {
writeResponse(w, err.Code, err.AsMessage())
} else {
writeResponse(w, http.StatusOK, customer)
}
}
func writeResponse(w http.ResponseWriter, code int, data interface{}) {
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(code)
if err := json.NewEncoder(w).Encode(data); err != nil {
panic(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/waterwx_admin/banking.git
git@gitee.com:waterwx_admin/banking.git
waterwx_admin
banking
banking
01fa8199727c

搜索帮助