Skip to content

Commit

Permalink
create a new package to store api files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cesare De Cal committed Jul 4, 2024
1 parent 24e4370 commit 4b13d16
Show file tree
Hide file tree
Showing 55 changed files with 43 additions and 50 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified backend/bin/main/com/cesaredecal/Application.class
Binary file not shown.
Binary file removed backend/bin/main/com/cesaredecal/StarWarsClient.class
Binary file not shown.
Binary file modified backend/bin/main/com/cesaredecal/StarWarsController.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 0 additions & 6 deletions backend/src/main/java/com/cesaredecal/Application.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package com.cesaredecal;

import io.micronaut.runtime.Micronaut;
import io.micronaut.runtime.event.ApplicationStartupEvent;
import io.micronaut.runtime.event.annotation.EventListener;

public class Application {

public static void main(String[] args) {
Micronaut.run(Application.class, args);
}

@EventListener
void onStartup(ApplicationStartupEvent event) {
}
}
18 changes: 6 additions & 12 deletions backend/src/main/java/com/cesaredecal/StarWarsController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cesaredecal;

import com.cesaredecal.starwarsapi.StarWarsService;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
Expand All @@ -11,7 +12,7 @@

import reactor.core.publisher.Mono;

import com.cesaredecal.models.DataType;
import com.cesaredecal.starwarsapi.models.DataType;

@Controller("/api/v1")
public class StarWarsController {
Expand All @@ -26,31 +27,24 @@ public StarWarsController(StarWarsService starWarsService) {
// People endpoints

@Get(value = "/people/columns", produces = MediaType.APPLICATION_JSON)
public Mono<String> getPeopleTableColumns() throws IOException {
public Mono<String> getPeopleTableColumns() {
return starWarsService.fetchColumns(DataType.PEOPLE);
}

@Get(value = "/people/data", produces = MediaType.APPLICATION_JSON)
public Mono<String> getPeople(@Nullable @QueryValue String sortBy, @Nullable @QueryValue String sortOrder) throws IOException {
public Mono<String> getPeople(@Nullable @QueryValue String sortBy, @Nullable @QueryValue String sortOrder) {
return starWarsService.fetchTableDataFromStorage(DataType.PEOPLE, sortBy, sortOrder);
}

// Planets endpoints

@Get(value = "/planets/columns", produces = MediaType.APPLICATION_JSON)
public Mono<String> getPlanetsTableColumns() throws IOException {
public Mono<String> getPlanetsTableColumns() {
return starWarsService.fetchColumns(DataType.PLANETS);
}

@Get(value = "/planets/data", produces = MediaType.APPLICATION_JSON)
public Mono<String> getPlanetsTableData(@Nullable @QueryValue String sortBy, @Nullable @QueryValue String sortOrder) throws IOException {
public Mono<String> getPlanetsTableData(@Nullable @QueryValue String sortBy, @Nullable @QueryValue String sortOrder) {
return starWarsService.fetchTableDataFromStorage(DataType.PLANETS, sortBy, sortOrder);
}

// Possible future improvement:
// Add a /ready or /health endpoint so that the platform solution (e.g. Kubernetes) knows when the application can
// be considered to be "ready". In the /ready endpoint we would check if the people_data and planets_data JSON files
// are already available. If yes, we're good to go and the application is ready to start receiving requests.
// This is to avoid the state of the backend app being up but it still hasn't finished download the data from the
// backend. Kubernetes makes this easy to do.
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cesaredecal;
package com.cesaredecal.starwarsapi;

import com.cesaredecal.models.PeopleResponse;
import io.micronaut.serde.ObjectMapper;

import javax.inject.Inject;
Expand All @@ -11,7 +10,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.cesaredecal;
package com.cesaredecal.starwarsapi;

import com.cesaredecal.models.PeopleResponse;
import com.cesaredecal.models.PlanetsResponse;
import com.cesaredecal.starwarsapi.models.PeopleResponse;
import com.cesaredecal.starwarsapi.models.PlanetsResponse;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.QueryValue;
import io.micronaut.http.client.annotation.Client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.cesaredecal;
package com.cesaredecal.starwarsapi;

import com.cesaredecal.models.DataType;
import com.cesaredecal.models.PeopleResponse;
import com.cesaredecal.models.PlanetsResponse;
import com.cesaredecal.starwarsapi.models.DataType;
import com.cesaredecal.starwarsapi.models.PeopleResponse;
import com.cesaredecal.starwarsapi.models.PlanetsResponse;
import io.micronaut.context.event.StartupEvent;
import io.micronaut.core.type.Argument;
import io.micronaut.runtime.event.annotation.EventListener;
Expand Down Expand Up @@ -96,31 +96,39 @@ public void fetchTableDataAndSaveJson(DataType dataType) {

// Read operations

public Mono<String> fetchColumns(DataType dataType) throws IOException {
String fileName = dataType.getColumnsFileName();
return Mono.just(jsonFileService.readJsonFile(fileName));
public Mono<String> fetchColumns(DataType dataType) {
try {
String fileName = dataType.getColumnsFileName();
return Mono.just(jsonFileService.readJsonFile(fileName));
} catch (IOException e) {
return Mono.error(new RuntimeException("Error fetching columns data for " + dataType, e));
}
}

public <T> Mono<String> fetchTableDataFromStorage(DataType dataType, String sortBy, String sortOrder) throws IOException {
String fileName = dataType.getDataFileName();
String jsonContent = jsonFileService.readJsonFile(fileName);
public <T> Mono<String> fetchTableDataFromStorage(DataType dataType, String sortBy, String sortOrder) {
try {
String fileName = dataType.getDataFileName();
String jsonContent = jsonFileService.readJsonFile(fileName);

// Exit early if no sorting is needed
if ((sortBy == null || sortBy.isEmpty()) || (sortOrder == null || sortOrder.isEmpty())) {
return Mono.just(jsonContent);
}
// Exit early if no sorting is needed
if ((sortBy == null || sortBy.isEmpty()) || (sortOrder == null || sortOrder.isEmpty())) {
return Mono.just(jsonContent);
}

Class<T> type = (Class<T>) dataType.getType();
List<T> data = objectMapper.readValue(jsonContent, Argument.listOf(type));
Comparator<T> comparator = getComparator(dataType, sortBy);
Class<T> type = (Class<T>) dataType.getType();
List<T> data = objectMapper.readValue(jsonContent, Argument.listOf(type));
Comparator<T> comparator = getComparator(dataType, sortBy);

if ("desc".equalsIgnoreCase(sortOrder)) {
comparator = comparator.reversed();
}
if ("desc".equalsIgnoreCase(sortOrder)) {
comparator = comparator.reversed();
}

Collections.sort(data, comparator);
Collections.sort(data, comparator);

return Mono.just(objectMapper.writeValueAsString(data));
return Mono.just(objectMapper.writeValueAsString(data));
} catch (IOException e) {
return Mono.error(new RuntimeException("Error fetching table data for " + dataType, e));
}
}

private <T> Comparator<T> getComparator(DataType dataType, String sortBy) {
Expand Down Expand Up @@ -151,5 +159,4 @@ private Comparator<PlanetsResponse.Planet> getPlanetsComparator(String sortBy) {
return Comparator.comparing(PlanetsResponse.Planet::getName);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cesaredecal.models;
package com.cesaredecal.starwarsapi.models;

public enum DataType {
PEOPLE("people_data.json", "people_metadata.json", PeopleResponse.Person.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cesaredecal.models;
package com.cesaredecal.starwarsapi.models;

import io.micronaut.core.annotation.Introspected;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cesaredecal.models;
package com.cesaredecal.starwarsapi.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.micronaut.serde.annotation.Serdeable;
Expand Down

0 comments on commit 4b13d16

Please sign in to comment.