forked from avniproject/avni-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avniproject#733 | Multiple changes (Refactoring)
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
Showing
15 changed files
with
218 additions
and
169 deletions.
There are no files selected for viewing
23 changes: 0 additions & 23 deletions
23
avni-server-api/src/main/java/org/avni/server/config/RestTemplateConfig.java
This file was deleted.
Oops, something went wrong.
98 changes: 0 additions & 98 deletions
98
avni-server-api/src/main/java/org/avni/server/dao/MetabaseRepository.java
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
avni-server-api/src/main/java/org/avni/server/dao/metabase/BaseMetabaseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
avni-server-api/src/main/java/org/avni/server/dao/metabase/CollectionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
avni-server-api/src/main/java/org/avni/server/dao/metabase/DatabaseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
avni-server-api/src/main/java/org/avni/server/dao/metabase/MetabaseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
avni-server-api/src/main/java/org/avni/server/dao/metabase/PermissionsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.avni.server.dao.metabase; | ||
|
||
public class PermissionsRepository { | ||
} |
31 changes: 31 additions & 0 deletions
31
avni-server-api/src/main/java/org/avni/server/domain/metabase/AvniDatabase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
avni-server-api/src/main/java/org/avni/server/domain/metabase/Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
avni-server-api/src/main/java/org/avni/server/domain/metabase/DatabaseResponse.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.