|
|
@@ -1,6 +1,8 @@
|
|
|
package service
|
|
|
|
|
|
import (
|
|
|
+ "errors"
|
|
|
+ "xps/cache"
|
|
|
"xps/datamodels"
|
|
|
"xps/datasource"
|
|
|
"xps/repositories"
|
|
|
@@ -15,37 +17,40 @@ type ApiService interface {
|
|
|
Update(d *datamodels.Api) error
|
|
|
ChangeStatus(modId, apiId, status int64) error
|
|
|
Delete(m map[string]interface{}) error
|
|
|
+ CacheApiData(modId, apiId int64) error
|
|
|
}
|
|
|
|
|
|
type apiService struct {
|
|
|
- repo *repositories.ApiRepo
|
|
|
+ apiRepo *repositories.ApiRepo
|
|
|
+ transRepo *repositories.ApiTransUnitRepo
|
|
|
}
|
|
|
|
|
|
func NewApiService() ApiService {
|
|
|
return &apiService{
|
|
|
- repo: repositories.NewApiRepo(datasource.InstanceMaster()),
|
|
|
+ apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
|
|
|
+ transRepo: repositories.NewApiTransUnitRepo(datasource.InstanceMaster()),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func (s *apiService) GetList(m map[string]interface{}) ([]viewmodels.ApiInfo, error) {
|
|
|
- return s.repo.GetList(m)
|
|
|
+ return s.apiRepo.GetList(m)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) GetPage(m map[string]interface{}) (*viewmodels.PageResult, error) {
|
|
|
- return s.repo.GetPage(m)
|
|
|
+ return s.apiRepo.GetPage(m)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) GetById(modId, apiId int64) (*viewmodels.ApiInfo, error) {
|
|
|
- return s.repo.GetById(modId, apiId)
|
|
|
+ return s.apiRepo.GetById(modId, apiId)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) Create(Api *datamodels.Api) error {
|
|
|
Api.ApiId = NewID()
|
|
|
- return s.repo.Create(Api)
|
|
|
+ return s.apiRepo.Create(Api)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) Update(Api *datamodels.Api) error {
|
|
|
- return s.repo.Update(Api)
|
|
|
+ return s.apiRepo.Update(Api)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) ChangeStatus(modId, apiId, status int64) error {
|
|
|
@@ -54,9 +59,41 @@ func (s *apiService) ChangeStatus(modId, apiId, status int64) error {
|
|
|
ApiId: apiId,
|
|
|
Status: status,
|
|
|
}
|
|
|
- return s.repo.Update(data)
|
|
|
+ return s.apiRepo.Update(data)
|
|
|
}
|
|
|
|
|
|
func (s *apiService) Delete(m map[string]interface{}) error {
|
|
|
- return s.repo.Delete(m)
|
|
|
+ return s.apiRepo.Delete(m)
|
|
|
}
|
|
|
+
|
|
|
+func (s *apiService) CacheApiData(modId, apiId int64) error {
|
|
|
+ api, err := s.apiRepo.GetById(modId, apiId)
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ trans, err := s.transRepo.GetListById(apiId)
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+
|
|
|
+ cacheData := &viewmodels.ApiDataInfo{
|
|
|
+ ModId: api.ModId,
|
|
|
+ ModTitle: api.ModTitle,
|
|
|
+ ApiId: api.ApiId,
|
|
|
+ ApiCode: api.ApiCode,
|
|
|
+ ApiTitle: api.ApiTitle,
|
|
|
+ ApiType: api.ApiType,
|
|
|
+ ApiParams: api.ApiParams,
|
|
|
+ ApiDesc: api.ApiDesc,
|
|
|
+ TransUnits: trans,
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存数据
|
|
|
+ err = cache.SetApiData(api.ApiCode, cacheData)
|
|
|
+ if err != nil {
|
|
|
+ return errors.New("Redis缓存Model数据失败")
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil
|
|
|
+}
|