func()

in server/internal/async/entityController.go [55:86]


func (e *EntityController) GetEntity(ctx context.Context, opReq opbus.OperationRequest) (opbus.Entity, error) {
	logger := ctxlogger.GetLogger(ctx)
	logger.Info("Getting entity with id: " + opReq.EntityId)

	queryEntity := fmt.Sprintf("SELECT last_operation_id FROM %s WHERE entity_id = @p1", e.entityTableName)
	rows, err := database.QueryDb(ctx, e.dbClient, queryEntity, opReq.EntityId)
	if err != nil {
		logger.Error("Error executing query: " + err.Error())
		return nil, status.Error(codes.Internal, err.Error())
	}
	defer rows.Close()

	var lastOperationId string
	if rows.Next() {
		err := rows.Scan(&lastOperationId)
		if err != nil {
			logger.Info("Error scanning row: " + err.Error())
			return nil, status.Error(codes.Internal, err.Error())
		}
	} else {
		logger.Error("No rows returned for entityId: " + opReq.EntityId)
		return nil, status.Error(codes.NotFound, "EntityId not found in database.")
	}

	entity, err := e.matcher.CreateEntityInstance(opReq.OperationName, lastOperationId)
	if err != nil {
		logger.Error("Something went wrong creating the entity instance: " + err.Error())
		return nil, err
	}

	return entity, nil
}