Skip to content

Commit

Permalink
avniproject#733 | Multiple changes (Refactoring)
Browse files Browse the repository at this point in the history
Split Metabase repositories based on domain.
Change some interfaces to use objects instead of ids.
Remove RestTemplate class
Add stub test class
  • Loading branch information
vinayvenu committed Jun 14, 2024
1 parent 51b79c8 commit 98b4545
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 169 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.avni.server.dao.metabase;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Repository;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Repository
public class BaseMetabaseRepository {
protected final RestTemplate restTemplate;

@Value("${metabase.api.url}")
protected String metabaseApiUrl;

@Value("${metabase.api.key}")
private String apiKey;

public BaseMetabaseRepository(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}

private HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("x-api-key", apiKey);
return headers;
}

protected <T> HttpEntity<T> createHttpEntity(T body) {
HttpHeaders headers = getHeaders();
return new HttpEntity<>(body, headers);
}

protected void sendPutRequest(String url, Map<String, Object> requestBody) {
HttpEntity<Map<String, Object>> entity = createHttpEntity(requestBody);
restTemplate.exchange(url, HttpMethod.PUT, entity, Map.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.Collection;
import org.avni.server.domain.metabase.CollectionResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;

public class CollectionRepository extends MetabaseRepository{
public CollectionRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public CollectionResponse save(Collection collection) {
String url = metabaseApiUrl + "/collection";
HttpEntity<Collection> entity = createHttpEntity(collection);
CollectionResponse response = restTemplate.postForObject(url, entity, CollectionResponse.class);

return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.Database;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Repository;

@Repository
public class DatabaseRepository extends MetabaseRepository{
public DatabaseRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public Database save(Database database) {
String url = metabaseApiUrl + "/database";
HttpEntity<Database> entity = createHttpEntity(database);
Database response = restTemplate.postForObject(url, entity, Database.class);

return new Database(response.getId(), database.getName(), database.getEngine(), database.getDetails());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.avni.server.dao.metabase;

import org.avni.server.domain.metabase.CollectionPermissions;
import org.avni.server.domain.metabase.Permissions;
import org.avni.server.domain.metabase.PermissionsGroup;
import org.avni.server.domain.metabase.PermissionsGroupResponse;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;

import java.util.Map;

@Repository
public class MetabaseRepository extends BaseMetabaseRepository{

public MetabaseRepository(RestTemplateBuilder restTemplateBuilder) {
super(restTemplateBuilder);
}

public PermissionsGroupResponse createPermissionsGroup(PermissionsGroup permissionsGroup) {
String url = metabaseApiUrl + "/permissions/group";
HttpEntity<PermissionsGroup> entity = createHttpEntity(permissionsGroup);
PermissionsGroupResponse response = restTemplate.postForObject(url, entity, PermissionsGroupResponse.class);

return response;
}

public Map<String, Object> getPermissionsGraph() {
String url = metabaseApiUrl + "/permissions/graph";
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, createHttpEntity(null), Map.class);
return response.getBody();
}

public Map<String, Object> getCollectionPermissionsGraph() {
String url = metabaseApiUrl + "/collection/graph";
ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.GET, createHttpEntity(null), Map.class);
return response.getBody();
}

public void assignDatabasePermissions(Permissions permissions, int groupId, int databaseId) {
permissions.updatePermissionsGraph(groupId, databaseId);
String url = metabaseApiUrl + "/permissions/graph";
sendPutRequest(url, permissions.getPermissionsGraph());
}

public void updateCollectionPermissions(CollectionPermissions collectionPermissions, int groupId, int collectionId) {
collectionPermissions.updatePermissionsGraph(groupId, collectionId);
String url = metabaseApiUrl + "/collection/graph";
sendPutRequest(url, collectionPermissions.getPermissionsGraph());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.avni.server.dao.metabase;

public class PermissionsRepository {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.avni.server.domain.metabase;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AvniDatabase {
@Value("${avni.database.server}")
private String avniDatabaseServer;

@Value("${avni.database.port}")
private String avniDatabasePort;

@Value("${avni.database}")
private String avniDatabaseName;

public AvniDatabase() {
}

public String getAvniDatabaseServer() {
return avniDatabaseServer;
}

public String getAvniDatabasePort() {
return avniDatabasePort;
}

public String getAvniDatabaseName() {
return avniDatabaseName;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package org.avni.server.domain.metabase;

public class Database {
private final Integer id;
private String name;
private String engine;
private DatabaseDetails details;

public Database(String name, String engine, DatabaseDetails details) {
this(null, name, engine, details);
}

public Database(Integer id, String name, String engine, DatabaseDetails details) {
this.id = id;
this.name = name;
this.engine = engine;
this.details = details;
}

public Integer getId() {
return id;
}
public String getName() {
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public DatabaseDetails(String host, String port, String db, String user) {
this.user = user;
}


public DatabaseDetails(AvniDatabase avniDatabase, String dbUser) {
this(avniDatabase.getAvniDatabaseServer(), avniDatabase.getAvniDatabasePort(), avniDatabase.getAvniDatabaseName(), dbUser);
}

public String getHost() {
return host;
}
Expand All @@ -32,4 +35,3 @@ public String getUser() {


}

This file was deleted.

Loading

0 comments on commit 98b4545

Please sign in to comment.