feat: 统一sql命名、函数命名
This commit is contained in:
135
internal/handler/admin/api.go
Normal file
135
internal/handler/admin/api.go
Normal file
@@ -0,0 +1,135 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/pkg/httputil"
|
||||
"server/internal/router"
|
||||
"server/internal/service/admin"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type ApiHandler struct {
|
||||
apiService *admin.ApiService
|
||||
}
|
||||
|
||||
var _ router.Registrar = (*ApiHandler)(nil)
|
||||
|
||||
func NewApiHandler(apiService *admin.ApiService) *ApiHandler {
|
||||
return &ApiHandler{apiService: apiService}
|
||||
}
|
||||
|
||||
func (h *ApiHandler) Register(r chi.Router) {
|
||||
r.Route("/apis", func(r chi.Router) {
|
||||
r.Get("/", h.List)
|
||||
r.Get("/all", h.ListAll)
|
||||
r.Get("/groups", h.ListApiGroups)
|
||||
r.Post("/", h.Create)
|
||||
r.Patch("/{id}", h.Update)
|
||||
r.Delete("/{id}", h.Delete)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *ApiHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
pagination := httputil.Pagination(r)
|
||||
|
||||
params := request.SearchApiParams{
|
||||
Pagination: *pagination,
|
||||
Name: r.URL.Query().Get("name"),
|
||||
GroupName: r.URL.Query().Get("group_name"),
|
||||
Method: r.URL.Query().Get("method"),
|
||||
}
|
||||
|
||||
result, err := h.apiService.List(r.Context(), params)
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := common.PageResponse{
|
||||
Page: pagination.Page,
|
||||
PageSize: pagination.PageSize,
|
||||
List: result.List,
|
||||
Total: result.Total,
|
||||
}
|
||||
|
||||
httputil.OkWithPage(w, &resp)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ListAll(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.apiService.ListAll(r.Context())
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, list)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) ListApiGroups(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.apiService.ListApiGroups(r.Context())
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, list)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
var req request.CreateApiRequest
|
||||
|
||||
if err := httputil.BindJson(r, &req); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.apiService.Create(r.Context(), req); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
var req request.UpdateApiRequest
|
||||
|
||||
id, err := httputil.URLParamInt32(r, "id")
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = httputil.BindJson(r, &req); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = h.apiService.Update(r.Context(), id, req); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w)
|
||||
}
|
||||
|
||||
func (h *ApiHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := httputil.URLParamInt32(r, "id")
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = h.apiService.Delete(r.Context(), id); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w)
|
||||
}
|
||||
Reference in New Issue
Block a user