From ec0795abb5e6ad305031ad846959d2d8b696ff6a Mon Sep 17 00:00:00 2001 From: thijsheijden <hi@thijsheijden.nl> Date: Mon, 19 Apr 2021 19:37:24 +0200 Subject: [PATCH] Moved domain specific entities into entity package --- internal/entity/document.go | 13 +++++++++++++ internal/usecases/request/interface.go | 4 +++- internal/usecases/request/request.go | 27 +++++++++++++------------- 3 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 internal/entity/document.go diff --git a/internal/entity/document.go b/internal/entity/document.go new file mode 100644 index 0000000..08ef36d --- /dev/null +++ b/internal/entity/document.go @@ -0,0 +1,13 @@ +package entity + +// Document with Empty struct to retrieve all data from the DB Document +type Document map[string]interface{} + +// GeneralFormat with Empty struct to retrieve all data from the DB Document +type GeneralFormat map[string][]Document + +// ListContainer is a struct that keeps track of the nodes and edges that need to be returned +type ListContainer struct { + NodeList []Document + EdgeList []Document +} diff --git a/internal/usecases/request/interface.go b/internal/usecases/request/interface.go index 7a30fb2..c54ca39 100644 --- a/internal/usecases/request/interface.go +++ b/internal/usecases/request/interface.go @@ -1,6 +1,8 @@ package request +import "query-service/internal/entity" + // UseCase is an interface describing the request usecases type UseCase interface { - SendAQLQuery(query string) (*map[string][]Document, error) + SendAQLQuery(query string) (*map[string][]entity.Document, error) } diff --git a/internal/usecases/request/request.go b/internal/usecases/request/request.go index 083150b..bfcf2b4 100644 --- a/internal/usecases/request/request.go +++ b/internal/usecases/request/request.go @@ -5,6 +5,7 @@ import ( "crypto/tls" "log" "os" + "query-service/internal/entity" "encoding/json" "io/ioutil" @@ -25,10 +26,10 @@ Parameters: AQLQuery is a string containing the query that will be send to the d Return: a map with two entries: "nodes" with a list of vertices/nodes and "edges" with a list of edges that will be returned to the frontend */ -func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]Document, error) { +func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]entity.Document, error) { // Get ArangoDB url from environment variable arangoURL := os.Getenv("ARANGO_HOST") - var queryResult = make(map[string][]Document) + var queryResult = make(map[string][]entity.Document) conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{arangoURL}, TLSConfig: &tls.Config{InsecureSkipVerify: true}, @@ -62,7 +63,7 @@ func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]Document, error) defer cursor.Close() //Loop through the resulting documents - listContainer := ListContainer{} + listContainer := entity.ListContainer{} for { var doc map[string][]interface{} _, err := cursor.ReadDocument(ctx, &doc) @@ -76,8 +77,8 @@ func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]Document, error) parseResult(doc, &listContainer) } - queryResult["nodes"] = listContainer.nodeList - queryResult["edges"] = listContainer.edgeList + queryResult["nodes"] = listContainer.NodeList + queryResult["edges"] = listContainer.EdgeList //writeJSON(queryResult) //file, err := json.MarshalIndent(queryResult, "", " ") @@ -91,20 +92,20 @@ listContainer is a struct containing the nodelist and edgelist that will be retu Return: Nothing because the result is stored in the listContainer */ -func parseResult(doc map[string][]interface{}, listContainer *ListContainer) { +func parseResult(doc map[string][]interface{}, listContainer *entity.ListContainer) { vertices := doc["vertices"] edges := doc["edges"] for _, vertex := range vertices { vertexDoc := vertex.(map[string]interface{}) - (*listContainer).nodeList = append((*listContainer).nodeList, parseNode(vertexDoc)) + (*listContainer).NodeList = append((*listContainer).NodeList, parseNode(vertexDoc)) } for _, edge := range edges { edgeDoc := edge.(map[string]interface{}) - (*listContainer).edgeList = append((*listContainer).edgeList, parseEdge(edgeDoc)) + (*listContainer).EdgeList = append((*listContainer).EdgeList, parseEdge(edgeDoc)) } } @@ -113,10 +114,10 @@ Parameters: d is a single entry of an edge Return: a document with almost the same structure as before, but the attributes are grouped */ -func parseEdge(d map[string]interface{}) Document { +func parseEdge(d map[string]interface{}) entity.Document { doc := d //.(map[string]interface{}) - data := make(Document) + data := make(entity.Document) data["_id"] = doc["_id"] delete(doc, "_id") data["_key"] = doc["_key"] @@ -138,7 +139,7 @@ func parseEdge(d map[string]interface{}) Document { } // writeJSON writes a json file for testing purposes -func writeJSON(queryResult map[string][]Document) { +func writeJSON(queryResult map[string][]entity.Document) { file, _ := json.MarshalIndent(queryResult, "", " ") _ = ioutil.WriteFile("result.json", file, 0644) @@ -149,10 +150,10 @@ Parameters: d is a single entry of an node Return: a document with almost the same structure as before, but the attributes are grouped */ -func parseNode(d map[string]interface{}) Document { +func parseNode(d map[string]interface{}) entity.Document { doc := d //.(map[string]interface{}) - data := make(Document) + data := make(entity.Document) data["_id"] = doc["_id"] delete(doc, "_id") data["_key"] = doc["_key"] -- GitLab