@@ -2,14 +2,57 @@ package charrep
22
33import (
44 "context"
5+ "errors"
56 "log"
67
78 "github.com/UltimateForm/gopen-api/internal/repository"
89 "github.com/neo4j/neo4j-go-driver/v5/neo4j"
910)
1011
12+ func txReadOneCharacter (ctx context.Context , id string ) neo4j.ManagedTransactionWorkT [Character ] {
13+ query := repository .NewQuery (map [string ]any {"id" : id },
14+ `
15+ MATCH (character:Character {id: $id})
16+ RETURN character
17+ ` )
18+ return func (tx neo4j.ManagedTransaction ) (Character , error ) {
19+ res , err := query .Execute (tx , ctx )
20+ if err != nil {
21+ return Character {}, err
22+ }
23+
24+ record , singleErr := res .Single (ctx )
25+ if singleErr != nil {
26+ return Character {}, singleErr
27+ }
28+
29+ rowMap , rowMapOk := record .AsMap ()["character" ]
30+ rowCharacter , rowCharacterOk := rowMap .(neo4j.Node )
31+ if ! rowMapOk || ! rowCharacterOk {
32+ return Character {}, errors .New ("bad row from db" )
33+ }
34+
35+ propsMap := rowCharacter .GetProperties ()
36+ name := propsMap ["name" ]
37+ id := propsMap ["id" ]
38+ description := propsMap ["description" ]
39+ debut := propsMap ["debut" ]
40+ nameStr := name .(string )
41+ idStr := id .(string )
42+ debutNum := int (debut .(float64 ))
43+ descrStr := description .(string )
44+
45+ return Character {
46+ Name : nameStr ,
47+ Id : idStr ,
48+ Debut : debutNum ,
49+ Description : descrStr ,
50+ }, nil
51+ }
52+ }
53+
1154func txReadCharacters (ctx context.Context , offset Offset ) neo4j.ManagedTransactionWorkT [[]Character ] {
12- query := repository .NewQuery (offset ,
55+ query := repository .NewQueryWithParams (offset ,
1356 `
1457MATCH (character:Character)
1558RETURN character
@@ -61,3 +104,7 @@ LIMIT $limit
61104func ReadCharacters (ctx context.Context , offset Offset ) ([]Character , error ) {
62105 return repository .ExecuteReadWithDriver (ctx , txReadCharacters (ctx , offset ))
63106}
107+
108+ func ReadOneCharacter (ctx context.Context , id string ) (Character , error ) {
109+ return repository .ExecuteReadWithDriver (ctx , txReadOneCharacter (ctx , id ))
110+ }
0 commit comments