Search in sources :

Example 1 with Expense

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;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) Expense(com.example.expenses.model.Expense)

Example 2 with Expense

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;
}
Also used : Expense(com.example.expenses.model.Expense) SQSEventNotifier(com.example.expenses.lambda.aws.SQSEventNotifier)

Example 3 with Expense

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");
    }
}
Also used : APIGatewayProxyRequestEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent) Expense(com.example.expenses.model.Expense) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with Expense

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")));
}
Also used : Expense(com.example.expenses.model.Expense) Person(com.example.expenses.model.Person)

Example 5 with Expense

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.");
    }
}
Also used : Expense(com.example.expenses.model.Expense) JsonNode(com.fasterxml.jackson.databind.JsonNode) APIGatewayProxyResponseEvent(com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent) Person(com.example.expenses.model.Person) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BigDecimal(java.math.BigDecimal)

Aggregations

Expense (com.example.expenses.model.Expense)12 Person (com.example.expenses.model.Person)7 DisplayName (org.junit.jupiter.api.DisplayName)6 Test (org.junit.jupiter.api.Test)6 APIGatewayProxyResponseEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)5 APIGatewayProxyRequestEvent (com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent)2 SQSEventNotifier (com.example.expenses.lambda.aws.SQSEventNotifier)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BigDecimal (java.math.BigDecimal)1 Expression (software.amazon.awssdk.enhanced.dynamodb.Expression)1 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)1