Skip to content

Instantly share code, notes, and snippets.

@AndreasPizsa
Created November 21, 2017 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasPizsa/153a2aa5eed753cdeb1bd80919952cf8 to your computer and use it in GitHub Desktop.
Save AndreasPizsa/153a2aa5eed753cdeb1bd80919952cf8 to your computer and use it in GitHub Desktop.
Interview
package com.goodgamestest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmetric.spark.authentication.AuthenticationDetails;
import com.qmetric.spark.authentication.BasicAuthenticationFilter;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.post;
public class ApplicationMain {
public static void main(String[] args) {
// helper class to deal with to<->from json conversion
Type inJsonType = new TypeToken<ArrayList<PostRequestHelper>>(){}.getType();
Type outJsonType = new TypeToken<ArrayList<Transaction>>(){}.getType();
// naive database replacement
Map<Long, List<Transaction>> storage = new HashMap<>();
before(new BasicAuthenticationFilter("/customerTransactions/*", new AuthenticationDetails
("test", "123")
));
// method to add new transactions
post("/customerTransactions/:customerID/transactions",
(request, response) ->
{
long customerId;
try {
customerId = Long.parseLong(request.params(":customerID"));
} catch (Exception e) {
response.status(400);
return "Invalid customer id: " + e.getMessage();
}
List<Transaction> incomingTransactions = new ArrayList<>();
try {
String asJson = request.body();
List<PostRequestHelper> original = new Gson().fromJson(asJson, inJsonType);
original.forEach(input ->
{
incomingTransactions.add(input.customerTransactions);
});
} catch (Exception e) {
response.status(400);
return "Invalid transactions: " + e.getMessage();
}
for(Transaction t : incomingTransactions) {
if (t.getCustomerId() != customerId) {
response.status(400);
return "Transaction customer Id differs from request customer Id, security issue";
}
}
// no errors expected at this point
response.type("application/json");
synchronized (storage) {
List<Transaction> allCustomerTransactions;
if (storage.containsKey(customerId))
allCustomerTransactions = storage.get(customerId);
else {
allCustomerTransactions = new ArrayList<>();
storage.put(customerId, allCustomerTransactions);
}
allCustomerTransactions.addAll(incomingTransactions);
}
return "{ \"addedRecords\" : " + incomingTransactions.size() + "}";
}
);
// method to list existing transactions
get("/customerTransactions/:customerID/transactions",
(request, response) ->
{
long customerId;
try {
customerId = Long.parseLong(request.params(":customerID"));
} catch (Exception e) {
response.status(400);
return "Invalid customer id: " + e.getMessage();
}
TransactionState state;
try {
state = TransactionState.valueOf(request.queryParams("state"));
} catch (Exception e) {
response.status(400);
return "Invalid state: " + e.getMessage();
}
synchronized (storage) {
if (!storage.containsKey(customerId)) {
response.status(400);
return "Response.Status.NOT_FOUND";
}
// no errors expected at this point
response.type("application/json");
List<Transaction> allCustomerTransactions = storage.get(customerId);
List<Transaction> customerTransactions = allCustomerTransactions
.stream().filter(t -> t.getState() == state).collect(Collectors.toList());
String asJson = new Gson().toJson(customerTransactions, outJsonType);
return asJson;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment