chore: initial commit
This commit is contained in:
112
internal/handler/sys_api.go
Normal file
112
internal/handler/sys_api.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/pkg/httputil"
|
||||
"server/internal/service"
|
||||
)
|
||||
|
||||
type SysApiHandler struct {
|
||||
apiService *service.SysApiService
|
||||
}
|
||||
|
||||
func NewSysApiHandler(apiService *service.SysApiService) *SysApiHandler {
|
||||
return &SysApiHandler{apiService: apiService}
|
||||
}
|
||||
|
||||
func (h *SysApiHandler) ListPage(w http.ResponseWriter, r *http.Request) {
|
||||
pagination := httputil.Pagination(r)
|
||||
|
||||
list, total, err := h.apiService.ListPage(r.Context(), pagination)
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
resp := common.PageResponse{
|
||||
Page: pagination.Page,
|
||||
PageSize: pagination.PageSize,
|
||||
List: list,
|
||||
Total: total,
|
||||
}
|
||||
|
||||
httputil.OkWithPage(w, &resp)
|
||||
}
|
||||
|
||||
func (h *SysApiHandler) GetAllSysApis(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.apiService.GetAllSysApis(r.Context())
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, list)
|
||||
}
|
||||
|
||||
func (h *SysApiHandler) GetApiGroupNames(w http.ResponseWriter, r *http.Request) {
|
||||
list, err := h.apiService.GetApiGroupNames(r.Context())
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, list)
|
||||
}
|
||||
|
||||
func (h *SysApiHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
var req request.CreateSysApiRequest
|
||||
|
||||
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 *SysApiHandler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
var req request.UpdateSysApiRequest
|
||||
|
||||
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 *SysApiHandler) 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