Skip to content

Commit

Permalink
Merge pull request #2 from FalconSocial/improve_assignment
Browse files Browse the repository at this point in the history
Improve workshop-assignment
  • Loading branch information
SzaboAdamImre authored Feb 22, 2024
2 parents 96a10b7 + 56a59f8 commit 30eb499
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Some solutions can be done by adding/modifying a few lines of code, we would ask
Other, larger solutions don't need implementation, just discuss it like you would discuss it with a colleague.

You don't need to check for creating new, useful endpoints or other domain-changes, we are interested in what code or architectural changes would you make in the repository.
Also keep a look out for scalability and stability issues.

## Project description

Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_CONFLUENT_TOPIC_REPLICATION_FACTOR: 1
KAFKA_CREATE_TOPICS: "indexing-app:1:1"
KAFKA_CREATE_TOPICS: "match-events:1:1"

database:
image: postgres:latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@
@Service
public class ChuckNorrisIndexingService extends IndexingService {

public static final String NETWORK_NAME = "chucknorris";
private final RestTemplate restTemplate = new RestTemplate();
public ChuckNorrisIndexingService(IndexingAppConfiguration configuration, ContentRepository contentRepository, QueryRepository queryRepository, KafkaTemplate<String, String> kafkaTemplate, ObjectMapper objectMapper) {
super(configuration, contentRepository, queryRepository, kafkaTemplate, objectMapper);
}

@Override
public String getNetwork() {
return "chucknorris";
return NETWORK_NAME;
}

@Override
protected List<Content> loadNewContentsForNetwork() throws URISyntaxException {
// APIDoc: https://api-ninjas.com/api/chucknorris

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("X-Api-Key", this.getConfiguration().apiKey());
RequestEntity request = new RequestEntity<>(requestHeaders, HttpMethod.GET, new URI("https://api.api-ninjas.com/v1/chucknorris"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,28 @@
@Service
public class DadJokeIndexingService extends IndexingService {

public static final String NETWORK_NAME = "dadjoke";
private final RestTemplate restTemplate = new RestTemplate();
public DadJokeIndexingService(IndexingAppConfiguration configuration, ContentRepository contentRepository, QueryRepository queryRepository, KafkaTemplate<String, String> kafkaTemplate, ObjectMapper objectMapper) {
super(configuration, contentRepository, queryRepository, kafkaTemplate, objectMapper);
}

@Override
public String getNetwork() {
return "dadjoke";
return NETWORK_NAME;
}

@Override
protected List<Content> loadNewContentsForNetwork() throws URISyntaxException {
// APIdoc: https://api-ninjas.com/api/dadjokes

HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("X-Api-Key", this.getConfiguration().apiKey());
RequestEntity request = new RequestEntity<>(requestHeaders, HttpMethod.GET, new URI("https://api.api-ninjas.com/v1/dadjokes?limit=5"));
ResponseEntity<List> response = this.restTemplate.exchange(request, List.class);
List<Map> responseBody = response.getBody();
return responseBody.stream()
.map(element -> new Content(null, Instant.now(), "dadjoke", String.valueOf(element.get("joke"))))
.map(element -> new Content(null, Instant.now(), NETWORK_NAME, String.valueOf(element.get("joke"))))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public abstract class IndexingService {
public void indexAndSendNotifications() {
try {
List<Content> contents = this.loadNewContentsForNetwork();
contents.forEach(this::saveContentAndSendNotifications);
contents.forEach(content -> {
Content savedContent = this.contentRepository.save(content);
List<Query> matchingQueries = getMatchingQueries(savedContent);
sendNotificationsForQueries(savedContent, matchingQueries);
});
} catch (Throwable t) {
log.error("Indexing failed with error: ", t);
}
Expand All @@ -45,13 +49,13 @@ public List<Content> getContents() {
return this.contentRepository.findByNetwork(this.getNetwork());
}

private void saveContentAndSendNotifications(Content content) {
this.contentRepository.saveAndFlush(content);

private List<Query> getMatchingQueries(Content content) {
List<Query> matchingQueries = this.queryRepository.findByNetwork(this.getNetwork()).stream()
.filter(query -> content.getContent().toLowerCase().contains(query.getQueryText()))
.collect(Collectors.toList());

return matchingQueries;
}
private void sendNotificationsForQueries(Content content, List<Query> matchingQueries) {
matchingQueries.stream()
.map(query -> new MatchedQueryEventDTO(query.getId(), this.getNetwork(), content.getContent()))
.forEach(event -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

Expand All @@ -27,7 +26,10 @@ public class IndexingController {
@GetMapping("/index/{network}/index")
// Index data for the given network, and match queries for found new contents
public ResponseEntity indexData(@PathVariable String network) {
new Thread(() -> this.indexingServices.stream().filter(service -> service.getNetwork().equalsIgnoreCase(network)).forEach(IndexingService::indexAndSendNotifications)).run();
new Thread(() -> this.indexingServices.stream()
.filter(service -> service.getNetwork().equalsIgnoreCase(network))
.forEach(IndexingService::indexAndSendNotifications))
.run();
return ResponseEntity.ok().build();
}

Expand All @@ -40,14 +42,19 @@ public ResponseEntity<List<Content>> getContents(@PathVariable String network) {
.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}

@PostMapping("/query/create")
// Create a new query for the user
public ResponseEntity<UUID> createQuery(@RequestParam String network, @RequestParam String matchText, @RequestParam String user) {
public ResponseEntity<UUID> createQuery(@RequestParam String network,
@RequestParam String matchText,
@RequestParam String user) {
return ResponseEntity.ok(this.queryService.create(network, matchText, user));
}

@PostMapping("/query/delete")
// Delete queries based on ID or user
public ResponseEntity<Query> calculateAverages(@RequestParam(required = false) String user, @RequestParam(required = false) UUID id) {
public ResponseEntity<Query> deleteQuery(@RequestParam(required = false) String user,
@RequestParam(required = false) UUID id) {
if (user == null) {
this.queryService.delete(id);
} else {
Expand Down

0 comments on commit 30eb499

Please sign in to comment.