Skip to content

Commit 2d8901c

Browse files
committed
add NewQuery
1 parent 0bdf252 commit 2d8901c

5 files changed

Lines changed: 27 additions & 19 deletions

File tree

internal/repository/authrep/main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@ import (
99
)
1010

1111
func txUsrExistsAtomically(ctx context.Context, user User) neo4j.ManagedTransactionWorkT[bool] {
12-
query := queryComputeUserExistsAtomically(user)
12+
query := repository.NewQuery(
13+
user,
14+
`
15+
MATCH (u:User {email:$email, password:$password})
16+
RETURN count(u) > 0 as exists
17+
`,
18+
)
1319

1420
return func(tx neo4j.ManagedTransaction) (bool, error) {
1521
res, err := query.Execute(tx, ctx)

internal/repository/authrep/queries.go

Lines changed: 0 additions & 18 deletions
This file was deleted.

internal/repository/authrep/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,10 @@ type User struct {
44
Email string
55
Password string
66
}
7+
8+
func (src User) ToParams() map[string]any {
9+
return map[string]any{
10+
"email": src.Email,
11+
"password": src.Password,
12+
}
13+
}

internal/repository/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ func ExecuteReadWithDriver[T any](ctx context.Context, tx neo4j.ManagedTransacti
1515
defer session.Close(readCtx)
1616
return neo4j.ExecuteRead(readCtx, session, tx)
1717
}
18+
19+
func NewQuery(data IQueryParams, query string) QueryRunner {
20+
return func(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.ResultWithContext, error) {
21+
return tx.Run(
22+
ctx,
23+
query,
24+
data.ToParams())
25+
}
26+
}

internal/repository/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ type QueryRunner func(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.R
1111
func (src QueryRunner) Execute(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.ResultWithContext, error) {
1212
return src(tx, ctx)
1313
}
14+
15+
type IQueryParams interface {
16+
ToParams() map[string]any
17+
}

0 commit comments

Comments
 (0)