From f78d468623232d40ea45c0f55e381c7a0111560a Mon Sep 17 00:00:00 2001 From: Eddy Decena Date: Mon, 7 Oct 2024 17:10:37 -0400 Subject: [PATCH] fix: Add data source sync method --- docs/docs.go | 39 ++++++++++++++++- docs/swagger.json | 39 ++++++++++++++++- docs/swagger.yaml | 27 +++++++++++- internal/controllers/ds.controllers.go | 58 ++++++++++++++++++++++++++ 4 files changed, 157 insertions(+), 6 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index b1f1758..8be64d7 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -114,6 +114,41 @@ const docTemplate = `{ } } }, + "/datasources/sync/{name}": { + "post": { + "description": "Synchronize data source schemas", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "data_source" + ], + "summary": "Sync Data Source", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.HTTPSuccess" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/models.HTTPError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/models.HTTPError" + } + } + } + } + }, "/datasources/{name}": { "get": { "description": "Retrieve data source configuration with the specified name", @@ -191,7 +226,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.HTTPSuccess" + "$ref": "#/definitions/models.DataSource" } }, "400": { @@ -233,7 +268,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.HTTPSuccess" + "$ref": "#/definitions/models.DataSource" } }, "400": { diff --git a/docs/swagger.json b/docs/swagger.json index 8242eae..f39adbf 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -105,6 +105,41 @@ } } }, + "/datasources/sync/{name}": { + "post": { + "description": "Synchronize data source schemas", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "data_source" + ], + "summary": "Sync Data Source", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/models.HTTPSuccess" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/models.HTTPError" + } + }, + "404": { + "description": "Not Found", + "schema": { + "$ref": "#/definitions/models.HTTPError" + } + } + } + } + }, "/datasources/{name}": { "get": { "description": "Retrieve data source configuration with the specified name", @@ -182,7 +217,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.HTTPSuccess" + "$ref": "#/definitions/models.DataSource" } }, "400": { @@ -224,7 +259,7 @@ "200": { "description": "OK", "schema": { - "$ref": "#/definitions/models.HTTPSuccess" + "$ref": "#/definitions/models.DataSource" } }, "400": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 4dc182b..c4777e2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -168,7 +168,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.HTTPSuccess' + $ref: '#/definitions/models.DataSource' "400": description: Bad Request schema: @@ -234,7 +234,7 @@ paths: "200": description: OK schema: - $ref: '#/definitions/models.HTTPSuccess' + $ref: '#/definitions/models.DataSource' "400": description: Bad Request schema: @@ -271,6 +271,29 @@ paths: summary: List Data Sources tags: - data_source + /datasources/sync/{name}: + post: + consumes: + - application/json + description: Synchronize data source schemas + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/models.HTTPSuccess' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/models.HTTPError' + "404": + description: Not Found + schema: + $ref: '#/definitions/models.HTTPError' + summary: Sync Data Source + tags: + - data_source /prompts/generate: post: consumes: diff --git a/internal/controllers/ds.controllers.go b/internal/controllers/ds.controllers.go index 3928318..4ff4d95 100644 --- a/internal/controllers/ds.controllers.go +++ b/internal/controllers/ds.controllers.go @@ -209,6 +209,63 @@ func (self *DataSourceController) CreateDataSource(c *gin.Context) { // @BasePath / +// PingExample godoc +// @Summary Sync Data Source +// @Schemes +// @Description Synchronize data source schemas +// @Tags data_source +// @Accept json +// @Produce json +// @Success 200 {object} models.HTTPSuccess +// @Failure 401 {object} models.HTTPError +// @Failure 404 {object} models.HTTPError +// @Router /datasources/sync/{name} [post] +func (self *DataSourceController) SyncDataSource(c *gin.Context) { + name := c.Param("name") + + _ownerId, _ := c.Get("owner_id") + ownerId, ok := _ownerId.(string) + if !ok { + c.JSON(http.StatusInternalServerError, models.HTTPError{ + Error: "internal_server_error", + Description: "Failed to read user identity", + }) + } + + _sub, _ := c.Get("sub") + sub, ok := _sub.(string) + if !ok { + c.JSON(http.StatusInternalServerError, models.HTTPError{ + Error: "internal_server_error", + Description: "Failed to read user identity", + }) + } + + dataSource, err := self.DataSourceService.GetByName(name, ownerId, false) + if err != nil { + logger.Error.Println(fmt.Printf("[%s][%s] Couldn't find data: %v\n", ownerId, sub, err)) + c.JSON(http.StatusNotFound, models.HTTPError{ + Error: "not_found", + Description: "No data source found for your organization", + }) + return + } + + err = self.DataSourceService.Sync(dataSource.ID, dataSource.Type) + if err != nil { + logger.Error.Println(fmt.Printf("[%s][%s] Couldn't find data: %v\n", ownerId, sub, err)) + c.JSON(http.StatusNotFound, models.HTTPError{ + Error: "not_found", + Description: "Error while syncing data source", + }) + return + } + + c.JSON(http.StatusOK, models.HTTPSuccess{Message: "Data Source synced successfully"}) +} + +// @BasePath / + // PingExample godoc // @Summary Modify Data Source // @Schemes @@ -322,6 +379,7 @@ func (self *DataSourceController) RegisterDataSourceRoutes(rg *gin.RouterGroup) dataSourceRoute := rg.Group("datasources") dataSourceRoute.GET("/:name", self.GetDataSourceByName) dataSourceRoute.GET("/all", self.GetAllDataSource) + dataSourceRoute.POST("/sync/:name", self.SyncDataSource) dataSourceRoute.POST("", self.CreateDataSource) dataSourceRoute.PUT("/:name", self.UpdateDataSourceByName) dataSourceRoute.DELETE("/:name", self.DeleteDataSourceByName)