mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-14 20:41:15 +08:00
api文档完成
This commit is contained in:
@@ -113,3 +113,23 @@ type ServiceDoc struct {
|
||||
Updater auto.Label `json:"updater" aolabel:"user"`
|
||||
UpdateTime auto.TimeLabel `json:"update_time"`
|
||||
}
|
||||
|
||||
type ExportService struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Prefix string `json:"prefix,omitempty"`
|
||||
Description string `json:"description"`
|
||||
Team string `json:"team"`
|
||||
ServiceType string `json:"service_type"`
|
||||
Catalogue string `json:"catalogue"`
|
||||
Tags []string `json:"tags"`
|
||||
Logo string `json:"logo"`
|
||||
Doc string `json:"doc"`
|
||||
}
|
||||
|
||||
type ExportApp struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Team string `json:"team"`
|
||||
}
|
||||
|
||||
+127
-57
@@ -6,39 +6,40 @@ import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
|
||||
service_tag "github.com/APIParkLab/APIPark/service/service-tag"
|
||||
|
||||
|
||||
service_doc "github.com/APIParkLab/APIPark/service/service-doc"
|
||||
|
||||
|
||||
serviceDto "github.com/APIParkLab/APIPark/module/service/dto"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/tag"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/service"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/subscribe"
|
||||
"gorm.io/gorm"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/api"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/auto"
|
||||
|
||||
|
||||
team_member "github.com/APIParkLab/APIPark/service/team-member"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/store"
|
||||
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/utils"
|
||||
|
||||
|
||||
"github.com/APIParkLab/APIPark/service/team"
|
||||
|
||||
|
||||
service_dto "github.com/APIParkLab/APIPark/module/service/dto"
|
||||
)
|
||||
|
||||
var (
|
||||
_ IServiceModule = (*imlServiceModule)(nil)
|
||||
_ IServiceModule = (*imlServiceModule)(nil)
|
||||
_ IExportServiceModule = (*imlServiceModule)(nil)
|
||||
)
|
||||
|
||||
type imlServiceModule struct {
|
||||
@@ -52,8 +53,64 @@ type imlServiceModule struct {
|
||||
transaction store.ITransaction `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.ExportService, error) {
|
||||
services, err := i.serviceService.ServiceList(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceIds := utils.SliceToSlice(services, func(s *service.Service) string {
|
||||
return s.Id
|
||||
})
|
||||
serviceTags, err := i.serviceTagService.List(ctx, serviceIds, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tagMap, err := i.tagService.Map(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceTagMap := make(map[string][]string)
|
||||
for _, st := range serviceTags {
|
||||
if _, ok := tagMap[st.Tid]; !ok {
|
||||
continue
|
||||
}
|
||||
if _, ok := serviceTagMap[st.Sid]; !ok {
|
||||
serviceTagMap[st.Sid] = make([]string, 0)
|
||||
}
|
||||
serviceTagMap[st.Sid] = append(serviceTagMap[st.Sid], tagMap[st.Tid].Name)
|
||||
}
|
||||
|
||||
docMap, err := i.serviceDocService.Map(ctx, serviceIds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
items := make([]*service_dto.ExportService, 0, len(services))
|
||||
for _, s := range services {
|
||||
info := &service_dto.ExportService{
|
||||
Id: s.Id,
|
||||
Name: s.Name,
|
||||
Prefix: s.Prefix,
|
||||
Description: s.Description,
|
||||
Team: s.Team,
|
||||
ServiceType: s.ServiceType.String(),
|
||||
Catalogue: s.Catalogue,
|
||||
Logo: s.Logo,
|
||||
}
|
||||
if v, ok := docMap[s.Id]; ok {
|
||||
info.Doc = v.Doc
|
||||
}
|
||||
if tags, ok := serviceTagMap[s.Id]; ok {
|
||||
info.Tags = tags
|
||||
}
|
||||
items = append(items, info)
|
||||
}
|
||||
return items, nil
|
||||
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string, keyword string) ([]*service.Service, error) {
|
||||
|
||||
|
||||
userID := utils.UserId(ctx)
|
||||
condition := make(map[string]interface{})
|
||||
condition["as_server"] = true
|
||||
@@ -73,7 +130,7 @@ func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string,
|
||||
condition["team"] = teamIds
|
||||
return i.serviceService.Search(ctx, keyword, condition, "update_at desc")
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string, keyword string) ([]*service_dto.ServiceItem, error) {
|
||||
@@ -88,7 +145,7 @@ func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string,
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
items := make([]*service_dto.ServiceItem, 0, len(services))
|
||||
for _, model := range services {
|
||||
if teamId != "" && model.Team != teamId {
|
||||
@@ -121,7 +178,7 @@ func (i *imlServiceModule) SimpleAPPS(ctx context.Context, keyword string) ([]*s
|
||||
Id: p.Id,
|
||||
Name: p.Name,
|
||||
Description: p.Description,
|
||||
|
||||
|
||||
Team: auto.UUID(p.Team),
|
||||
}
|
||||
}), nil
|
||||
@@ -130,15 +187,15 @@ func (i *imlServiceModule) SimpleAPPS(ctx context.Context, keyword string) ([]*s
|
||||
func (i *imlServiceModule) Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
|
||||
w := make(map[string]interface{})
|
||||
w["as_server"] = true
|
||||
|
||||
|
||||
services, err := i.serviceService.Search(ctx, keyword, w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
items := make([]*service_dto.SimpleServiceItem, 0, len(services))
|
||||
for _, p := range services {
|
||||
|
||||
|
||||
items = append(items, &service_dto.SimpleServiceItem{
|
||||
Id: p.Id,
|
||||
Name: p.Name,
|
||||
@@ -151,14 +208,14 @@ func (i *imlServiceModule) Simple(ctx context.Context, keyword string) ([]*servi
|
||||
|
||||
func (i *imlServiceModule) MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error) {
|
||||
services, err := i.searchMyServices(ctx, "", keyword)
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
items := make([]*service_dto.SimpleServiceItem, 0, len(services))
|
||||
for _, p := range services {
|
||||
|
||||
|
||||
items = append(items, &service_dto.SimpleServiceItem{
|
||||
Id: p.Id,
|
||||
Name: p.Name,
|
||||
@@ -178,7 +235,7 @@ func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Ser
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
s := service_dto.ToService(serviceInfo)
|
||||
s.Tags = auto.List(utils.SliceToSlice(tags, func(p *service_tag.Tag) string {
|
||||
return p.Tid
|
||||
@@ -201,24 +258,19 @@ func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword st
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
serviceIds := utils.SliceToSlice(list, func(s *service.Service) string {
|
||||
return s.Id
|
||||
})
|
||||
|
||||
|
||||
apiCountMap, err := i.apiService.CountByGroup(ctx, "", map[string]interface{}{"service": serviceIds}, "service")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//serviceCountMap, err := i.serviceService.CountByGroup(ctx, "", map[string]interface{}{"uuid": serviceIds}, "service")
|
||||
//if err != nil {
|
||||
// return nil, err
|
||||
//}
|
||||
|
||||
|
||||
items := make([]*service_dto.ServiceItem, 0, len(list))
|
||||
for _, model := range list {
|
||||
apiCount := apiCountMap[model.Id]
|
||||
//serviceCount := serviceCountMap[model.Id]
|
||||
items = append(items, &service_dto.ServiceItem{
|
||||
Id: model.Id,
|
||||
Name: model.Name,
|
||||
@@ -234,7 +286,7 @@ func (i *imlServiceModule) Search(ctx context.Context, teamID string, keyword st
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *service_dto.CreateService) (*service_dto.Service, error) {
|
||||
|
||||
|
||||
if input.Id == "" {
|
||||
input.Id = uuid.New().String()
|
||||
}
|
||||
@@ -300,7 +352,7 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
|
||||
return fmt.Errorf("catalogue can not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
err = i.serviceService.Save(ctx, id, &service.Edit{
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
@@ -329,7 +381,7 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -337,7 +389,7 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d
|
||||
}
|
||||
|
||||
func (i *imlServiceModule) Delete(ctx context.Context, id string) error {
|
||||
|
||||
|
||||
err := i.transaction.Transaction(ctx, func(ctx context.Context) error {
|
||||
count, err := i.apiService.CountByService(ctx, id)
|
||||
if err != nil {
|
||||
@@ -346,7 +398,7 @@ func (i *imlServiceModule) Delete(ctx context.Context, id string) error {
|
||||
if count > 0 {
|
||||
return fmt.Errorf("service has apis, can not delete")
|
||||
}
|
||||
|
||||
|
||||
return i.serviceService.Delete(ctx, id)
|
||||
})
|
||||
return err
|
||||
@@ -389,7 +441,7 @@ func (i *imlServiceModule) getTagUuids(ctx context.Context, tags []string) ([]st
|
||||
|
||||
func (i *imlServiceModule) ServiceDoc(ctx context.Context, pid string) (*serviceDto.ServiceDoc, error) {
|
||||
_, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -421,7 +473,7 @@ func (i *imlServiceModule) ServiceDoc(ctx context.Context, pid string) (*service
|
||||
|
||||
func (i *imlServiceModule) SaveServiceDoc(ctx context.Context, pid string, input *serviceDto.SaveServiceDoc) error {
|
||||
_, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true})
|
||||
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -431,7 +483,10 @@ func (i *imlServiceModule) SaveServiceDoc(ctx context.Context, pid string, input
|
||||
})
|
||||
}
|
||||
|
||||
var _ IAppModule = &imlAppModule{}
|
||||
var (
|
||||
_ IAppModule = &imlAppModule{}
|
||||
_ IExportAppModule = &imlAppModule{}
|
||||
)
|
||||
|
||||
type imlAppModule struct {
|
||||
teamService team.ITeamService `autowired:""`
|
||||
@@ -441,6 +496,21 @@ type imlAppModule struct {
|
||||
transaction store.ITransaction `autowired:""`
|
||||
}
|
||||
|
||||
func (i *imlAppModule) ExportAll(ctx context.Context) ([]*service_dto.ExportApp, error) {
|
||||
apps, err := i.serviceService.AppList(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return utils.SliceToSlice(apps, func(p *service.Service) *service_dto.ExportApp {
|
||||
return &service_dto.ExportApp{
|
||||
Id: p.Id,
|
||||
Name: p.Name,
|
||||
Description: p.Description,
|
||||
Team: p.Team,
|
||||
}
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (i *imlAppModule) Search(ctx context.Context, teamId string, keyword string) ([]*service_dto.AppItem, error) {
|
||||
var services []*service.Service
|
||||
var err error
|
||||
@@ -456,16 +526,16 @@ func (i *imlAppModule) Search(ctx context.Context, teamId string, keyword string
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
serviceIds := utils.SliceToSlice(services, func(p *service.Service) string {
|
||||
return p.Id
|
||||
})
|
||||
|
||||
|
||||
subscribers, err := i.subscribeService.SubscriptionsByApplication(ctx, serviceIds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
subscribeCount := map[string]int64{}
|
||||
subscribeVerifyCount := map[string]int64{}
|
||||
verifyTmp := map[string]struct{}{}
|
||||
@@ -484,7 +554,7 @@ func (i *imlAppModule) Search(ctx context.Context, teamId string, keyword string
|
||||
subscribeVerifyCount[s.Application]++
|
||||
}
|
||||
default:
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
items := make([]*service_dto.AppItem, 0, len(services))
|
||||
@@ -516,7 +586,7 @@ func (i *imlAppModule) Search(ctx context.Context, teamId string, keyword string
|
||||
}
|
||||
|
||||
func (i *imlAppModule) CreateApp(ctx context.Context, teamID string, input *service_dto.CreateApp) (*service_dto.App, error) {
|
||||
|
||||
|
||||
if input.Id == "" {
|
||||
input.Id = uuid.New().String()
|
||||
}
|
||||
@@ -536,11 +606,11 @@ func (i *imlAppModule) CreateApp(ctx context.Context, teamID string, input *serv
|
||||
if len(members) == 0 {
|
||||
return nil, fmt.Errorf("master is not in team")
|
||||
}
|
||||
|
||||
|
||||
err = i.transaction.Transaction(ctx, func(ctx context.Context) error {
|
||||
|
||||
|
||||
return i.serviceService.Create(ctx, mo)
|
||||
|
||||
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -560,7 +630,7 @@ func (i *imlAppModule) UpdateApp(ctx context.Context, appId string, input *servi
|
||||
//if info.Master != userId {
|
||||
// return nil, fmt.Errorf("user is not app master, can not update")
|
||||
//}
|
||||
|
||||
|
||||
err = i.serviceService.Save(ctx, appId, &service.Edit{
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
@@ -589,7 +659,7 @@ func (i *imlAppModule) searchMyApps(ctx context.Context, teamId string, keyword
|
||||
}
|
||||
teamIds := membersForUser[userID]
|
||||
condition["team"] = teamIds
|
||||
|
||||
|
||||
return i.serviceService.Search(ctx, keyword, condition, "update_at desc")
|
||||
}
|
||||
}
|
||||
@@ -602,12 +672,12 @@ func (i *imlAppModule) SearchMyApps(ctx context.Context, teamId string, keyword
|
||||
serviceIds := utils.SliceToSlice(services, func(p *service.Service) string {
|
||||
return p.Id
|
||||
})
|
||||
|
||||
|
||||
subscribers, err := i.subscribeService.SubscriptionsByApplication(ctx, serviceIds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
subscribeCount := map[string]int64{}
|
||||
subscribeVerifyCount := map[string]int64{}
|
||||
verifyTmp := map[string]struct{}{}
|
||||
@@ -626,7 +696,7 @@ func (i *imlAppModule) SearchMyApps(ctx context.Context, teamId string, keyword
|
||||
subscribeVerifyCount[s.Application]++
|
||||
}
|
||||
default:
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
items := make([]*service_dto.AppItem, 0, len(services))
|
||||
@@ -681,7 +751,7 @@ func (i *imlAppModule) MySimpleApps(ctx context.Context, keyword string) ([]*ser
|
||||
}
|
||||
items := make([]*service_dto.SimpleAppItem, 0, len(services))
|
||||
for _, p := range services {
|
||||
|
||||
|
||||
items = append(items, &service_dto.SimpleAppItem{
|
||||
Id: p.Id,
|
||||
Name: p.Name,
|
||||
@@ -722,6 +792,6 @@ func (i *imlAppModule) DeleteApp(ctx context.Context, appId string) error {
|
||||
if !info.AsApp {
|
||||
return errors.New("not app, can not delete")
|
||||
}
|
||||
|
||||
|
||||
return i.serviceService.Delete(ctx, appId)
|
||||
}
|
||||
|
||||
+32
-13
@@ -2,10 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/APIParkLab/APIPark/module/system"
|
||||
"reflect"
|
||||
|
||||
|
||||
service_dto "github.com/APIParkLab/APIPark/module/service/dto"
|
||||
|
||||
|
||||
"github.com/eolinker/go-common/autowire"
|
||||
)
|
||||
|
||||
@@ -24,15 +25,19 @@ type IServiceModule interface {
|
||||
Delete(ctx context.Context, id string) error
|
||||
// Simple 获取简易项目列表
|
||||
Simple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
|
||||
|
||||
|
||||
// MySimple 获取我的简易项目列表
|
||||
MySimple(ctx context.Context, keyword string) ([]*service_dto.SimpleServiceItem, error)
|
||||
|
||||
|
||||
ServiceDoc(ctx context.Context, pid string) (*service_dto.ServiceDoc, error)
|
||||
// SaveServiceDoc 保存服务文档
|
||||
SaveServiceDoc(ctx context.Context, pid string, input *service_dto.SaveServiceDoc) error
|
||||
}
|
||||
|
||||
type IExportServiceModule interface {
|
||||
system.IExportModule[service_dto.ExportService]
|
||||
}
|
||||
|
||||
type IAppModule interface {
|
||||
CreateApp(ctx context.Context, teamID string, input *service_dto.CreateApp) (*service_dto.App, error)
|
||||
UpdateApp(ctx context.Context, appId string, input *service_dto.UpdateApp) (*service_dto.App, error)
|
||||
@@ -45,13 +50,27 @@ type IAppModule interface {
|
||||
DeleteApp(ctx context.Context, appId string) error
|
||||
}
|
||||
|
||||
func init() {
|
||||
autowire.Auto[IServiceModule](func() reflect.Value {
|
||||
m := new(imlServiceModule)
|
||||
return reflect.ValueOf(m)
|
||||
})
|
||||
autowire.Auto[IAppModule](func() reflect.Value {
|
||||
return reflect.ValueOf(new(imlAppModule))
|
||||
})
|
||||
|
||||
type IExportAppModule interface {
|
||||
system.IExportModule[service_dto.ExportApp]
|
||||
}
|
||||
|
||||
func init() {
|
||||
serviceModule := new(imlServiceModule)
|
||||
autowire.Auto[IServiceModule](func() reflect.Value {
|
||||
return reflect.ValueOf(serviceModule)
|
||||
})
|
||||
|
||||
autowire.Auto[IExportServiceModule](func() reflect.Value {
|
||||
return reflect.ValueOf(serviceModule)
|
||||
})
|
||||
|
||||
appModule := new(imlAppModule)
|
||||
autowire.Auto[IAppModule](func() reflect.Value {
|
||||
return reflect.ValueOf(appModule)
|
||||
})
|
||||
|
||||
autowire.Auto[IExportAppModule](func() reflect.Value {
|
||||
return reflect.ValueOf(appModule)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user