forked from CodelyTV/java-ddd-example
-
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.
- Loading branch information
1 parent
2e7ce89
commit 3df63eb
Showing
10 changed files
with
302 additions
and
2 deletions.
There are no files selected for viewing
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
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 @@ | ||
# ELASTIC - Search | ||
POST localhost:9200/backoffice_courses/_search | ||
Content-Type: application/json | ||
|
||
{ | ||
"query": { | ||
"term": { | ||
"name": "Pepe" | ||
} | ||
} | ||
} | ||
|
||
### | ||
# ELASTIC - Search | ||
POST localhost:9200/backoffice_courses/_search | ||
Content-Type: application/json | ||
|
||
### | ||
|
||
PUT localhost:9200/backoffice_courses/_settings | ||
Content-Type: application/json | ||
|
||
{ | ||
"index": { | ||
"blocks": { | ||
"read_only_allow_delete": "false" | ||
} | ||
} | ||
} | ||
|
||
### |
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
20 changes: 20 additions & 0 deletions
20
src/backoffice/main/resources/database/backoffice/courses.json
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 @@ | ||
{ | ||
"mappings": { | ||
"courses": { | ||
"properties": { | ||
"id": { | ||
"type": "keyword", | ||
"index": true | ||
}, | ||
"name": { | ||
"type": "text", | ||
"index": true | ||
}, | ||
"duration": { | ||
"type": "text", | ||
"index": true | ||
} | ||
} | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
...ackoffice/courses/infrastructure/persistence/ElasticsearchBackofficeCourseRepository.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,39 @@ | ||
package tv.codely.backoffice.courses.infrastructure.persistence; | ||
|
||
import org.springframework.context.annotation.Primary; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourse; | ||
import tv.codely.backoffice.courses.domain.BackofficeCourseRepository; | ||
import tv.codely.shared.domain.Service; | ||
import tv.codely.shared.domain.criteria.Criteria; | ||
import tv.codely.shared.infrastructure.elasticsearch.ElasticsearchClient; | ||
import tv.codely.shared.infrastructure.elasticsearch.ElasticsearchRepository; | ||
|
||
import java.util.List; | ||
|
||
@Primary | ||
@Service | ||
public final class ElasticsearchBackofficeCourseRepository extends ElasticsearchRepository<BackofficeCourse> implements BackofficeCourseRepository { | ||
public ElasticsearchBackofficeCourseRepository(ElasticsearchClient client) { | ||
super(client); | ||
} | ||
|
||
@Override | ||
public void save(BackofficeCourse course) { | ||
persist(course.id(), course.toPrimitives()); | ||
} | ||
|
||
@Override | ||
public List<BackofficeCourse> searchAll() { | ||
return searchAllInElastic(BackofficeCourse::fromPrimitives); | ||
} | ||
|
||
@Override | ||
public List<BackofficeCourse> matching(Criteria criteria) { | ||
return searchAllInElastic(BackofficeCourse::fromPrimitives); | ||
} | ||
|
||
@Override | ||
protected String moduleName() { | ||
return "courses"; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...ly/backoffice/shared/infrastructure/persistence/BackofficeElasticsearchConfiguration.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,83 @@ | ||
package tv.codely.backoffice.shared.infrastructure.persistence; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.Request; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.client.indices.CreateIndexRequest; | ||
import org.elasticsearch.client.indices.GetIndexRequest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.ResourcePatternResolver; | ||
import tv.codely.shared.infrastructure.config.Parameter; | ||
import tv.codely.shared.infrastructure.config.ParameterNotExist; | ||
import tv.codely.shared.infrastructure.elasticsearch.ElasticsearchClient; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.Scanner; | ||
|
||
@Configuration | ||
public class BackofficeElasticsearchConfiguration { | ||
private final Parameter config; | ||
private final ResourcePatternResolver resourceResolver; | ||
|
||
public BackofficeElasticsearchConfiguration(Parameter config, ResourcePatternResolver resourceResolver) { | ||
this.config = config; | ||
this.resourceResolver = resourceResolver; | ||
} | ||
|
||
@Bean | ||
public ElasticsearchClient elasticsearchClient() throws ParameterNotExist, IOException { | ||
ElasticsearchClient client = new ElasticsearchClient( | ||
new RestHighLevelClient( | ||
RestClient.builder( | ||
new HttpHost( | ||
config.get("BACKOFFICE_ELASTICSEARCH_HOST"), | ||
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"), | ||
"http" | ||
) | ||
) | ||
), | ||
RestClient.builder( | ||
new HttpHost( | ||
config.get("BACKOFFICE_ELASTICSEARCH_HOST"), | ||
config.getInt("BACKOFFICE_ELASTICSEARCH_PORT"), | ||
"http" | ||
)).build(), | ||
config.get("BACKOFFICE_ELASTICSEARCH_INDEX_PREFIX") | ||
); | ||
|
||
generateIndexIfNotExists(client, "backoffice"); | ||
|
||
return client; | ||
} | ||
|
||
private void generateIndexIfNotExists(ElasticsearchClient client, String contextName) throws IOException { | ||
Resource[] jsonsIndexes = resourceResolver.getResources( | ||
String.format("classpath:database/%s/*.json", contextName) | ||
); | ||
|
||
for (Resource jsonIndex : jsonsIndexes) { | ||
String indexName = Objects.requireNonNull(jsonIndex.getFilename()).replace(".json", ""); | ||
|
||
if (!indexExists(indexName, client)) { | ||
String indexBody = new Scanner( | ||
jsonIndex.getInputStream(), | ||
"UTF-8" | ||
).useDelimiter("\\A").next(); | ||
|
||
Request request = new Request("PUT", indexName); | ||
request.setJsonEntity(indexBody); | ||
|
||
client.lowLevelClient().performRequest(request); | ||
} | ||
} | ||
} | ||
|
||
private boolean indexExists(String indexName, ElasticsearchClient client) throws IOException { | ||
return client.highLevelClient().indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/shared/main/tv/codely/shared/infrastructure/elasticsearch/ElasticsearchClient.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 tv.codely.shared.infrastructure.elasticsearch; | ||
|
||
import org.elasticsearch.action.index.IndexRequest; | ||
import org.elasticsearch.client.RequestOptions; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.HashMap; | ||
|
||
public final class ElasticsearchClient { | ||
private final RestHighLevelClient highLevelClient; | ||
private final RestClient lowLevelClient; | ||
private final String indexPrefix; | ||
|
||
public ElasticsearchClient(RestHighLevelClient highLevelClient, RestClient lowLevelClient, String indexPrefix) { | ||
this.highLevelClient = highLevelClient; | ||
this.lowLevelClient = lowLevelClient; | ||
this.indexPrefix = indexPrefix; | ||
} | ||
|
||
public RestHighLevelClient highLevelClient() { | ||
return highLevelClient; | ||
} | ||
|
||
public RestClient lowLevelClient() { | ||
return lowLevelClient; | ||
} | ||
|
||
public String indexPrefix() { | ||
return indexPrefix; | ||
} | ||
|
||
public void persist(String moduleName, String id, HashMap<String, Serializable> plainBody) throws IOException { | ||
IndexRequest request = new IndexRequest(indexFor(moduleName), moduleName, id).source(plainBody); | ||
|
||
highLevelClient().index(request, RequestOptions.DEFAULT); | ||
} | ||
|
||
public String indexFor(String moduleName) { | ||
return String.format("%s_%s", indexPrefix(), moduleName); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/shared/main/tv/codely/shared/infrastructure/elasticsearch/ElasticsearchRepository.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 tv.codely.shared.infrastructure.elasticsearch; | ||
|
||
import org.elasticsearch.action.search.SearchRequest; | ||
import org.elasticsearch.action.search.SearchResponse; | ||
import org.elasticsearch.client.RequestOptions; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
public abstract class ElasticsearchRepository<T> { | ||
private final ElasticsearchClient client; | ||
|
||
public ElasticsearchRepository(ElasticsearchClient client) { | ||
this.client = client; | ||
} | ||
|
||
abstract protected String moduleName(); | ||
|
||
protected List<T> searchAllInElastic(Function<Map<String, Object>, T> unserializer) { | ||
SearchRequest request = new SearchRequest(client.indexFor(moduleName())); | ||
try { | ||
SearchResponse response = client.highLevelClient().search(request, RequestOptions.DEFAULT); | ||
|
||
return Arrays.stream(response.getHits().getHits()) | ||
.map(hit -> unserializer.apply(hit.getSourceAsMap())) | ||
.collect(Collectors.toList()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return Collections.emptyList(); | ||
} | ||
|
||
protected void persist(String id, HashMap<String, Serializable> plainBody) { | ||
try { | ||
client.persist(moduleName(), id, plainBody); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |