mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
api文档完成
This commit is contained in:
@@ -2,10 +2,11 @@ package catalogue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/APIParkLab/APIPark/module/system"
|
||||
"reflect"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
|
||||
|
||||
catalogue_dto "github.com/APIParkLab/APIPark/module/catalogue/dto"
|
||||
)
|
||||
|
||||
@@ -16,6 +17,8 @@ type ICatalogueModule interface {
|
||||
Create(ctx context.Context, input *catalogue_dto.CreateCatalogue) error
|
||||
// Edit 编辑目录
|
||||
Edit(ctx context.Context, id string, input *catalogue_dto.EditCatalogue) error
|
||||
|
||||
Get(ctx context.Context, id string) (*catalogue_dto.Catalogue, error)
|
||||
// Delete 删除目录
|
||||
Delete(ctx context.Context, id string) error
|
||||
// Services 关键字筛选服务列表
|
||||
@@ -25,10 +28,20 @@ type ICatalogueModule interface {
|
||||
// Subscribe 订阅服务
|
||||
Subscribe(ctx context.Context, subscribeInfo *catalogue_dto.SubscribeService) error
|
||||
Sort(ctx context.Context, sorts []*catalogue_dto.SortItem) error
|
||||
//ExportAll(ctx context.Context) ([]*catalogue_dto.ExportCatalogue, error)
|
||||
}
|
||||
|
||||
type IExportCatalogueModule interface {
|
||||
system.IExportModule[catalogue_dto.ExportCatalogue]
|
||||
}
|
||||
|
||||
func init() {
|
||||
catalogueModule := new(imlCatalogueModule)
|
||||
autowire.Auto[ICatalogueModule](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlCatalogueModule))
|
||||
return reflect.ValueOf(catalogueModule)
|
||||
})
|
||||
|
||||
autowire.Auto[IExportCatalogueModule](func() reflect.Value {
|
||||
return reflect.ValueOf(catalogueModule)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ type CreateCatalogue struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Parent *string `json:"parent" aocheck:"catalogue"`
|
||||
Sort *int `json:"sort"`
|
||||
}
|
||||
|
||||
type EditCatalogue struct {
|
||||
Name *string `json:"name"`
|
||||
Parent *string `json:"parent" aocheck:"catalogue"`
|
||||
Sort *int `json:"sort"`
|
||||
}
|
||||
|
||||
type SubscribeService struct {
|
||||
|
||||
@@ -61,3 +61,17 @@ type Partition struct {
|
||||
Name string `json:"name"`
|
||||
Prefix string `json:"prefix"`
|
||||
}
|
||||
|
||||
type Catalogue struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Parent string `json:"parent"`
|
||||
Sort int `json:"sort"`
|
||||
}
|
||||
|
||||
type ExportCatalogue struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Parent string `json:"parent"`
|
||||
Sort int `json:"sort"`
|
||||
}
|
||||
|
||||
+95
-61
@@ -2,37 +2,36 @@ package catalogue
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
|
||||
service_doc "github.com/APIParkLab/APIPark/service/service-doc"
|
||||
|
||||
|
||||
service_tag "github.com/APIParkLab/APIPark/service/service-tag"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/subscribe"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/store"
|
||||
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/utils"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/release"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/api"
|
||||
"github.com/eolinker/go-common/auto"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/tag"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/service"
|
||||
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/catalogue"
|
||||
|
||||
|
||||
catalogue_dto "github.com/APIParkLab/APIPark/module/catalogue/dto"
|
||||
)
|
||||
|
||||
@@ -52,10 +51,39 @@ type imlCatalogueModule struct {
|
||||
subscribeService subscribe.ISubscribeService `autowired:""`
|
||||
subscribeApplyService subscribe.ISubscribeApplyService `autowired:""`
|
||||
transaction store.ITransaction `autowired:""`
|
||||
|
||||
|
||||
root *Root
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) Get(ctx context.Context, id string) (*catalogue_dto.Catalogue, error) {
|
||||
info, err := i.catalogueService.Get(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &catalogue_dto.Catalogue{
|
||||
Id: info.Id,
|
||||
Name: info.Name,
|
||||
Parent: info.Parent,
|
||||
Sort: info.Sort,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) ExportAll(ctx context.Context) ([]*catalogue_dto.ExportCatalogue, error) {
|
||||
list, err := i.catalogueService.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return utils.SliceToSlice(list, func(c *catalogue.Catalogue) *catalogue_dto.ExportCatalogue {
|
||||
return &catalogue_dto.ExportCatalogue{
|
||||
Id: c.Id,
|
||||
Name: c.Name,
|
||||
Parent: c.Parent,
|
||||
Sort: c.Sort,
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catalogue_dto.SubscribeService) error {
|
||||
if len(subscribeInfo.Applications) == 0 {
|
||||
return fmt.Errorf("applications is empty")
|
||||
@@ -68,18 +96,18 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
if !s.AsServer {
|
||||
return fmt.Errorf("service does not support subscribe")
|
||||
}
|
||||
|
||||
|
||||
userId := utils.UserId(ctx)
|
||||
return i.transaction.Transaction(ctx, func(ctx context.Context) error {
|
||||
|
||||
|
||||
apps := make([]string, 0, len(subscribeInfo.Applications))
|
||||
|
||||
|
||||
for _, appId := range subscribeInfo.Applications {
|
||||
if appId == s.Id {
|
||||
// 不能订阅自己
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
appInfo, err := i.serviceService.Get(ctx, appId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -103,7 +131,7 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
Status: subscribe.ApplyStatusReview,
|
||||
Applier: userId,
|
||||
})
|
||||
|
||||
|
||||
//} else {
|
||||
// status := subscribe.ApplyStatusReview
|
||||
// err = i.subscribeApplyService.Save(ctx, info.Id, &subscribe.EditApply{
|
||||
@@ -114,7 +142,7 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// 修改订阅表状态
|
||||
subscribers, err := i.subscribeService.ListByApplication(ctx, subscribeInfo.Service, appId)
|
||||
if err != nil {
|
||||
@@ -131,7 +159,7 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
} else {
|
||||
subscriberMap := utils.SliceToMap(subscribers, func(t *subscribe.Subscribe) string {
|
||||
return t.Application
|
||||
@@ -154,9 +182,9 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
ApplyStatus: &status,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
apps = append(apps, appId)
|
||||
}
|
||||
if len(apps) == 0 {
|
||||
@@ -164,7 +192,7 @@ func (i *imlCatalogueModule) Subscribe(ctx context.Context, subscribeInfo *catal
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) ServiceDetail(ctx context.Context, sid string) (*catalogue_dto.ServiceDetail, error) {
|
||||
@@ -182,7 +210,7 @@ func (i *imlCatalogueModule) ServiceDetail(ctx context.Context, sid string) (*ca
|
||||
} else {
|
||||
docStr = doc.Doc
|
||||
}
|
||||
|
||||
|
||||
r, err := i.releaseService.GetRunning(ctx, s.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
@@ -196,7 +224,7 @@ func (i *imlCatalogueModule) ServiceDetail(ctx context.Context, sid string) (*ca
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
return nil, fmt.Errorf("get running release failed: %w", err)
|
||||
}
|
||||
_, docCommits, _, err := i.releaseService.GetReleaseInfos(ctx, r.UUID)
|
||||
@@ -213,39 +241,40 @@ func (i *imlCatalogueModule) ServiceDetail(ctx context.Context, sid string) (*ca
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
apis := make([]*catalogue_dto.ServiceApi, 0, len(apiList))
|
||||
// TODO:此处载入API文档
|
||||
for _, info := range apiList {
|
||||
basicApi := &catalogue_dto.ServiceApiBasic{
|
||||
Id: info.UUID,
|
||||
Name: info.Name,
|
||||
Description: info.Description,
|
||||
Method: info.Method,
|
||||
Path: info.Path,
|
||||
Creator: auto.UUID(info.Creator),
|
||||
Updater: auto.UUID(info.Updater),
|
||||
CreateTime: auto.TimeLabel(info.CreateAt),
|
||||
UpdateTime: auto.TimeLabel(info.UpdateAt),
|
||||
//Methods: info.Methods,
|
||||
Path: info.Path,
|
||||
Creator: auto.UUID(info.Creator),
|
||||
Updater: auto.UUID(info.Updater),
|
||||
CreateTime: auto.TimeLabel(info.CreateAt),
|
||||
UpdateTime: auto.TimeLabel(info.UpdateAt),
|
||||
}
|
||||
v, ok := apiMap[info.UUID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
commit, err := i.apiService.GetDocumentCommit(ctx, v.Commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp := make(map[string]interface{})
|
||||
if commit.Data != nil {
|
||||
err = json.Unmarshal([]byte(commit.Data.Content), &tmp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
//v, ok := apiMap[info.UUID]
|
||||
//if !ok {
|
||||
// continue
|
||||
//}
|
||||
//commit, err := i.apiService.GetDocumentCommit(ctx, v.Commit)
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
//tmp := make(map[string]interface{})
|
||||
//if commit.Data != nil {
|
||||
// err = json.Unmarshal([]byte(commit.Data.Content), &tmp)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
//}
|
||||
|
||||
apis = append(apis, &catalogue_dto.ServiceApi{
|
||||
ServiceApiBasic: basicApi,
|
||||
Doc: tmp,
|
||||
//Doc: tmp,
|
||||
})
|
||||
}
|
||||
countMap, err := i.subscribeService.CountMapByService(ctx, subscribe.ApplyStatusSubscribe, sid)
|
||||
@@ -280,7 +309,7 @@ func (i *imlCatalogueModule) ServiceDetail(ctx context.Context, sid string) (*ca
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*catalogue_dto.ServiceItem, error) {
|
||||
|
||||
|
||||
serviceTags, err := i.serviceTagService.List(ctx, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -288,7 +317,7 @@ func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*c
|
||||
serviceTagMap := utils.SliceToMapArrayO(serviceTags, func(t *service_tag.Tag) (string, string) {
|
||||
return t.Sid, t.Tid
|
||||
})
|
||||
|
||||
|
||||
items, err := i.serviceService.SearchPublicServices(ctx, keyword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -303,25 +332,25 @@ func (i *imlCatalogueModule) Services(ctx context.Context, keyword string) ([]*c
|
||||
if len(serviceIds) < 1 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
// 获取服务API数量
|
||||
apiCountMap, err := i.apiService.CountMapByService(ctx, serviceIds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
subscriberCountMap, err := i.subscribeService.CountMapByService(ctx, subscribe.ApplyStatusSubscribe, serviceIds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
result := make([]*catalogue_dto.ServiceItem, 0, len(items))
|
||||
for _, v := range items {
|
||||
apiNum, ok := apiCountMap[v.Id]
|
||||
if !ok || apiNum < 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
|
||||
result = append(result, &catalogue_dto.ServiceItem{
|
||||
Id: v.Id,
|
||||
Name: v.Name,
|
||||
@@ -376,7 +405,7 @@ func (i *imlCatalogueModule) Sort(ctx context.Context, sorts []*catalogue_dto.So
|
||||
i.root = NewRoot(all)
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (i *imlCatalogueModule) Search(ctx context.Context, keyword string) ([]*catalogue_dto.Item, error) {
|
||||
@@ -396,7 +425,7 @@ func (i *imlCatalogueModule) Search(ctx context.Context, keyword string) ([]*cat
|
||||
}
|
||||
return treeItems("", parentMap), nil
|
||||
}
|
||||
|
||||
|
||||
catalogues, err := i.catalogueService.Search(ctx, keyword, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -406,7 +435,7 @@ func (i *imlCatalogueModule) Search(ctx context.Context, keyword string) ([]*cat
|
||||
i.root = NewRoot(all)
|
||||
}
|
||||
items := make([]*catalogue_dto.Item, 0, len(catalogues))
|
||||
|
||||
|
||||
return items, nil
|
||||
}
|
||||
|
||||
@@ -418,11 +447,15 @@ func (i *imlCatalogueModule) Create(ctx context.Context, input *catalogue_dto.Cr
|
||||
if input.Id == "" {
|
||||
input.Id = uuid.New().String()
|
||||
}
|
||||
index := _sortMax
|
||||
if input.Sort != nil {
|
||||
index = *input.Sort
|
||||
}
|
||||
err := i.catalogueService.Create(ctx, &catalogue.CreateCatalogue{
|
||||
Id: input.Id,
|
||||
Name: input.Name,
|
||||
Parent: parent,
|
||||
Sort: _sortMax,
|
||||
Sort: index,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -440,6 +473,7 @@ func (i *imlCatalogueModule) Edit(ctx context.Context, id string, input *catalog
|
||||
err := i.catalogueService.Save(ctx, id, &catalogue.EditCatalogue{
|
||||
Name: input.Name,
|
||||
Parent: input.Parent,
|
||||
Sort: input.Sort,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user