Skip to content

Commit

Permalink
avniproject/avni-webapp#1337 | Resolve issues with code blocking perm…
Browse files Browse the repository at this point in the history
…issions setup
  • Loading branch information
himeshr committed Dec 4, 2024
1 parent cbef3ed commit ca8bd64
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.avni.server.domain.metabase.*;
import org.avni.server.util.ObjectMapperSingleton;
import org.avni.server.util.S;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Repository;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class DatabaseRepository extends MetabaseConnector {
Expand Down Expand Up @@ -75,13 +76,14 @@ public CollectionInfoResponse getCollectionByName(Database database) {
try {
String jsonResponse = getForObject(url, String.class);
List<CollectionInfoResponse> collections = objectMapper.readValue(
jsonResponse, new TypeReference<List<CollectionInfoResponse>>() {}
jsonResponse, new TypeReference<List<CollectionInfoResponse>>() {
}
);

return collections.stream()
.filter(collection -> collection.getName().equals(database.getName()))
.findFirst()
.orElseThrow(null);
.orElse(null);
} catch (Exception e) {
throw new RuntimeException("Failed to retrieve collection", e);
}
Expand All @@ -103,7 +105,7 @@ public void createQuestionForTable(Database database, TableDetails tableDetails,
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
getCollectionByName(database).getIdAsInt()
getCollectionForDatabase(database).getIdAsInt()
);

postForObject(metabaseApiUrl + "/card", requestBody.toJson(), JsonNode.class);
Expand All @@ -120,12 +122,20 @@ public void createQuestionForASingleTable(Database database, TableDetails tableD
VisualizationType.TABLE,
null,
objectMapper.createObjectNode(),
getCollectionByName(database).getIdAsInt()
getCollectionForDatabase(database).getIdAsInt()
);

postForObject(metabaseApiUrl + "/card", requestBody.toJson(), JsonNode.class);
}

private CollectionInfoResponse getCollectionForDatabase(Database database) {
CollectionInfoResponse collectionByName = getCollectionByName(database);
if (Objects.isNull(collectionByName)) {
throw new RuntimeException(String.format("Failed to fetch collection for database %s", database.getName()));
}
return collectionByName;
}

public FieldDetails getFieldDetailsByName(Database database, TableDetails tableDetails, FieldDetails fieldDetails) {
List<FieldDetails> fieldsList = getFields(database);
String snakeCaseTableName = S.toSnakeCase(tableDetails.getName());
Expand Down

0 comments on commit ca8bd64

Please sign in to comment.