api_service.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package service
  2. import (
  3. "errors"
  4. "xps/cache"
  5. "xps/datamodel"
  6. "xps/datasource"
  7. "xps/repositories"
  8. "xps/viewmodel"
  9. )
  10. type ApiService interface {
  11. GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error)
  12. GetPage(m map[string]interface{}) (*viewmodel.PageResult, error)
  13. GetById(apiId int64) (*viewmodel.ApiInfo, error)
  14. Create(d *datamodel.Api) error
  15. Update(d *datamodel.Api) error
  16. ChangeStatus(apiId, status int64) error
  17. Delete(m map[string]interface{}) error
  18. CacheApiData(apiId int64) error
  19. }
  20. type apiService struct {
  21. apiRepo *repositories.ApiRepo
  22. }
  23. func NewApiService() ApiService {
  24. return &apiService{
  25. apiRepo: repositories.NewApiRepo(datasource.InstanceMaster()),
  26. }
  27. }
  28. func (s *apiService) GetList(m map[string]interface{}) ([]viewmodel.ApiInfo, error) {
  29. return s.apiRepo.GetList(m)
  30. }
  31. func (s *apiService) GetPage(m map[string]interface{}) (*viewmodel.PageResult, error) {
  32. return s.apiRepo.GetPage(m)
  33. }
  34. func (s *apiService) GetById(apiId int64) (*viewmodel.ApiInfo, error) {
  35. return s.apiRepo.GetById(apiId)
  36. }
  37. func (s *apiService) Create(api *datamodel.Api) error {
  38. api.ApiId = NewID()
  39. err := s.apiRepo.Create(api)
  40. if err != nil {
  41. return err
  42. }
  43. apiId := api.ApiId
  44. return s.CacheApiData(apiId)
  45. }
  46. func (s *apiService) Update(api *datamodel.Api) error {
  47. err := s.apiRepo.Update(api)
  48. if err != nil {
  49. return err
  50. }
  51. apiId := api.ApiId
  52. return s.CacheApiData(apiId)
  53. }
  54. func (s *apiService) ChangeStatus(apiId, status int64) error {
  55. data := &datamodel.Api{
  56. ApiId: apiId,
  57. Status: status,
  58. }
  59. err := s.apiRepo.Update(data)
  60. if err != nil {
  61. return err
  62. }
  63. return s.CacheApiData(apiId)
  64. }
  65. func (s *apiService) Delete(m map[string]interface{}) error {
  66. return s.apiRepo.Delete(m)
  67. }
  68. func (s *apiService) CacheApiData(apiId int64) error {
  69. api, err := s.apiRepo.GetById(apiId)
  70. if err != nil {
  71. return nil
  72. }
  73. // 缓存数据
  74. err = cache.SetApiData(api.ObjectCode, api.OamCode, api)
  75. if err != nil {
  76. return errors.New("Redis缓存Model数据失败")
  77. }
  78. return nil
  79. }