diff --git a/db/tocosoap.sql b/db/tocosoap.sql index 4faaedbc14c78bbcf0b8a47eb547a7e68492d7b7..1bc5e5c5239cdb4c72838fe70670e6ccc953af8a 100644 --- a/db/tocosoap.sql +++ b/db/tocosoap.sql @@ -43,7 +43,7 @@ INSERT INTO `api` (`api_key`) VALUES ('toco_rest'), ('toco_php'); -CREATE TABLE `voucher`( +CREATE TABLE `voucher_record`( `vid` int(11) NOT NULL AUTO_INCREMENT, `code` varchar(255) NOT NULL, `user_id` int NOT NULL, diff --git a/src/main/java/org/toco/entity/voucher_entity.java b/src/main/java/org/toco/entity/voucher_entity.java index f786a85be6f1ced70eecfa17c1fd08d09f23f9fe..ddedd43106da4dc369531b8c619e9b76d169f0cd 100644 --- a/src/main/java/org/toco/entity/voucher_entity.java +++ b/src/main/java/org/toco/entity/voucher_entity.java @@ -6,10 +6,12 @@ public class voucher_entity { private String code; private Integer user_id; private Integer amount; + private String created_at; - public voucher_entity(String code, Integer user_id, Integer amount) { + public voucher_entity(String code, Integer user_id, Integer amount, String created_at) { this.code = code; this.user_id = user_id; this.amount = amount; + this.created_at = created_at; } } diff --git a/src/main/java/org/toco/model/voucher_model.java b/src/main/java/org/toco/model/voucher_model.java index 4f5937a4eeeddb8d5d96f9fcddde046b64a6e78d..d6935abb2a3eac3e75c89836251be651ee19cc57 100644 --- a/src/main/java/org/toco/model/voucher_model.java +++ b/src/main/java/org/toco/model/voucher_model.java @@ -7,7 +7,7 @@ import org.toco.entity.voucher_entity; public class voucher_model { public void insert (voucher_entity voucher_entity) { - String sql = "INSERT INTO voucher (code, user_id, amount) VALUES (?, ?, ?)"; + String sql = "INSERT INTO voucher_record (code, user_id, amount) VALUES (?, ?, ?)"; try (Connection connection = connector.connect() ; PreparedStatement command = connection.prepareStatement(sql)) { command.setString(1, voucher_entity.getCode()); @@ -20,24 +20,24 @@ public class voucher_model { } public voucher_entity [] getAllVouchers () { - String sql = "SELECT * FROM voucher"; + String sql = "SELECT * FROM voucher_record"; try (Connection connection = connector.connect() ; PreparedStatement command = connection.prepareStatement(sql)) { ResultSet result = command.executeQuery(); voucher_entity [] voucher_entity = new voucher_entity[getAllCount()]; int i = 0; while (result.next()) { - voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount")); + voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"), result.getString("timestamp")); i++; } return voucher_entity; } catch (SQLException exception) { - throw new RuntimeException("Error when inserting", exception); + throw new RuntimeException("Error when getting", exception); } } public voucher_entity[] getSpecifiedVoucher(String code){ - String sql = "SELECT * FROM voucher WHERE code = ?"; + String sql = "SELECT * FROM voucher_record WHERE code = ?"; try (Connection connection = connector.connect() ; PreparedStatement command = connection.prepareStatement(sql)) { command.setString(1, code); @@ -45,17 +45,17 @@ public class voucher_model { voucher_entity [] voucher_entity = new voucher_entity[getSpecifiedCount(code)]; int i = 0; while (result.next()) { - voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount")); + voucher_entity[i] = new voucher_entity(result.getString("code"), result.getInt("user_id"), result.getInt("amount"),result.getString("timestamp")); i++; } return voucher_entity; } catch (SQLException exception) { - throw new RuntimeException("Error when inserting", exception); + throw new RuntimeException("Error when getting", exception); } } public Integer getSpecifiedCount(String code){ - String sql = "SELECT COUNT(*) FROM voucher WHERE code = ?"; + String sql = "SELECT COUNT(*) FROM voucher_record WHERE code = ?"; try (Connection connection = connector.connect() ; PreparedStatement command = connection.prepareStatement(sql)) { command.setString(1, code); @@ -67,12 +67,12 @@ public class voucher_model { return 0; } } catch (SQLException exception) { - throw new RuntimeException("Error when inserting", exception); + throw new RuntimeException("Error when counting", exception); } } public Integer getAllCount(){ - String sql = "SELECT COUNT(*) FROM voucher"; + String sql = "SELECT COUNT(*) FROM voucher_record"; try (Connection connection = connector.connect() ; PreparedStatement command = connection.prepareStatement(sql)) { ResultSet result = command.executeQuery(); @@ -83,7 +83,7 @@ public class voucher_model { return 0; } } catch (SQLException exception) { - throw new RuntimeException("Error when inserting", exception); + throw new RuntimeException("Error when counting", exception); } } diff --git a/src/main/java/org/toco/service/toco_service_impl.java b/src/main/java/org/toco/service/toco_service_impl.java index c4277e61b1f0621104fe768a42723ca08ed92689..665696c57c14b42106448644ce21b0aca3d272c3 100644 --- a/src/main/java/org/toco/service/toco_service_impl.java +++ b/src/main/java/org/toco/service/toco_service_impl.java @@ -111,7 +111,7 @@ public class toco_service_impl implements toco_service { voucher_model voucherModel = new voucher_model(); userGems_model userGemsModel = new userGems_model(); transaction_model transactionModel = new transaction_model(); - voucher_entity voucherEntity = new voucher_entity(voucher,user_id,amount); + voucher_entity voucherEntity = new voucher_entity(voucher,user_id,amount,"0"); voucherModel.insert(voucherEntity); transactionModel.insert(new transaction_entity(user_id, amount, "voucher", "accepted", "0")); if (userGemsModel.checkUser(user_id)) { @@ -137,7 +137,7 @@ public class toco_service_impl implements toco_service { Integer len = voucherModel.getSpecifiedCount(code); String[] ret = new String[len]; for (int i = 0; i < len; i++) { - ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString(); + ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString()+", "+voucherEntity[i].getCreated_at(); } return ret; } @@ -155,7 +155,7 @@ public class toco_service_impl implements toco_service { Integer len = voucherModel.getAllCount(); String[] ret = new String[len]; for (int i = 0; i < len; i++) { - ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString(); + ret[i] = voucherEntity[i].getCode()+", "+voucherEntity[i].getAmount().toString()+", "+voucherEntity[i].getUser_id().toString()+", "+voucherEntity[i].getCreated_at(); } return ret; }