From 9563cbab8d95750ddcd71c351b20e9d6548a599f Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Tue, 14 Nov 2023 15:03:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=96=B0=E5=A2=9E=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/ldap_apis_logic.go | 58 ++++++++++++- .../internal/handler/getapishandler.go | 35 ++++++++ server/ldap-admin/internal/handler/routes.go | 10 +++ .../internal/handler/saveapihandler.go | 35 ++++++++ .../ldap-admin/internal/logic/getapislogic.go | 67 +++++++++++++++ .../ldap-admin/internal/logic/saveapilogic.go | 86 +++++++++++++++++++ server/ldap-admin/internal/types/types.go | 15 ++++ server/ldap-admin/ldap-admin_test.go | 7 ++ server_api/ldap-admin.api | 23 +++++ 9 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 server/ldap-admin/internal/handler/getapishandler.go create mode 100644 server/ldap-admin/internal/handler/saveapihandler.go create mode 100644 server/ldap-admin/internal/logic/getapislogic.go create mode 100644 server/ldap-admin/internal/logic/saveapilogic.go create mode 100644 server/ldap-admin/ldap-admin_test.go diff --git a/model/gmodel/ldap_apis_logic.go b/model/gmodel/ldap_apis_logic.go index e68225aa..b679da41 100644 --- a/model/gmodel/ldap_apis_logic.go +++ b/model/gmodel/ldap_apis_logic.go @@ -1,2 +1,58 @@ package gmodel -// TODO: 使用model的属性做你想做的 \ No newline at end of file + +import ( + "context" + "time" +) + +// TODO: 使用model的属性做你想做的 + +type FindPageReq struct { + Fields string //筛选的字段 + Sort string //排序 + Page int //当前页 + Limit int //每页数量 +} + +// FindPage 分页查询 +func (s *LdapApisModel) FindPage(ctx context.Context, req FindPageReq) (resp []LdapApis, total int64, err error) { + db := s.db.WithContext(ctx).Model(&LdapApis{}) + if req.Fields != "" { + db = db.Select(req.Fields) + } + if req.Sort != "" { + db = db.Order(req.Sort) + } + //查询数量 + if err = db.Limit(1).Count(&total).Error; err != nil { + return nil, 0, err + } + offset := (req.Page - 1) * req.Limit + err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error + return resp, total, err +} + +func (s *LdapApisModel) FindOneById(ctx context.Context, id int64) (resp LdapApis, err error) { + db := s.db.WithContext(ctx).Model(&LdapApis{}) + + err = db.Take(&resp).Error + return resp, err +} + +// InsertOne 单个插入 +func (s *LdapApisModel) InsertOne(ctx context.Context, insertData LdapApis) error { + db := s.db.WithContext(ctx).Model(&LdapApis{}) + var nowTime = time.Now().UTC() + insertData.Ctime = &nowTime + insertData.Utime = &nowTime + result := db.Create(&insertData) + return result.Error +} + +// UpdateOne 单个更新 +func (s *LdapApisModel) UpdateOne(ctx context.Context, model LdapApis, updateData map[string]interface{}) error { + db := s.db.WithContext(ctx).Model(&model) + updateData["utime"] = time.Now().UTC() + result := db.Updates(updateData) + return result.Error +} diff --git a/server/ldap-admin/internal/handler/getapishandler.go b/server/ldap-admin/internal/handler/getapishandler.go new file mode 100644 index 00000000..da4f662a --- /dev/null +++ b/server/ldap-admin/internal/handler/getapishandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/ldap-admin/internal/logic" + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" +) + +func GetApisHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.GetApisReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewGetApisLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.GetApis(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/ldap-admin/internal/handler/routes.go b/server/ldap-admin/internal/handler/routes.go index 358f8674..234a5eb4 100644 --- a/server/ldap-admin/internal/handler/routes.go +++ b/server/ldap-admin/internal/handler/routes.go @@ -22,6 +22,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/ldap-admin/save_department", Handler: SaveDepartmentHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/api/ldap-admin/get_apis", + Handler: GetApisHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/api/ldap-admin/save_api", + Handler: SaveApiHandler(serverCtx), + }, }, ) } diff --git a/server/ldap-admin/internal/handler/saveapihandler.go b/server/ldap-admin/internal/handler/saveapihandler.go new file mode 100644 index 00000000..6048cb85 --- /dev/null +++ b/server/ldap-admin/internal/handler/saveapihandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/ldap-admin/internal/logic" + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" +) + +func SaveApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.SaveApiReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewSaveApiLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.SaveApi(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/ldap-admin/internal/logic/getapislogic.go b/server/ldap-admin/internal/logic/getapislogic.go new file mode 100644 index 00000000..95857faa --- /dev/null +++ b/server/ldap-admin/internal/logic/getapislogic.go @@ -0,0 +1,67 @@ +package logic + +import ( + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "math" + + "context" + + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetApisLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetApisLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApisLogic { + return &GetApisLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *GetApisLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *GetApisLogic) GetApis(req *types.GetApisReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + resList, resCount, err := l.svcCtx.AllModels.LdapApis.FindPage(l.ctx, gmodel.FindPageReq{ + Page: req.CurrentPage, + Limit: req.PerPage, + }) + if err != nil { + return resp.SetStatus(basic.CodeServiceErr) + } + var pageCount int = 1 + if resCount > int64(req.PerPage) { + var float64Count = float64(resCount) + var float64PerPage = float64(req.PerPage) + pageCountFloat := math.Ceil(float64Count / float64PerPage) + pageCount = int(pageCountFloat) + } + + return resp.SetStatus(basic.CodeOK, map[string]interface{}{ + "list": resList, + "meta": map[string]int{ + "total_count": int(resCount), + "page_count": pageCount, + "current_page": req.CurrentPage, + "per_page": req.PerPage, + }, + }) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *GetApisLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/ldap-admin/internal/logic/saveapilogic.go b/server/ldap-admin/internal/logic/saveapilogic.go new file mode 100644 index 00000000..b27dd8e7 --- /dev/null +++ b/server/ldap-admin/internal/logic/saveapilogic.go @@ -0,0 +1,86 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" + + "github.com/zeromicro/go-zero/core/logx" + "gorm.io/gorm" +) + +type SaveApiLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewSaveApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveApiLogic { + return &SaveApiLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *SaveApiLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *SaveApiLogic) SaveApi(req *types.SaveApiReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + var err1 error + if req.Id > 0 { + resOne, err := l.svcCtx.AllModels.LdapApis.FindOneById(l.ctx, req.Id) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + basic.CodeServiceErr.Message = "记录不存在" + } else { + basic.CodeServiceErr.Message = "系统出错" + } + return resp.SetStatus(basic.CodeServiceErr) + } + var updateMap = make(map[string]interface{}) + if req.Method != "" { + updateMap["method"] = req.Method + } + if req.Path != "" { + updateMap["path"] = req.Path + } + if req.Category != "" { + updateMap["category"] = req.Category + } + if req.Remark != "" { + updateMap["remark"] = req.Remark + } + if req.Creator != "" { + updateMap["creator"] = req.Creator + } + err1 = l.svcCtx.AllModels.LdapApis.UpdateOne(l.ctx, resOne, updateMap) + } else { + err1 = l.svcCtx.AllModels.LdapApis.InsertOne(l.ctx, gmodel.LdapApis{ + Method: &req.Method, + Path: &req.Path, + Category: &req.Category, + Remark: &req.Remark, + Creator: &req.Creator, + }) + } + if err1 != nil { + basic.CodeServiceErr.Message = "系统出错" + return resp.SetStatus(basic.CodeServiceErr) + } + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *SaveApiLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index 7ddf6522..033cf4e7 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -5,6 +5,21 @@ import ( "fusenapi/utils/basic" ) +type GetApisReq struct { + Sort string `form:"sort,optional"` + CurrentPage int `form:"current_page,optional,default=1"` + PerPage int `form:"per_page,optional,default=10"` +} + +type SaveApiReq struct { + Id int64 `json:"id"` + Method string `json:"method"` + Path string `json:"path"` + Category string `json:"category"` + Remark string `json:"remark"` + Creator string `json:"creator"` +} + type GetDepartmentsRsp struct { List []*DepartmentsItem `json:"list"` } diff --git a/server/ldap-admin/ldap-admin_test.go b/server/ldap-admin/ldap-admin_test.go new file mode 100644 index 00000000..ef649768 --- /dev/null +++ b/server/ldap-admin/ldap-admin_test.go @@ -0,0 +1,7 @@ +package main + +import "testing" + +func TestMain(t *testing.T) { + main() +} diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index 1c3319bc..a978df34 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -16,6 +16,29 @@ service ldap-admin { //保存部门信息 @handler SaveDepartmentHandler post /api/ldap-admin/save_department(SaveDepartmentReq) returns (response); + + //获取API列表 + @handler GetApisHandler + get /api/ldap-admin/get_apis(GetApisReq) returns (response); + + //保存API + @handler SaveApiHandler + post /api/ldap-admin/save_api(SaveApiReq) returns (response); +} + +type GetApisReq { + Sort string `form:"sort,optional"` + CurrentPage int `form:"current_page,optional,default=1"` + PerPage int `form:"per_page,optional,default=10"` +} + +type SaveApiReq { + Id int64 `json:"id"` + Method string `json:"method"` + Path string `json:"path"` + Category string `json:"category"` + Remark string `json:"remark"` + Creator string `json:"creator"` } //获取部门列表