| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package service
- import (
- "errors"
- "xps/cache"
- "xps/datamodel"
- "xps/datasource"
- "xps/repositories"
- "xps/viewmodel"
- )
- type ApiService interface {
- GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error)
- GetPage(m map[string]interface{}) (*viewmodel.PageResult, error)
- GetById(apiId int64) (*viewmodel.ApiInfo, error)
- Create(d *datamodel.Api) error
- Update(d *datamodel.Api) error
- ChangeStatus(apiId, status int64) error
- Delete(m map[string]interface{}) error
-
- CacheApiData(apiId int64) error
- }
- type apiService struct {
- apiRepo *repositories.ApiRepo
- }
- func NewApiService() ApiService {
- return &apiService{
- apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
- }
- }
- func (s *apiService) GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error) {
- return s.apiRepo.GetList(m)
- }
- func (s *apiService) GetPage(m map[string]interface{}) (*viewmodel.PageResult, error) {
- return s.apiRepo.GetPage(m)
- }
- func (s *apiService) GetById(apiId int64) (*viewmodel.ApiInfo, error) {
- return s.apiRepo.GetById(apiId)
- }
- func (s *apiService) Create(api *datamodel.Api) error {
- api.ApiId = NewID()
- err := s.apiRepo.Create(api)
- if err != nil {
- return err
- }
- apiId := api.ApiId
- return s.CacheApiData(apiId)
- }
- func (s *apiService) Update(api *datamodel.Api) error {
- err := s.apiRepo.Update(api)
- if err != nil {
- return err
- }
-
- apiId := api.ApiId
- return s.CacheApiData(apiId)
- }
- func (s *apiService) ChangeStatus(apiId, status int64) error {
- data := &datamodel.Api{
- ApiId: apiId,
- Status: status,
- }
- err := s.apiRepo.Update(data)
- if err != nil {
- return err
- }
-
- return s.CacheApiData(apiId)
- }
- func (s *apiService) Delete(m map[string]interface{}) error {
- return s.apiRepo.Delete(m)
- }
- func (s *apiService) CacheApiData(apiId int64) error {
- api, err := s.apiRepo.GetById(apiId)
- if err != nil {
- return nil
- }
-
- // 缓存数据
- err = cache.SetApiData(api.ObjectCode, api.OamCode, api)
- if err != nil {
- return errors.New("Redis缓存Model数据失败")
- }
-
- return nil
- }
|