use of com.example.expenses.model.Expense in project aws-java-serverless by hermanlintvelt.
the class DynamoDBRepository method findExpensesPaidBy.
@Override
public List<Expense> findExpensesPaidBy(Person person) {
LOG.info("Finding in DynamoDB: expenses for " + person);
// Build an Expression for a filtered query
Map<String, AttributeValue> expressionValues = Map.of(":value", AttributeValue.builder().s(person.getEmail()).build());
Expression expression = Expression.builder().expression("email = :value").expressionValues(expressionValues).build();
// Get items in the Customer table and write out the ID value.
List<Expense> expenses = expensesTable.query(r -> r.filterExpression(expression)).items().stream().map(ExpenseRecord::asExpense).collect(Collectors.toUnmodifiableList());
LOG.info(" ** found " + expenses.size() + " expenses");
return expenses;
}
use of com.example.expenses.model.Expense in project aws-java-serverless by hermanlintvelt.
the class ExpenseAggregate method createExpense.
/**
* Create an event and ensure it is stored in the repository
* @param expense
* @return
*/
public Expense createExpense(Expense expense) {
LOG.log(Level.INFO, "Creating an Expense for: " + expense);
dataRepository.addPerson(expense.getPaidByPerson());
Expense result = dataRepository.addExpense(expense);
eventNotifier.notifyEvent(new EventNotifier.ExpenseAdded(result));
return result;
}
use of com.example.expenses.model.Expense in project aws-java-serverless by hermanlintvelt.
the class GetExpensesHandlerTest method testGetExpenses.
@Test
@DisplayName("Test that request to function returns list of expenses")
void testGetExpenses() {
APIGatewayProxyResponseEvent result = testHandler.handleRequest(new APIGatewayProxyRequestEvent(), testContext);
assertThat(result.getStatusCode()).isEqualTo(200);
try {
List<Expense> expenses = OBJECT_MAPPER.readValue(result.getBody(), List.class);
assertThat(expenses).asList().isNotEmpty();
assertThat(expenses).asList().size().isGreaterThanOrEqualTo(3);
} catch (JsonProcessingException e) {
fail("did not expect json error");
}
}
use of com.example.expenses.model.Expense in project aws-java-serverless by hermanlintvelt.
the class ExpenseAggregate method createMockedData.
public void createMockedData() {
Person me = new Person("me@me.com");
createExpense(new Expense(BigDecimal.valueOf(150.0), me));
createExpense(new Expense(BigDecimal.valueOf(234.0), me));
createExpense(new Expense(BigDecimal.valueOf(234.0), new Person("you@me.com")));
}
use of com.example.expenses.model.Expense in project aws-java-serverless by hermanlintvelt.
the class CreateExpenseHandlerJsonNode method handleRequest.
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
LOG.info("Creating an expense from request: " + request.toString());
try {
JsonNode jsonNode = OBJECT_MAPPER.readTree(request.getBody());
String email = jsonNode.get("email").asText();
BigDecimal amount = BigDecimal.valueOf(jsonNode.get("amount").asDouble());
Expense newExpense = EXPENSE_AGGREGATE.createExpense(new Expense(amount, new Person(email)));
LOG.info("Created expense: " + newExpense);
return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(OBJECT_MAPPER.writeValueAsString(newExpense));
} catch (JsonProcessingException e) {
LOG.error("Error parsing request body to create new expense", e);
return new APIGatewayProxyResponseEvent().withStatusCode(400).withBody("Invalid Expense object in request body.");
}
}
Aggregations