From ed197e2a5d7ac73ef21d1c8c1db88fb9ddb5f8ca Mon Sep 17 00:00:00 2001
From: Sebastian Reichel
Date: Mon, 16 Feb 2015 00:28:02 +0100
Subject: web: support setting product deprecation state
---
src/database/database.vala | 30 ++++++++++++++++++++++++++++++
src/database/db-interface.vala | 2 ++
src/web/web.vala | 39 +++++++++++++++++++++++++++++++++++++--
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 @@
EAN | {{{EAN}}} |
Name | {{{NAME}}} |
Amount | {{{AMOUNT}}} |
+ State | |
@@ -38,3 +39,25 @@
+
+
--
cgit v1.2.3