From af029f7bcea38200d1d050bf7ffee338172d6c29 Mon Sep 17 00:00:00 2001 From: Vinoth Ramiah Date: Thu, 2 Sep 2021 18:37:43 +0100 Subject: [PATCH] optimized `originalfilenames` API endpoint --- database/neo_client.go | 32 ++++++++++++++++++++------------ server.go | 24 +++++++++--------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/database/neo_client.go b/database/neo_client.go index a40f356..9380ce6 100644 --- a/database/neo_client.go +++ b/database/neo_client.go @@ -497,13 +497,18 @@ func (neo *Neo4j) AddPathForOriginalAsset(id string, assetid string, remotepatho return err } -func (neo *Neo4j) SetAssetOriginalFilename(id string, assetid string, originalfilename string) error { +func (neo *Neo4j) SetAssetsOriginalFilenames(id string, data map[string]string) error { conn, err := neo.driverPool.OpenPool() if err != nil { return err } defer conn.Close() + // stmt, err := conn.PrepareNeo( + // "WITH {dicts} AS dicts " + + // "UNWIND dicts as data " + + // "MATCH (:User { id: {id} }) <- [:MEMORY] - (asset:Asset { uuid: data.uuid }) " + + // "SET asset.originalfilename = data.originalfilename ") stmt, err := conn.PrepareNeo( "MATCH (:User { id: {id} }) <- [:MEMORY] - (asset:Asset { uuid: {assetid} }) " + "SET asset.originalfilename = {originalfilename} ") @@ -512,18 +517,21 @@ func (neo *Neo4j) SetAssetOriginalFilename(id string, assetid string, originalfi } defer stmt.Close() // closing the statment will also close the rows - // executing a statement just returns summary information - result, err := stmt.ExecNeo(map[string] interface{} { - "id": id, - "assetid": assetid, - "originalfilename": originalfilename, - }) - if err != nil { - return err + // have to use loop as the unofficial neo4j go driver cannot encode lists/maps + for assetid, originalfilename := range data { + result, err := stmt.ExecNeo(map[string] interface{} { // executing a statement just returns summary information + "id": id, + "assetid": assetid, + "originalfilename": originalfilename, + }) + if err != nil { + return err + } + _, err = result.RowsAffected(); if err != nil { + return err + } } - - _, err = result.RowsAffected() - return err + return nil } func (neo *Neo4j) LeaveGroup(ownerid string, groupid string) error { diff --git a/server.go b/server.go index 6594918..ce33c99 100644 --- a/server.go +++ b/server.go @@ -998,12 +998,14 @@ func putAssetOriginalFilename(response http.ResponseWriter, request *http.Reques return } - err := neoDB.SetAssetOriginalFilename(token.UID, assetID, payload.Originalfilename) - if err == nil { - response.WriteHeader(http.StatusOK) - } else { + var data = map[string]string { + assetID: payload.Originalfilename, + } + if err := neoDB.SetAssetsOriginalFilenames(token.UID, data); err != nil { response.WriteHeader(http.StatusInternalServerError) errLogger.Println(err.Error()) + } else { + response.WriteHeader(http.StatusOK) } } @@ -1030,19 +1032,11 @@ func patchAssetsOriginalFilenames(response http.ResponseWriter, request *http.Re return } - var err error - for assetID, originalfilename := range payload { - err = neoDB.SetAssetOriginalFilename(token.UID, assetID, originalfilename) - if err != nil { - break - } - } - - if err == nil { - response.WriteHeader(http.StatusOK) - } else { + if err := neoDB.SetAssetsOriginalFilenames(token.UID, payload); err != nil { response.WriteHeader(http.StatusInternalServerError) errLogger.Println(err.Error()) + } else { + response.WriteHeader(http.StatusOK) } }