Skip to content

Commit 0bdf252

Browse files
committed
reorganize repository
1 parent 3f7a79a commit 0bdf252

8 files changed

Lines changed: 94 additions & 36 deletions

File tree

cmd/api/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package api
33
import (
44
"net/http"
55

6-
"github.com/UltimateForm/gopen-api/internal/authrep"
76
"github.com/UltimateForm/gopen-api/internal/core"
7+
"github.com/UltimateForm/gopen-api/internal/repository/authrep"
88
)
99

1010
type LoginRequest struct {

internal/authrep/main.go

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

internal/db/main.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
88
)
99

10-
func init() {
11-
log.Println("DB PACKAGE INIT")
12-
}
10+
var Driver neo4j.DriverWithContext
1311

14-
func GetDriver() (neo4j.DriverWithContext, error) {
12+
func init() {
1513
dbCfg := config.LoadDbConfig()
1614
driver, driverErr := neo4j.NewDriverWithContext(dbCfg.Uri, neo4j.BasicAuth(dbCfg.Username, dbCfg.Password, ""))
17-
return driver, driverErr
15+
if driverErr != nil {
16+
panic(driverErr)
17+
}
18+
Driver = driver
19+
log.Println("DB PACKAGE INIT")
1820
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package authrep
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/UltimateForm/gopen-api/internal/repository"
8+
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
9+
)
10+
11+
func txUsrExistsAtomically(ctx context.Context, user User) neo4j.ManagedTransactionWorkT[bool] {
12+
query := queryComputeUserExistsAtomically(user)
13+
14+
return func(tx neo4j.ManagedTransaction) (bool, error) {
15+
res, err := query.Execute(tx, ctx)
16+
if err != nil {
17+
return false, err
18+
}
19+
record, singleErr := res.Single(ctx)
20+
if singleErr != nil {
21+
return false, singleErr
22+
}
23+
exists, found := record.Get("exists")
24+
if !found {
25+
return false, nil
26+
}
27+
boolValue, isBoolValue := exists.(bool)
28+
if !isBoolValue {
29+
return false, errors.New("expected 'exists' bool value from DB row")
30+
}
31+
return boolValue, nil
32+
}
33+
34+
}
35+
36+
func UserExistsAtomically(ctx context.Context, user User) (bool, error) {
37+
return repository.ExecuteReadWithDriver(ctx, txUsrExistsAtomically(ctx, user))
38+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package authrep
2+
3+
import (
4+
"context"
5+
6+
"github.com/UltimateForm/gopen-api/internal/repository"
7+
8+
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
9+
)
10+
11+
func queryComputeUserExistsAtomically(user User) repository.QueryRunner {
12+
return func(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.ResultWithContext, error) {
13+
return tx.Run(
14+
ctx,
15+
"MATCH (u:User {email:$email, password:$password}) RETURN count(u) > 0 as exists",
16+
map[string]any{"email": user.Email, "password": user.Password})
17+
}
18+
}

internal/repository/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/UltimateForm/gopen-api/internal/db"
8+
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
9+
)
10+
11+
func ExecuteReadWithDriver[T any](ctx context.Context, tx neo4j.ManagedTransactionWorkT[T]) (T, error) {
12+
readCtx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
13+
defer cancel()
14+
session := db.Driver.NewSession(readCtx, neo4j.SessionConfig{})
15+
defer session.Close(readCtx)
16+
return neo4j.ExecuteRead(readCtx, session, tx)
17+
}

internal/repository/types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
7+
)
8+
9+
type QueryRunner func(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.ResultWithContext, error)
10+
11+
func (src QueryRunner) Execute(tx neo4j.ManagedTransaction, ctx context.Context) (neo4j.ResultWithContext, error) {
12+
return src(tx, ctx)
13+
}

0 commit comments

Comments
 (0)