diff options
author | Sebastian Reichel <sre@ring0.de> | 2015-02-16 00:28:02 +0100 |
---|---|---|
committer | Sebastian Reichel <sre@ring0.de> | 2015-02-16 00:28:02 +0100 |
commit | ed197e2a5d7ac73ef21d1c8c1db88fb9ddb5f8ca (patch) | |
tree | 893bd75cde8e5a9cee071d483612249d8b079d5b | |
parent | 21d2375adf68c62feb19f7d5b93adf94337fe82c (diff) | |
download | serial-barcode-scanner-ed197e2a5d7ac73ef21d1c8c1db88fb9ddb5f8ca.tar.bz2 |
web: support setting product deprecation state
-rw-r--r-- | src/database/database.vala | 30 | ||||
-rw-r--r-- | src/database/db-interface.vala | 2 | ||||
-rw-r--r-- | src/web/web.vala | 39 | ||||
-rw-r--r-- | templates/products/entry.html | 23 |
4 files changed, 92 insertions, 2 deletions
diff --git a/src/database/database.vala b/src/database/database.vala index 9335b35..9c4e495 100644 --- a/src/database/database.vala +++ b/src/database/database.vala @@ -83,6 +83,8 @@ public class DataBase : Object { /* setup queries */ queries["product_name"] = "SELECT name FROM products WHERE id = ?"; queries["product_amount"] = "SELECT amount FROM products WHERE id = ?"; + queries["product_deprecated"]= "SELECT deprecated FROM products WHERE id = ?"; + queries["product_set_deprecated"] = "UPDATE products SET deprecated=? WHERE id = ?"; queries["products"] = "SELECT id, name, amount FROM products ORDER BY name"; queries["purchase"] = "INSERT INTO sales ('user', 'product', 'timestamp') VALUES (?, ?, ?)"; queries["last_purchase"] = "SELECT product FROM sales WHERE user = ? ORDER BY timestamp DESC LIMIT 1"; @@ -367,6 +369,34 @@ public class DataBase : Object { } } + public bool get_product_deprecated(uint64 article) throws DatabaseError { + statements["product_deprecated"].reset(); + statements["product_deprecated"].bind_text(1, "%llu".printf(article)); + + int rc = statements["product_deprecated"].step(); + + switch(rc) { + case Sqlite.ROW: + return statements["product_deprecated"].column_int(0) == 1; + case Sqlite.DONE: + throw new DatabaseError.PRODUCT_NOT_FOUND("unknown product: %llu", article); + default: + throw new DatabaseError.INTERNAL_ERROR("internal error: %d", rc); + } + } + + public void product_deprecate(uint64 article, bool value) throws DatabaseError { + int rc; + + statements["product_set_deprecated"].reset(); + statements["product_set_deprecated"].bind_int(1, value ? 1 : 0); + statements["product_set_deprecated"].bind_text(2, "%llu".printf(article)); + + rc = statements["product_set_deprecated"].step(); + if(rc != Sqlite.DONE) + throw new DatabaseError.INTERNAL_ERROR("internal error: %d", rc); + } + public Price get_product_price(int user, uint64 article) throws DatabaseError { int64 timestamp = (new DateTime.now_utc()).to_unix(); bool member = user != 0; diff --git a/src/database/db-interface.vala b/src/database/db-interface.vala index 5042c7f..979abf5 100644 --- a/src/database/db-interface.vala +++ b/src/database/db-interface.vala @@ -21,6 +21,8 @@ public interface Database : Object { public abstract bool buy(int32 user, uint64 article) throws IOError, DatabaseError; public abstract string get_product_name(uint64 article) throws IOError, DatabaseError; public abstract int get_product_amount(uint64 article) throws IOError, DatabaseError; + public abstract bool get_product_deprecated(uint64 article) throws IOError, DatabaseError; + public abstract void product_deprecate(uint64 article, bool value) throws IOError, DatabaseError; public abstract Price get_product_price(int user, uint64 article) throws IOError, DatabaseError; public abstract string undo(int32 user) throws IOError, DatabaseError; public abstract void restock(int user, uint64 product, uint amount, uint price, int supplier, int64 best_before_date) throws IOError, DatabaseError; diff --git a/src/web/web.vala b/src/web/web.vala index db32974..05a9b77 100644 --- a/src/web/web.vala +++ b/src/web/web.vala @@ -469,6 +469,9 @@ public class WebServer { case "newprice": handler_product_newprice(server, msg, path, query, client, id); break; + case "togglestate": + handler_product_togglestate(server, msg, path, query, client, id); + break; default: handler_product_entry(server, msg, path, query, client, id); break; @@ -509,6 +512,31 @@ public class WebServer { } } + void handler_product_togglestate(Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client, uint64 id) { + try { + var l = new WebSession(server, msg, path, query, client); + + if(!l.superuser && !l.auth_products) { + handler_403(server, msg, path, query, client); + return; + } + + var oldstate = db.get_product_deprecated(id); + db.product_deprecate(id, !oldstate); + var newstate = db.get_product_deprecated(id); + + var statestr = newstate ? "deprecated" : "active"; + msg.set_response("application/json", Soup.MemoryUse.COPY, @"{ \"state\": \"$statestr\" }".data); + msg.set_status(200); + } catch(DatabaseError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } catch(IOError e) { + stderr.printf(e.message+"\n"); + handler_400(server, msg, path, query, client); + } + } + void handler_product_entry(Soup.Server server, Soup.Message msg, string path, GLib.HashTable? query, Soup.ClientContext client, uint64 id) { try { var l = new WebSession(server, msg, path, query, client); @@ -526,10 +554,17 @@ public class WebServer { /* amount */ t.replace("AMOUNT", "%d".printf(db.get_product_amount(id))); - if(l.superuser || l.auth_products) + var deprecated = db.get_product_deprecated(id); + t.replace("BTNSTATE", deprecated ? "btn-danger" : "btn-success"); + t.replace("STATE", deprecated ? "Deprecated" : "Active"); + + if(l.superuser || l.auth_products) { t.replace("ISADMIN", "block"); - else + t.replace("ISADMIN2", ""); + } else { t.replace("ISADMIN", "none"); + t.replace("ISADMIN2", "disabled=\"disabled\""); + } /* prices */ string prices = ""; diff --git a/templates/products/entry.html b/templates/products/entry.html index a7d0d2f..80c1535 100644 --- a/templates/products/entry.html +++ b/templates/products/entry.html @@ -4,6 +4,7 @@ <th>EAN</th><td>{{{EAN}}}</td></tr> <th>Name</th><td>{{{NAME}}}</td></tr> <th>Amount</th><td>{{{AMOUNT}}}</td></tr> + <th>State</th><td><button id="statebutton" type="button" class="btn {{{BTNSTATE}}}" {{{ISADMIN2}}}>{{{STATE}}}</button></td></tr> </table> </p></p> @@ -38,3 +39,25 @@ <button type="submit" class="btn btn-primary"><i class="icon-plus"></i></button> </form> </div> + +<script> +$('#statebutton').on('click', function (e) { + var req = $.getJSON( + "/products/{{{EAN}}}/togglestate", + function( data ) { + if(data["state"] == "deprecated") { + $('#statebutton').html("Deprecated") + $('#statebutton').addClass("btn-danger") + $('#statebutton').removeClass("btn-success") + } else if(data["state"] == "active") { + $('#statebutton').html("Active") + $('#statebutton').addClass("btn-success") + $('#statebutton').removeClass("btn-danger") + } else { + console.error("unknown state") + console.info(data) + } + } + ); +}); +</script> |