feat:新增接口模块

This commit is contained in:
momo 2023-11-14 15:03:01 +08:00
parent 1cfc190b73
commit 9563cbab8d
9 changed files with 335 additions and 1 deletions

View File

@ -1,2 +1,58 @@
package gmodel
// TODO: 使用model的属性做你想做的
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
}

View File

@ -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)
}
}
}

View File

@ -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),
},
},
)
}

View File

@ -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)
}
}
}

View File

@ -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)
// }

View File

@ -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)
// }

View File

@ -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"`
}

View File

@ -0,0 +1,7 @@
package main
import "testing"
func TestMain(t *testing.T) {
main()
}

View File

@ -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"`
}
//获取部门列表