From 3e9a1a28b1283e40bc7edb94e2370c74e7fd68e0 Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 17 五月 2024 15:54:29 +0800
Subject: [PATCH] 添加订阅查询接口

---
 util/http.go                |   26 +++++++++++--
 client/notify.go            |   11 +++++
 routes/webApi.go            |    4 +
 controller/subscribeCtl.go  |   28 ++++++++++++++
 repository/subscribeRepo.go |   13 ++++++
 5 files changed, 77 insertions(+), 5 deletions(-)

diff --git a/client/notify.go b/client/notify.go
index 2b1cd08..e1a9336 100644
--- a/client/notify.go
+++ b/client/notify.go
@@ -53,6 +53,7 @@
 
 	return vo.StatusSuccess
 }
+
 func UpdateSubscribe(url string, msg []byte) int {
 	rsp, err := util.HttpPut(url, headers, msg)
 	if err != nil {
@@ -72,3 +73,13 @@
 
 	return vo.StatusSuccess
 }
+
+func GetSubscribes(url string) ([]byte, error) {
+	rsp, err := util.HttpGet(url, headers)
+	if err != nil {
+		logger.Warn("Put subscribe failed, %s", err.Error())
+
+	}
+
+	return rsp, err
+}
diff --git a/controller/subscribeCtl.go b/controller/subscribeCtl.go
index 2cceebc..6ffc199 100644
--- a/controller/subscribeCtl.go
+++ b/controller/subscribeCtl.go
@@ -192,3 +192,31 @@
 
 	c.JSON(http.StatusOK, gin.H{"msg": "ok"})
 }
+
+func (s SubscribeController) List(c *gin.Context) {
+	if c.Param("id") == "" {
+		c.JSON(http.StatusBadRequest, gin.H{"msg": "涓嬬骇id涓虹┖"})
+	}
+
+	list, err := s.Repository.ListByFromId(c.Param("id"))
+	if err != nil {
+		c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
+		return
+	}
+
+	c.JSON(http.StatusOK, gin.H{"msg": "ok", "data": list})
+}
+
+func (s SubscribeController) RemoteList(c *gin.Context) {
+	if c.Param("id") == "" {
+		c.JSON(http.StatusBadRequest, gin.H{"msg": "涓嬬骇id涓虹┖"})
+	}
+
+	data, err := s.Repository.RemoteList(c.Param("id"))
+	if err != nil {
+		c.JSON(http.StatusInternalServerError, gin.H{"msg": err.Error()})
+		return
+	}
+
+	c.JSON(http.StatusOK, gin.H{"msg": "ok", "data": data})
+}
diff --git a/repository/subscribeRepo.go b/repository/subscribeRepo.go
index c924b80..4ecc1f7 100644
--- a/repository/subscribeRepo.go
+++ b/repository/subscribeRepo.go
@@ -122,6 +122,19 @@
 	return err
 }
 
+func (s *SubscribeRepository) RemoteList(sid string) ([]byte, error) {
+	// 鏌ユ壘涓嬬骇
+	var platform models.SubPlatform
+	err := platform.FindById(sid)
+	if err != nil {
+		return nil, err
+	}
+
+	uri := fmt.Sprintf("http://%s:%d/VIID/Subscribes", platform.RemoteIP, platform.RemotePort)
+
+	return client.GetSubscribes(uri)
+}
+
 func (s *SubscribeRepository) SaveReceiveSubscribe(fromId string, subscribe *vo.Subscribe) error {
 	var sub = models.Subscribe{
 		Id:     subscribe.SubscribeID,
diff --git a/routes/webApi.go b/routes/webApi.go
index ccd4d3b..9dd2c34 100644
--- a/routes/webApi.go
+++ b/routes/webApi.go
@@ -12,7 +12,9 @@
 	{
 		router.POST("/:id", subCtl.CreateSubscribes)
 		router.PUT("/:id", subCtl.UpdateSubscribes)
-		router.PUT("/cancel/:id", subCtl.UpdateSubscribes)
+		router.PUT("/cancel/:id", subCtl.CancelSubscribes)
+		router.GET("/list/:id", subCtl.List)
+		router.GET("/remote_list/:id", subCtl.RemoteList)
 		//router.GET("/:id", subCtl.Faces)
 		//router.DELETE("/:id", subCtl.Faces)
 	}
diff --git a/util/http.go b/util/http.go
index ecd4c99..38ad875 100644
--- a/util/http.go
+++ b/util/http.go
@@ -83,11 +83,29 @@
 	return body, nil
 }
 
-func HttpGet(url string) error {
-	_, err := http.Get(url)
+func HttpGet(url string, header map[string]string) ([]byte, error) {
+	req, err := http.NewRequest(http.MethodGet, url, nil)
 	if err != nil {
-		return err
+		return nil, err
 	}
 
-	return nil
+	//req.Header.Set("Content-Type", "applicaiton/json; charset=UTF-8")
+	if header != nil {
+		for k, v := range header {
+			req.Header.Set(k, v)
+		}
+	}
+	cli := &http.Client{}
+	resp, err := cli.Do(req)
+	if err != nil {
+		return nil, err
+	}
+
+	defer resp.Body.Close()
+	body, err := io.ReadAll(resp.Body)
+	if err != nil {
+		return nil, err
+	}
+
+	return body, nil
 }

--
Gitblit v1.8.0