Search in sources :

Example 1 with TransferTemplate

use of model.banking.TransferTemplate in project amos-ss17-alexa by c-i-ber.

the class DynamoDbClientTest method createAndDeleteItemTest.

@Test
public void createAndDeleteItemTest() {
    TransferTemplate mockTemplate = new TransferTemplate(0) {

        TransferTemplate init() {
            this.createdAt = new Date();
            this.target = "alex";
            this.amount = 5.0;
            return this;
        }
    }.init();
    assertEquals(0, mockTemplate.getId());
    client.putItem(TransferTemplate.TABLE_NAME, mockTemplate);
    assertNotEquals(0, mockTemplate.getId());
    client.deleteItem(TransferTemplate.TABLE_NAME, mockTemplate);
    List<TransferTemplate> transferTemplateList = client.getItems("transfer_template", TransferTemplate::new);
    assertFalse(transferTemplateList.contains(mockTemplate));
}
Also used : TransferTemplate(model.banking.TransferTemplate) Date(java.util.Date) Test(org.junit.Test)

Example 2 with TransferTemplate

use of model.banking.TransferTemplate in project amos-ss17-alexa by c-i-ber.

the class TransferTemplateService method tellTemplates.

private SpeechletResponse tellTemplates(Session session, int offset, int limit) {
    List<TransferTemplate> templateList = DynamoDbClient.instance.getItems(TransferTemplate.TABLE_NAME, TransferTemplate::new);
    List<TransferTemplate> templates = new ArrayList<>(templateList);
    if (offset >= templates.size()) {
        session.setAttribute("TransferTemplateService.offset", null);
        return getResponse(TRANSFER_TEMPLATES, "Keine weiteren Vorlagen.");
    }
    if (offset + limit >= templates.size()) {
        limit = templates.size() - offset;
    }
    Collections.sort(templates);
    StringBuilder response = new StringBuilder();
    for (int i = offset; i < offset + limit; i++) {
        TransferTemplate template = templates.get(i);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(template.getCreatedAt());
        String dateFormatted = String.format("01.%02d.%d", calendar.get(Calendar.MONTH) + 2, calendar.get(Calendar.YEAR));
        response.append("Vorlage ").append(template.getId()).append(" vom ").append(dateFormatted).append(": ");
        response.append("Überweise ").append(template.getAmount()).append(" Euro an ").append(template.getTarget()).append(". ");
    }
    boolean isAskResponse = templates.size() > offset + limit;
    if (isAskResponse) {
        response.append("Weitere Vorlagen vorlesen?");
        session.setAttribute("TransferTemplateService.offset", offset + limit);
        return getAskResponse(TRANSFER_TEMPLATES, response.toString());
    } else {
        response.append("Keine weiteren Vorlagen.");
        session.setAttribute("TransferTemplateService.offset", null);
        return getResponse(TRANSFER_TEMPLATES, response.toString());
    }
}
Also used : TransferTemplate(model.banking.TransferTemplate)

Example 3 with TransferTemplate

use of model.banking.TransferTemplate in project amos-ss17-alexa by c-i-ber.

the class DynamoDbClientTest method putAndDeleteItemTest.

@Test
public void putAndDeleteItemTest() {
    TransferTemplate transferTemplate1 = TransferTemplate.make("max", 10.0);
    TransferTemplate transferTemplate2 = TransferTemplate.make("johannes", 10.0);
    assertEquals(transferTemplate1.getId() + 1, transferTemplate2.getId());
    List<TransferTemplate> transferTemplateList = client.getItems(TransferTemplate.TABLE_NAME, TransferTemplate::new);
    assert (transferTemplateList.contains(transferTemplate1));
    assert (transferTemplateList.contains(transferTemplate2));
    client.deleteItem(TransferTemplate.TABLE_NAME, transferTemplate1);
    transferTemplateList = client.getItems("transfer_template", TransferTemplate::new);
    assertFalse(transferTemplateList.contains(transferTemplate1));
    assert (transferTemplateList.contains(transferTemplate2));
    client.deleteItem(TransferTemplate.TABLE_NAME, transferTemplate2);
    transferTemplateList = client.getItems("transfer_template", TransferTemplate::new);
    assertFalse(transferTemplateList.contains(transferTemplate1));
    assertFalse(transferTemplateList.contains(transferTemplate2));
}
Also used : TransferTemplate(model.banking.TransferTemplate) Test(org.junit.Test)

Example 4 with TransferTemplate

use of model.banking.TransferTemplate in project amos-ss17-alexa by c-i-ber.

the class TransferTemplateService method onIntent.

@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) {
    IntentRequest request = requestEnvelope.getRequest();
    Session session = requestEnvelope.getSession();
    String intentName = request.getIntent().getName();
    if (YES_INTENT.equals(intentName)) {
        Integer offset = (Integer) session.getAttribute("TransferTemplateService.offset");
        Integer templateId = (Integer) session.getAttribute("TransferTemplateService.delete");
        Integer editTemplateId = (Integer) session.getAttribute("TransferTemplateService.editTemplateId");
        if (offset != null) {
            return tellTemplates(session, offset, 3);
        }
        if (templateId != null) {
            TransferTemplate transferTemplate = new TransferTemplate(templateId);
            DynamoDbClient.instance.deleteItem(TransferTemplate.TABLE_NAME, transferTemplate);
            return getResponse(TRANSFER_TEMPLATES, "Vorlage wurde gelöscht.");
        }
        if (editTemplateId != null) {
            Double newAmount = Double.parseDouble(session.getAttribute("TransferTemplateService.newAmount").toString());
            TransferTemplate transferTemplate = (TransferTemplate) DynamoDbClient.instance.getItem(TransferTemplate.TABLE_NAME, editTemplateId, TransferTemplate::new);
            transferTemplate.setAmount(newAmount);
            DynamoDbClient.instance.putItem(TransferTemplate.TABLE_NAME, transferTemplate);
            return getResponse(TRANSFER_TEMPLATES, "Vorlage wurde erfolgreich gespeichert.");
        }
    } else if (NO_INTENT.equals(intentName)) {
        if (session.getAttribute("TransferTemplateService.offset") != null || session.getAttribute("TransferTemplateService.delete") != null || session.getAttribute("TransferTemplateService.editTemplateId") != null || session.getAttribute("TransferTemplateService.newAmount") != null) {
            return getResponse(TRANSFER_TEMPLATES, "Okay, dann halt nicht. Tschüss!");
        }
    } else if (LIST_TRANSFER_TEMPLATES_INTENT.equals(intentName)) {
        return tellTemplates(session, 0, 3);
    } else if (DELETE_TRANSFER_TEMPLATES_INTENT.equals(intentName)) {
        String templateIdStr = request.getIntent().getSlot("TemplateID").getValue();
        if (templateIdStr == null || templateIdStr.equals("")) {
            return null;
        } else {
            int templateId = Integer.parseInt(templateIdStr);
            session.setAttribute("TransferTemplateService.delete", templateId);
            return getAskResponse(TRANSFER_TEMPLATES, "Möchtest du Vorlage Nummer " + templateId + " wirklich löschen?");
        }
    } else if (EDIT_TRANSFER_TEMPLATE_INTENT.equals(intentName)) {
        String templateIdStr = request.getIntent().getSlot("TemplateID").getValue();
        String newAmountStr = request.getIntent().getSlot("NewAmount").getValue();
        if (templateIdStr == null || templateIdStr.equals("") || newAmountStr == null || newAmountStr.equals("")) {
            return null;
        } else {
            int templateId = Integer.parseInt(templateIdStr);
            TransferTemplate template = (TransferTemplate) DynamoDbClient.instance.getItem(TransferTemplate.TABLE_NAME, templateId, TransferTemplate::new);
            if (template == null) {
                return getResponse(TRANSFER_TEMPLATES, "Ich kann Vorlage " + templateId + " nicht finden.");
            }
            double newAmount = 0;
            try {
                newAmount = Double.parseDouble(newAmountStr);
            } catch (NumberFormatException ignored) {
            // TODO: Maybe do some error handling here
            }
            session.setAttribute("TransferTemplateService.editTemplateId", templateId);
            session.setAttribute("TransferTemplateService.newAmount", newAmount);
            return getAskResponse(TRANSFER_TEMPLATES, "Möchtest du den Betrag von Vorlage " + templateId + " von " + template.getAmount() + " auf " + newAmount + " ändern?");
        }
    }
    return null;
}
Also used : IntentRequest(com.amazon.speech.speechlet.IntentRequest) TransferTemplate(model.banking.TransferTemplate) Session(com.amazon.speech.speechlet.Session)

Aggregations

TransferTemplate (model.banking.TransferTemplate)4 Test (org.junit.Test)2 IntentRequest (com.amazon.speech.speechlet.IntentRequest)1 Session (com.amazon.speech.speechlet.Session)1 Date (java.util.Date)1