From 6ee822a218dc08a196cf206b383f2753b8bce090 Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Mon, 5 Jul 2021 09:55:50 +0300 Subject: [PATCH] Add REST API for PlugSurfing parser --- .../{Parser.java => PlugSurfingParser.java} | 11 ++++++++--- .../com/devsoap/parsers/plugsurfing/upload.html | 10 ++++++++++ rest-api/build.gradle | 1 + .../src/main/java/com/devsoap/parsers/api/Api.java | 10 ++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) rename plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/{Parser.java => PlugSurfingParser.java} (93%) create mode 100644 plugsurfing-invoice/src/main/resources/com/devsoap/parsers/plugsurfing/upload.html diff --git a/plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/Parser.java b/plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/PlugSurfingParser.java similarity index 93% rename from plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/Parser.java rename to plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/PlugSurfingParser.java index 444649c..7cf310e 100644 --- a/plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/Parser.java +++ b/plugsurfing-invoice/src/main/java/com/devsoap/parsers/plugsurfing/PlugSurfingParser.java @@ -5,6 +5,7 @@ import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.Path; import java.nio.file.Paths; import java.text.NumberFormat; @@ -16,7 +17,7 @@ import java.time.format.DateTimeFormatter; import java.util.*; import java.util.regex.Pattern; -public class Parser { +public class PlugSurfingParser { private static final Pattern DATE_TIME_KWH_DURATION_PATTERN = Pattern.compile("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}) \\((.*), (.*)kWh\\)"); private static final Pattern QUANTITY_PATTERN = Pattern.compile("(\\d+) (\\d+,\\d+) (\\d+)% (\\d+,\\d+) €"); @@ -30,8 +31,12 @@ public class Parser { public static void main(String[] args) { var filename = args[0]; var file = Paths.get(filename); - System.out.println("Vuosi,Kuukausi,Latauksia,Perusmaksu(€),Total(€),Energia(kwh)"); - parse(file).forEach((month, sessions) -> System.out.println(sessions.stream() + run(file, System.out); + } + + public static void run(Path file, PrintStream result) { + result.println("Vuosi,Kuukausi,Latauksia,Perusmaksu(€),Total(€),Energia(kwh)"); + parse(file).forEach((month, sessions) -> result.println(sessions.stream() .reduce(Session::add) .map(session -> String.format("%s,%s,%d,%.02f,%.02f,%.02f", month.split("-")[0], month.split("-")[1], diff --git a/plugsurfing-invoice/src/main/resources/com/devsoap/parsers/plugsurfing/upload.html b/plugsurfing-invoice/src/main/resources/com/devsoap/parsers/plugsurfing/upload.html new file mode 100644 index 0000000..cbbf553 --- /dev/null +++ b/plugsurfing-invoice/src/main/resources/com/devsoap/parsers/plugsurfing/upload.html @@ -0,0 +1,10 @@ + + + +
+ + + +
+ + \ No newline at end of file diff --git a/rest-api/build.gradle b/rest-api/build.gradle index 897e3d6..66c3e61 100644 --- a/rest-api/build.gradle +++ b/rest-api/build.gradle @@ -5,6 +5,7 @@ dependencies { implementation project(':caruna-invoice') implementation project(':helen-invoice') implementation project(':composite-parsers') + implementation project(':plugsurfing-invoice') implementation 'org.apache.logging.log4j:log4j-core:2.11.0' implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.11.0' diff --git a/rest-api/src/main/java/com/devsoap/parsers/api/Api.java b/rest-api/src/main/java/com/devsoap/parsers/api/Api.java index a42a22d..1af6f7d 100644 --- a/rest-api/src/main/java/com/devsoap/parsers/api/Api.java +++ b/rest-api/src/main/java/com/devsoap/parsers/api/Api.java @@ -3,6 +3,7 @@ package com.devsoap.parsers.api; import com.devsoap.parsers.caruna.CarunaParser; import com.devsoap.parsers.composites.CarunaHelenParser; import com.devsoap.parsers.helen.HelenParser; +import com.devsoap.parsers.plugsurfing.PlugSurfingParser; import io.javalin.Javalin; import io.javalin.http.Context; import io.javalin.http.UploadedFile; @@ -73,6 +74,15 @@ public class Api { }); }); + path(PlugSurfingParser.class.getSimpleName(), () -> { + get(ctx -> ctx.render(renderParserStaticPage(PlugSurfingParser.class, "upload.html"))); + post(ctx -> { + withUploadedFile(ctx.uploadedFiles().get(0), plugFile -> { + renderParserOutput(ctx, result -> PlugSurfingParser + .run(plugFile, result), "plugsurfing-report.csv"); + }); + }); + }); }); });