From 768930909bbcfce2616725c24a44d2973a86ec5a Mon Sep 17 00:00:00 2001 From: sehansi-9 Date: Fri, 25 Jul 2025 15:50:35 +0530 Subject: [PATCH 1/2] Refactor: separate metadata handling for creation and update --- nexoan/crud-api/cmd/server/service.go | 4 +- .../db/repository/mongo/metadata_handler.go | 54 +++++++++++-------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/nexoan/crud-api/cmd/server/service.go b/nexoan/crud-api/cmd/server/service.go index 0873ca81..36ed0302 100644 --- a/nexoan/crud-api/cmd/server/service.go +++ b/nexoan/crud-api/cmd/server/service.go @@ -34,7 +34,7 @@ func (s *Server) CreateEntity(ctx context.Context, req *pb.Entity) (*pb.Entity, // Always save the entity in MongoDB, even if it has no metadata // The HandleMetadata function will only process it if it has metadata // FIXME: https://github.com/LDFLK/nexoan/issues/120 - err := s.mongoRepo.HandleMetadata(ctx, req.Id, req) + err := s.mongoRepo.HandleMetadataCreation(ctx, req.Id, req) if err != nil { log.Printf("[server.CreateEntity] Error saving metadata in MongoDB: %v", err) return nil, err @@ -180,7 +180,7 @@ func (s *Server) UpdateEntity(ctx context.Context, req *pb.UpdateEntityRequest) var metadata map[string]*anypb.Any // Pass the ID and metadata to HandleMetadata - err := s.mongoRepo.HandleMetadata(ctx, updateEntityID, updateEntity) + err := s.mongoRepo.HandleMetadataUpdate(ctx, updateEntityID, updateEntity.GetMetadata()) if err != nil { // Log error and continue with empty metadata log.Printf("[server.UpdateEntity] Error updating metadata for entity %s: %v", updateEntityID, err) diff --git a/nexoan/crud-api/db/repository/mongo/metadata_handler.go b/nexoan/crud-api/db/repository/mongo/metadata_handler.go index 7f260024..e4778962 100644 --- a/nexoan/crud-api/db/repository/mongo/metadata_handler.go +++ b/nexoan/crud-api/db/repository/mongo/metadata_handler.go @@ -12,39 +12,51 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -// Add this function to handle metadata operations -func (repo *MongoRepository) HandleMetadata(ctx context.Context, entityId string, entity *pb.Entity) error { - // Skip operations if no metadata is provided +// ...existing code... + +// CreateMetadata creates a new entity with metadata if it doesn't exist. +func (repo *MongoRepository) HandleMetadataCreation(ctx context.Context, entityId string, entity *pb.Entity) error { if entity == nil || entity.GetMetadata() == nil || len(entity.GetMetadata()) == 0 { return nil } - // Check if entity exists existingEntity, err := repo.ReadEntity(ctx, entityId) if err != nil && err != mongo.ErrNoDocuments { return err } + if existingEntity != nil { + return mongo.ErrNoDocuments // Or return a custom error: entity already exists + } + newEntity := &pb.Entity{ + Id: entityId, + Metadata: entity.GetMetadata(), + Kind: entity.Kind, + Created: entity.Created, + Terminated: entity.Terminated, + Name: entity.Name, + Attributes: entity.Attributes, + Relationships: entity.Relationships, + } + _, err = repo.CreateEntity(ctx, newEntity) + return err +} + +// UpdateMetadata updates metadata for an existing entity. +func (repo *MongoRepository) HandleMetadataUpdate(ctx context.Context, entityId string, metadata map[string]*anypb.Any) error { + if len(metadata) == 0 { + return nil + } + + existingEntity, err := repo.ReadEntity(ctx, entityId) + if err != nil { + return err + } if existingEntity == nil { - // Create new entity with all fields including metadata - newEntity := &pb.Entity{ - Id: entityId, - Metadata: entity.GetMetadata(), - Kind: entity.Kind, - Created: entity.Created, - Terminated: entity.Terminated, - Name: entity.Name, - Attributes: entity.Attributes, - Relationships: entity.Relationships, - } - _, err = repo.CreateEntity(ctx, newEntity) - } else { - // Update existing entity's metadata - // TODO: Should we choose _id for placing our id or should we use id field separately and use that. - // Because then it is going to be reading or deleting or whatever by filtering using an attribute not the id of the object. - _, err = repo.UpdateEntity(ctx, existingEntity.Id, bson.M{"metadata": entity.GetMetadata()}) + return mongo.ErrNoDocuments // Or return a custom error: entity not found } + _, err = repo.UpdateEntity(ctx, entityId, bson.M{"metadata": metadata}) return err } From 3d006bf82b02f3bc10f5e27c98f5b90265118c74 Mon Sep 17 00:00:00 2001 From: sehansi-9 Date: Fri, 25 Jul 2025 16:25:45 +0530 Subject: [PATCH 2/2] Refactor: refactor the metadata update's parameters to pass the entity object for consistency --- nexoan/crud-api/cmd/server/service.go | 2 +- .../crud-api/db/repository/mongo/metadata_handler.go | 12 +++++------- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/nexoan/crud-api/cmd/server/service.go b/nexoan/crud-api/cmd/server/service.go index 36ed0302..88944649 100644 --- a/nexoan/crud-api/cmd/server/service.go +++ b/nexoan/crud-api/cmd/server/service.go @@ -180,7 +180,7 @@ func (s *Server) UpdateEntity(ctx context.Context, req *pb.UpdateEntityRequest) var metadata map[string]*anypb.Any // Pass the ID and metadata to HandleMetadata - err := s.mongoRepo.HandleMetadataUpdate(ctx, updateEntityID, updateEntity.GetMetadata()) + err := s.mongoRepo.HandleMetadataUpdate(ctx, updateEntityID, updateEntity) if err != nil { // Log error and continue with empty metadata log.Printf("[server.UpdateEntity] Error updating metadata for entity %s: %v", updateEntityID, err) diff --git a/nexoan/crud-api/db/repository/mongo/metadata_handler.go b/nexoan/crud-api/db/repository/mongo/metadata_handler.go index e4778962..665b21ee 100644 --- a/nexoan/crud-api/db/repository/mongo/metadata_handler.go +++ b/nexoan/crud-api/db/repository/mongo/metadata_handler.go @@ -12,8 +12,6 @@ import ( "go.mongodb.org/mongo-driver/mongo" ) -// ...existing code... - // CreateMetadata creates a new entity with metadata if it doesn't exist. func (repo *MongoRepository) HandleMetadataCreation(ctx context.Context, entityId string, entity *pb.Entity) error { if entity == nil || entity.GetMetadata() == nil || len(entity.GetMetadata()) == 0 { @@ -25,7 +23,7 @@ func (repo *MongoRepository) HandleMetadataCreation(ctx context.Context, entityI return err } if existingEntity != nil { - return mongo.ErrNoDocuments // Or return a custom error: entity already exists + return mongo.ErrNoDocuments } newEntity := &pb.Entity{ @@ -43,8 +41,8 @@ func (repo *MongoRepository) HandleMetadataCreation(ctx context.Context, entityI } // UpdateMetadata updates metadata for an existing entity. -func (repo *MongoRepository) HandleMetadataUpdate(ctx context.Context, entityId string, metadata map[string]*anypb.Any) error { - if len(metadata) == 0 { +func (repo *MongoRepository) HandleMetadataUpdate(ctx context.Context, entityId string, entity *pb.Entity) error { + if len(entity.GetMetadata()) == 0 { return nil } @@ -53,10 +51,10 @@ func (repo *MongoRepository) HandleMetadataUpdate(ctx context.Context, entityId return err } if existingEntity == nil { - return mongo.ErrNoDocuments // Or return a custom error: entity not found + return mongo.ErrNoDocuments } - _, err = repo.UpdateEntity(ctx, entityId, bson.M{"metadata": metadata}) + _, err = repo.UpdateEntity(ctx, entityId, bson.M{"metadata": entity.GetMetadata()}) return err }