From 8cc5d214d2f77ed418e0a15e936fbee8dfbb0589 Mon Sep 17 00:00:00 2001 From: Lennart Weller Date: Sat, 16 Mar 2013 23:58:45 +0100 Subject: added a simple invoice api (for now local without authentication) --- Makefile | 4 ++-- src/web.vala | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6836132..be76705 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ SRC=src/main.vala src/device.vala src/scannersession.vala src/db.vala src/audio.vala src/web.vala src/graph-data.vala src/template.vala src/websession.vala src/admin.vala src/price.vapi src/ui/*.vala -DEPS=--pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 --pkg gee-1.0 --pkg gio-2.0 --pkg gstreamer-0.10 --pkg libarchive --pkg gpgme --pkg curses -X -lncursesw -FLAGS=-X -lgpgme -X -w --enable-experimental --thread --vapidir=vapi +DEPS=--pkg posix --pkg linux --pkg libsoup-2.4 --pkg sqlite3 --pkg gee-1.0 --pkg gio-2.0 --pkg gstreamer-0.10 --pkg libarchive --pkg gpgme --pkg curses --pkg json-glib-1.0 -X -lncursesw +FLAGS=-X -lgpgme -X -w --enable-experimental --thread --vapidir=vapi -g barcode-scanner: $(SRC) valac-0.16 --output $@ $(FLAGS) $(DEPS) $^ diff --git a/src/web.vala b/src/web.vala index 7140c22..b48ea4c 100644 --- a/src/web.vala +++ b/src/web.vala @@ -819,6 +819,56 @@ public class WebServer { } } + void handler_json(Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client) { + string action; + + if(query != null && query.contains("action")) { + action = query.get("action"); + if(action == "invoice" && query.contains("userid")) { + int userid = int.parse(query.get("userid")); + DateTime now, begin, end; + + now = new DateTime.now_local(); + if(query.contains("month")) { + int year, month = int.parse(query.get("month")); + if(query.contains("year")) + year = int.parse(query.get("year")); + else + year = now.get_year(); + + begin = new DateTime.local(year, month, 1, 0, 0, 0.0); + end = new DateTime.local(year, month, 1, 0, 0, 0.0); + end = end.add_months(1); + end = end.add_seconds(-1); + } + else { + begin = new DateTime.local(now.get_year(), now.get_month(), 1, 0, 0, 0.0); + end = now; + } + + + new Dialog("Begin: %s\nEnd: %s".printf(begin.to_string(), end.to_string())); + var gen = new Json.Generator(); + var root = new Json.Node(Json.NodeType.ARRAY); + var array = new Json.Array(); + root.set_array(array); + gen.set_root(root); + + foreach(var e in db.get_invoice(userid, begin.to_unix(), end.to_unix())) { + var o = new Json.Object(); + o.set_int_member("timestamp", e.timestamp); + o.set_string_member("product", e.product.name); + o.set_int_member("price", e.price); + array.add_object_element(o); + } + + msg.set_response("application/json", Soup.MemoryUse.COPY, gen.to_data(null).data); + } + } + else + handler_404(server, msg, path, query, client); + } + public WebServer(int port = 8080) { srv = new Soup.Server(Soup.SERVER_PORT, port); @@ -849,6 +899,9 @@ public class WebServer { srv.add_handler("/users/import", handler_user_import); srv.add_handler("/users/import-pgp", handler_user_pgp_import); + /* api */ + srv.add_handler("/api/0.1/", handler_json); + srv.run_async(); } } -- cgit v1.2.3