diff --git a/etc/tweeting.yaml b/etc/tweeting.yaml index 61ad1c3..8dfae0b 100644 --- a/etc/tweeting.yaml +++ b/etc/tweeting.yaml @@ -22,7 +22,7 @@ RedisCluster: Type: cluster Neo4J: - URI: neo4j://localhost:7687 + URI: bolt://localhost:7687 Username: neo4j Password: yyyytttt MaxConnectionPoolSize: 20 diff --git a/internal/lib/neo4j/neo4j.go b/internal/lib/neo4j/neo4j.go index 736099d..d8178e7 100644 --- a/internal/lib/neo4j/neo4j.go +++ b/internal/lib/neo4j/neo4j.go @@ -19,6 +19,12 @@ func NewNeo4J(conf *Config, opts ...Option) *Client { neo4ji := &Client{ neo4jConf: driverConfig, + serviceConf: Config{ + URI: conf.URI, + Username: conf.Username, + Password: conf.Password, + LogLevel: conf.LogLevel, + }, } for _, opt := range opts { @@ -35,13 +41,6 @@ func (c *Client) Conn() (neo4j.DriverWithContext, error) { if err != nil { return nil, fmt.Errorf("neo4j driver initialization error: %w", err) } - defer func(driver neo4j.DriverWithContext, ctx context.Context) { - err := driver.Close(ctx) - if err != nil { - // fmt - - } - }(driver, context.Background()) ctx := context.Background() // Verify the connection to Neo4j. diff --git a/internal/logic/socialnetworkservice/add_user_to_network_logic.go b/internal/logic/socialnetworkservice/add_user_to_network_logic.go index 69f51cb..1840a3c 100644 --- a/internal/logic/socialnetworkservice/add_user_to_network_logic.go +++ b/internal/logic/socialnetworkservice/add_user_to_network_logic.go @@ -28,6 +28,7 @@ func (l *AddUserToNetworkLogic) AddUserToNetwork(in *tweeting.AddUserToNetworkRe // todo: add your logic here and delete this line err := l.svcCtx.SocialNetworkRepository.CreateUserNode(l.ctx, in.GetUid()) if err != nil { + fmt.Println("gg88g88g8", err) return nil, err } fmt.Println(err) diff --git a/internal/repository/social_network.go b/internal/repository/social_network.go index 5adb29e..45ea0f8 100644 --- a/internal/repository/social_network.go +++ b/internal/repository/social_network.go @@ -5,10 +5,7 @@ import ( "app-cloudep-tweeting-service/internal/domain/repository" client4J "app-cloudep-tweeting-service/internal/lib/neo4j" "context" - "fmt" - - ers "code.30cm.net/digimon/library-go/errs" - n4 "github.com/neo4j/neo4j-go-driver/v5/neo4j" + "github.com/neo4j/neo4j-go-driver/v5/neo4j" ) type SocialNetworkParam struct { @@ -29,25 +26,28 @@ func MustSocialNetworkRepository(param SocialNetworkParam) repository.SocialNetw } func (s SocialNetworkRepository) CreateUserNode(ctx context.Context, uid string) error { - // 執行 Cypher - conn, err := s.neo4jClient.Conn() + session, err := s.neo4jClient.Conn() if err != nil { - return ers.DBError("failed to connect to node4j", err.Error()) + return err } + defer session.Close(ctx) - insert := map[string]any{ + params := map[string]interface{}{ "uid": uid, } - result, err := conn.NewSession(ctx, n4.SessionConfig{ - AccessMode: n4.AccessModeWrite, - }).Run(ctx, "CREATE (n:Person {name: $name}) RETURN n", insert) + _, err = session.NewSession(ctx, neo4j.SessionConfig{ + AccessMode: neo4j.AccessModeWrite, + }).Run(ctx, "CREATE (n:User {uid: $uid}) RETURN n", params) if err != nil { return err } - fmt.Println(result.Keys()) + // // 處理結果 + // if run.Next(ctx) { + // node := run.Record().AsMap() + // fmt.Printf("Created Node: %v\n", node) + // } return nil - } diff --git a/internal/svc/service_context.go b/internal/svc/service_context.go index 0c63516..e70b793 100644 --- a/internal/svc/service_context.go +++ b/internal/svc/service_context.go @@ -6,7 +6,6 @@ import ( "app-cloudep-tweeting-service/internal/lib/neo4j" model "app-cloudep-tweeting-service/internal/model/mongo" "app-cloudep-tweeting-service/internal/repository" - "github.com/zeromicro/go-zero/core/stores/redis" vi "code.30cm.net/digimon/library-go/validator" diff --git a/tweeting.go b/tweeting.go index cde5e9e..173ffd1 100644 --- a/tweeting.go +++ b/tweeting.go @@ -6,7 +6,10 @@ import ( "app-cloudep-tweeting-service/gen_result/pb/tweeting" "app-cloudep-tweeting-service/internal/config" + commentserviceServer "app-cloudep-tweeting-service/internal/server/commentservice" postserviceServer "app-cloudep-tweeting-service/internal/server/postservice" + socialnetworkserviceServer "app-cloudep-tweeting-service/internal/server/socialnetworkservice" + timelineserviceServer "app-cloudep-tweeting-service/internal/server/timelineservice" "app-cloudep-tweeting-service/internal/svc" "github.com/zeromicro/go-zero/core/conf" @@ -18,8 +21,6 @@ import ( var configFile = flag.String("f", "etc/tweeting.yaml", "the config file") -// TODO 要把每一個錯誤代碼修改的更詳細,目前都資料庫錯誤 - func main() { flag.Parse() @@ -29,6 +30,9 @@ func main() { s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) { tweeting.RegisterPostServiceServer(grpcServer, postserviceServer.NewPostServiceServer(ctx)) + tweeting.RegisterCommentServiceServer(grpcServer, commentserviceServer.NewCommentServiceServer(ctx)) + tweeting.RegisterTimelineServiceServer(grpcServer, timelineserviceServer.NewTimelineServiceServer(ctx)) + tweeting.RegisterSocialNetworkServiceServer(grpcServer, socialnetworkserviceServer.NewSocialNetworkServiceServer(ctx)) if c.Mode == service.DevMode || c.Mode == service.TestMode { reflection.Register(grpcServer)