Skip to content

Commit

Permalink
optimized originalfilenames API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vin047 committed Sep 2, 2021
1 parent 261ddda commit af029f7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
32 changes: 20 additions & 12 deletions database/neo_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} ")
Expand All @@ -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 {
Expand Down
24 changes: 9 additions & 15 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down

0 comments on commit af029f7

Please sign in to comment.