Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions nexoan/crud-api/cmd/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,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
Expand Down Expand Up @@ -241,7 +241,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)
if err != nil {
// Log error and continue with empty metadata
log.Printf("[server.UpdateEntity] Error updating metadata for entity %s: %v", updateEntityID, err)
Expand Down
52 changes: 31 additions & 21 deletions nexoan/crud-api/db/repository/mongo/metadata_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,49 @@ 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
// 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
}

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, entity *pb.Entity) error {
if len(entity.GetMetadata()) == 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
}

_, err = repo.UpdateEntity(ctx, entityId, bson.M{"metadata": entity.GetMetadata()})
return err
}

Expand Down
Loading