From 9c88e1a18946ff7dfdea7b89c195972539a2af48 Mon Sep 17 00:00:00 2001
From: thijsheijden <hi@thijsheijden.nl>
Date: Fri, 30 Apr 2021 14:26:53 +0200
Subject: [PATCH] Updated arango request code

The request function now requires database credentials instead of using
hardcoded values.
---
 internal/usecases/request/interface.go |  2 +-
 internal/usecases/request/request.go   | 14 ++++++--------
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/internal/usecases/request/interface.go b/internal/usecases/request/interface.go
index c54ca39..329f583 100644
--- a/internal/usecases/request/interface.go
+++ b/internal/usecases/request/interface.go
@@ -4,5 +4,5 @@ import "query-service/internal/entity"
 
 // UseCase is an interface describing the request usecases
 type UseCase interface {
-	SendAQLQuery(query string) (*map[string][]entity.Document, error)
+	SendAQLQuery(query string, username string, password string, hostname string, port int, database string) (*map[string][]entity.Document, error)
 }
diff --git a/internal/usecases/request/request.go b/internal/usecases/request/request.go
index bfcf2b4..9162f6f 100644
--- a/internal/usecases/request/request.go
+++ b/internal/usecases/request/request.go
@@ -3,8 +3,8 @@ package request
 import (
 	"context"
 	"crypto/tls"
+	"fmt"
 	"log"
-	"os"
 	"query-service/internal/entity"
 
 	"encoding/json"
@@ -26,12 +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][]entity.Document, error) {
-	// Get ArangoDB url from environment variable
-	arangoURL := os.Getenv("ARANGO_HOST")
+func (s *Service) SendAQLQuery(query string, username string, password string, hostname string, port int, database string) (*map[string][]entity.Document, error) {
 	var queryResult = make(map[string][]entity.Document)
 	conn, err := http.NewConnection(http.ConnectionConfig{
-		Endpoints: []string{arangoURL},
+		Endpoints: []string{fmt.Sprintf("%s:%d", hostname, port)},
 		TLSConfig: &tls.Config{InsecureSkipVerify: true},
 	})
 	if err != nil {
@@ -40,7 +38,7 @@ func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]entity.Document,
 	}
 	c, err := driver.NewClient(driver.ClientConfig{
 		Connection:     conn,
-		Authentication: driver.BasicAuthentication("root", "DikkeDraak"),
+		Authentication: driver.BasicAuthentication(username, password),
 	})
 	if err != nil {
 		log.Println("Could not log in to the ArangoDB") // Handle error
@@ -48,14 +46,14 @@ func (s *Service) SendAQLQuery(AQLQuery string) (*map[string][]entity.Document,
 	}
 
 	ctx := context.Background()
-	db, err := c.Database(ctx, "_system")
+	db, err := c.Database(ctx, database)
 	if err != nil {
 		// handle error
 		log.Println(err)
 		return nil, err
 	}
 
-	cursor, err := db.Query(ctx, AQLQuery, nil)
+	cursor, err := db.Query(ctx, query, nil)
 	if err != nil {
 		log.Println("Invalid query") // handle error
 		return nil, err
-- 
GitLab