|
| 1 | +package charrep |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/UltimateForm/gopen-api/internal/repository" |
| 8 | + "github.com/neo4j/neo4j-go-driver/v5/neo4j" |
| 9 | +) |
| 10 | + |
| 11 | +func txReadCharacters(ctx context.Context, offset Offset) neo4j.ManagedTransactionWorkT[[]Character] { |
| 12 | + query := repository.NewQuery(offset, |
| 13 | + ` |
| 14 | +MATCH (character:Character) |
| 15 | +RETURN character |
| 16 | +ORDER BY character.debut |
| 17 | +SKIP $skip |
| 18 | +LIMIT $limit |
| 19 | + `) |
| 20 | + return func(tx neo4j.ManagedTransaction) ([]Character, error) { |
| 21 | + res, err := query.Execute(tx, ctx) |
| 22 | + if err != nil { |
| 23 | + return nil, err |
| 24 | + } |
| 25 | + characters := make([]Character, offset.Limit) |
| 26 | + i := 0 |
| 27 | + for rec, yieldErr := range res.Records(ctx) { |
| 28 | + if yieldErr != nil { |
| 29 | + log.Printf("Error while iterating DB records, %v", yieldErr) |
| 30 | + i++ |
| 31 | + continue |
| 32 | + } |
| 33 | + rowMap, rowMapOk := rec.AsMap()["character"] |
| 34 | + rowCharacter, rowCharacterOk := rowMap.(neo4j.Node) |
| 35 | + if !rowMapOk || !rowCharacterOk { |
| 36 | + log.Printf("Bad row from db %+v\n", rec) |
| 37 | + i++ |
| 38 | + continue |
| 39 | + } |
| 40 | + propsMap := rowCharacter.GetProperties() |
| 41 | + name := propsMap["name"] |
| 42 | + id := propsMap["id"] |
| 43 | + description := propsMap["description"] |
| 44 | + debut := propsMap["debut"] |
| 45 | + nameStr := name.(string) |
| 46 | + idStr := id.(string) |
| 47 | + debutNum := int(debut.(float64)) |
| 48 | + descrStr := description.(string) |
| 49 | + characters[i] = Character{ |
| 50 | + Name: nameStr, |
| 51 | + Id: idStr, |
| 52 | + Debut: debutNum, |
| 53 | + Description: descrStr, |
| 54 | + } |
| 55 | + i++ |
| 56 | + } |
| 57 | + return characters, nil |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func ReadCharacters(ctx context.Context, offset Offset) ([]Character, error) { |
| 62 | + return repository.ExecuteReadWithDriver(ctx, txReadCharacters(ctx, offset)) |
| 63 | +} |
0 commit comments