From a4181ef6bfc65a34ea73ee2430364c8cf511f03d Mon Sep 17 00:00:00 2001 From: Pijus Kamandulis Date: Sat, 10 Feb 2024 21:05:08 +0200 Subject: [PATCH] Added get APIs for Stored Procedures, Triggers and User Defined Functions --- api/handlers/offers.go | 15 +++++++ api/handlers/stored_procedures.go | 22 ++++++++++ api/handlers/triggers.go | 22 ++++++++++ api/handlers/user_defined_functions.go | 22 ++++++++++ api/router.go | 5 +++ internal/repositories/collections.go | 2 +- internal/repositories/models.go | 41 +++++++++++++++++++ internal/repositories/stored_procedures.go | 15 +++++++ internal/repositories/triggers.go | 15 +++++++ .../repositories/user_defined_functions.go | 15 +++++++ 10 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 api/handlers/offers.go create mode 100644 api/handlers/stored_procedures.go create mode 100644 api/handlers/triggers.go create mode 100644 api/handlers/user_defined_functions.go create mode 100644 internal/repositories/stored_procedures.go create mode 100644 internal/repositories/triggers.go create mode 100644 internal/repositories/user_defined_functions.go diff --git a/api/handlers/offers.go b/api/handlers/offers.go new file mode 100644 index 0000000..b1d8426 --- /dev/null +++ b/api/handlers/offers.go @@ -0,0 +1,15 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" +) + +func GetOffers(c *gin.Context) { + c.IndentedJSON(http.StatusOK, gin.H{ + "_rid": "", + "_count": 0, + "Offers": []interface{}{}, + }) +} diff --git a/api/handlers/stored_procedures.go b/api/handlers/stored_procedures.go new file mode 100644 index 0000000..8aacf4a --- /dev/null +++ b/api/handlers/stored_procedures.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/internal/repositories" +) + +func GetAllStoredProcedures(c *gin.Context) { + databaseId := c.Param("databaseId") + collectionId := c.Param("collId") + + sps, status := repositories.GetAllStoredProcedures(databaseId, collectionId) + + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "StoredProcedures": sps, "_count": len(sps)}) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} diff --git a/api/handlers/triggers.go b/api/handlers/triggers.go new file mode 100644 index 0000000..86c5914 --- /dev/null +++ b/api/handlers/triggers.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/internal/repositories" +) + +func GetAllTriggers(c *gin.Context) { + databaseId := c.Param("databaseId") + collectionId := c.Param("collId") + + triggers, status := repositories.GetAllTriggers(databaseId, collectionId) + + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "Triggers": triggers, "_count": len(triggers)}) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} diff --git a/api/handlers/user_defined_functions.go b/api/handlers/user_defined_functions.go new file mode 100644 index 0000000..deed399 --- /dev/null +++ b/api/handlers/user_defined_functions.go @@ -0,0 +1,22 @@ +package handlers + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/pikami/cosmium/internal/repositories" +) + +func GetAllUserDefinedFunctions(c *gin.Context) { + databaseId := c.Param("databaseId") + collectionId := c.Param("collId") + + udfs, status := repositories.GetAllUserDefinedFunctions(databaseId, collectionId) + + if status == repositories.StatusOk { + c.IndentedJSON(http.StatusOK, gin.H{"_rid": "", "UserDefinedFunctions": udfs, "_count": len(udfs)}) + return + } + + c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": "Unknown error"}) +} diff --git a/api/router.go b/api/router.go index bae8411..39761a2 100644 --- a/api/router.go +++ b/api/router.go @@ -18,6 +18,11 @@ func CreateRouter() *gin.Engine { router.GET("/dbs/:databaseId", handlers.GetDatabase) router.DELETE("/dbs/:databaseId", handlers.DeleteDatabase) + router.GET("/dbs/:databaseId/colls/:collId/udfs", handlers.GetAllUserDefinedFunctions) + router.GET("/dbs/:databaseId/colls/:collId/sprocs", handlers.GetAllStoredProcedures) + router.GET("/dbs/:databaseId/colls/:collId/triggers", handlers.GetAllTriggers) + + router.GET("/offers", handlers.GetOffers) router.GET("/", handlers.GetServerInfo) return router diff --git a/internal/repositories/collections.go b/internal/repositories/collections.go index 7762eb0..5b9df3e 100644 --- a/internal/repositories/collections.go +++ b/internal/repositories/collections.go @@ -6,7 +6,7 @@ var collections = []Collection{ } func GetAllCollections(databaseId string) ([]Collection, RepositoryStatus) { - var dbCollections []Collection + dbCollections := make([]Collection, 0) for _, coll := range collections { if coll.internals.databaseId == databaseId { diff --git a/internal/repositories/models.go b/internal/repositories/models.go index 536dc08..88ec42f 100644 --- a/internal/repositories/models.go +++ b/internal/repositories/models.go @@ -45,3 +45,44 @@ type Collection struct { databaseId string } } + +type UserDefinedFunction struct { + Body string `json:"body"` + ID string `json:"id"` + Rid string `json:"_rid"` + Ts int `json:"_ts"` + Self string `json:"_self"` + Etag string `json:"_etag"` + internals struct { + databaseId string + collectionId string + } +} + +type StoredProcedure struct { + Body string `json:"body"` + ID string `json:"id"` + Rid string `json:"_rid"` + Ts int `json:"_ts"` + Self string `json:"_self"` + Etag string `json:"_etag"` + internals struct { + databaseId string + collectionId string + } +} + +type Trigger struct { + Body string `json:"body"` + ID string `json:"id"` + TriggerOperation string `json:"triggerOperation"` + TriggerType string `json:"triggerType"` + Rid string `json:"_rid"` + Ts int `json:"_ts"` + Self string `json:"_self"` + Etag string `json:"_etag"` + internals struct { + databaseId string + collectionId string + } +} diff --git a/internal/repositories/stored_procedures.go b/internal/repositories/stored_procedures.go new file mode 100644 index 0000000..43bcf30 --- /dev/null +++ b/internal/repositories/stored_procedures.go @@ -0,0 +1,15 @@ +package repositories + +var storedProcedures = []StoredProcedure{} + +func GetAllStoredProcedures(databaseId string, collectionId string) ([]StoredProcedure, RepositoryStatus) { + sps := make([]StoredProcedure, 0) + + for _, coll := range storedProcedures { + if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId { + sps = append(sps, coll) + } + } + + return sps, StatusOk +} diff --git a/internal/repositories/triggers.go b/internal/repositories/triggers.go new file mode 100644 index 0000000..48cc987 --- /dev/null +++ b/internal/repositories/triggers.go @@ -0,0 +1,15 @@ +package repositories + +var triggers = []Trigger{} + +func GetAllTriggers(databaseId string, collectionId string) ([]Trigger, RepositoryStatus) { + sps := make([]Trigger, 0) + + for _, coll := range triggers { + if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId { + sps = append(sps, coll) + } + } + + return sps, StatusOk +} diff --git a/internal/repositories/user_defined_functions.go b/internal/repositories/user_defined_functions.go new file mode 100644 index 0000000..cf46e66 --- /dev/null +++ b/internal/repositories/user_defined_functions.go @@ -0,0 +1,15 @@ +package repositories + +var userDefinedFunctions = []UserDefinedFunction{} + +func GetAllUserDefinedFunctions(databaseId string, collectionId string) ([]UserDefinedFunction, RepositoryStatus) { + udfs := make([]UserDefinedFunction, 0) + + for _, coll := range userDefinedFunctions { + if coll.internals.databaseId == databaseId && coll.internals.collectionId == collectionId { + udfs = append(udfs, coll) + } + } + + return udfs, StatusOk +}